From: arjen Date: Sat, 28 Sep 2002 06:58:45 +0000 (+0000) Subject: Bugfix: conversion of an empty string to a date or hour object X-Git-Tag: V0_3~10 X-Git-Url: http://www.andromeda.nl/gitweb/?p=AXE.git;a=commitdiff_plain;h=785587ad1d3412c73c923b77416c73b42777ade7 Bugfix: conversion of an empty string to a date or hour object now makes the values of such an object 0 (null) instead of giving a segmentation fault. The class UTC combines the date and hour classes. The most basic functions of the UTC class are now implemented. These include constructors and conversion to and from String objects. New functions: date::proper(), hour::proper() and UTC::proper(). Return true if the object holds a proper clock time and/or calendar date; false if at least one value is out of range. --- diff --git a/src/Makefile.am b/src/Makefile.am index c971693..80fbaf2 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -8,7 +8,8 @@ libAXE_a_SOURCES = xappl.cpp xwindow.cpp font.cpp menu.cpp frame.cpp \ filedialog.cpp \ parsedate.c datelex.c dateyacc.y \ button.cpp edit.cpp scroll.cpp table.cpp geometry.cpp icon.cpp \ - string.cpp regex.cpp integer.cpp date.cpp hour.cpp amount.cpp out.cpp \ + string.cpp regex.cpp integer.cpp date.cpp hour.cpp utc.cpp \ + amount.cpp out.cpp \ configuration.cpp include_HEADERS = table.h menu.h button.h edit.h scroll.h pixmap.h \ diff --git a/src/Makefile.in b/src/Makefile.in index 6570ea7..04e9b9d 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -77,7 +77,8 @@ libAXE_a_SOURCES = xappl.cpp xwindow.cpp font.cpp menu.cpp frame.cpp \ filedialog.cpp \ parsedate.c datelex.c dateyacc.y \ button.cpp edit.cpp scroll.cpp table.cpp geometry.cpp icon.cpp \ - string.cpp regex.cpp integer.cpp date.cpp hour.cpp amount.cpp out.cpp \ + string.cpp regex.cpp integer.cpp date.cpp hour.cpp utc.cpp \ + amount.cpp out.cpp \ configuration.cpp @@ -104,8 +105,8 @@ X_PRE_LIBS = @X_PRE_LIBS@ libAXE_a_LIBADD = libAXE_a_OBJECTS = xappl.o xwindow.o font.o menu.o frame.o filedialog.o \ parsedate.o datelex.o dateyacc.o button.o edit.o scroll.o table.o \ -geometry.o icon.o string.o regex.o integer.o date.o hour.o amount.o \ -out.o configuration.o +geometry.o icon.o string.o regex.o integer.o date.o hour.o utc.o \ +amount.o out.o configuration.o AR = ar CXXFLAGS = @CXXFLAGS@ CXXCOMPILE = $(CXX) $(DEFS) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) @@ -129,7 +130,7 @@ DEP_FILES = .deps/amount.P .deps/button.P .deps/configuration.P \ .deps/filedialog.P .deps/font.P .deps/frame.P .deps/geometry.P \ .deps/hour.P .deps/icon.P .deps/integer.P .deps/menu.P .deps/out.P \ .deps/parsedate.P .deps/regex.P .deps/scroll.P .deps/string.P \ -.deps/table.P .deps/xappl.P .deps/xwindow.P +.deps/table.P .deps/utc.P .deps/xappl.P .deps/xwindow.P SOURCES = $(libAXE_a_SOURCES) OBJECTS = $(libAXE_a_OBJECTS) diff --git a/src/date.cpp b/src/date.cpp index 5213fce..6ba3375 100644 --- a/src/date.cpp +++ b/src/date.cpp @@ -5,7 +5,7 @@ *********************** ** FILE NAME : date.cpp ** SYSTEM NAME : AXE - Andromeda X-windows Encapsulation -** VERSION NUMBER : $Revision: 1.2 $ +** VERSION NUMBER : $Revision: 1.3 $ ** ** DESCRIPTION : ** @@ -22,7 +22,18 @@ /***************************** $Log: date.cpp,v $ - Revision 1.2 2002-09-02 06:18:20 arjen + Revision 1.3 2002-09-28 06:58:45 arjen + Bugfix: conversion of an empty string to a date or hour object + now makes the values of such an object 0 (null) instead of giving + a segmentation fault. + The class UTC combines the date and hour classes. The most basic + functions of the UTC class are now implemented. + These include constructors and conversion to and from String objects. + New functions: date::proper(), hour::proper() and UTC::proper(). + Return true if the object holds a proper clock time and/or calendar + date; false if at least one value is out of range. + + Revision 1.2 2002/09/02 06:18:20 arjen Fixed some date and time conversion functions Revision 1.1 2002/07/25 08:01:26 arjen @@ -30,7 +41,7 @@ *****************************/ -static const char *RCSID = "$Id: date.cpp,v 1.2 2002-09-02 06:18:20 arjen Exp $"; +static const char *RCSID = "$Id: date.cpp,v 1.3 2002-09-28 06:58:45 arjen Exp $"; #include #include @@ -57,10 +68,19 @@ date::date(String s) { struct parseddate *pd; - pd = parsedate(s); - year = pd->year; - month = pd->month; - day = pd->day; + if (~s == 0) + { + year = 0; + month = 0; + day = 0; + } + else + { + pd = parsedate(s); + year = pd->year; + month = pd->month; + day = pd->day; + } } int date::Leap(void) @@ -170,6 +190,13 @@ date date::subtract(unsigned long days) return *this; } +bool date::proper() +{ + return year != -1 && year != 0 && + month > 0 && month <= 12 && + day > 0 && day <= DaysInMonth(); +} + date operator+(date d1, date d2) { d1.add(d2); @@ -260,7 +287,7 @@ long operator-(date &d1, date &d2) return d1.julian() - d2.julian(); } -ostream &operator<<(ostream &s, const date &d) +std::ostream &operator<<(std::ostream &s, const date &d) { s.width(2); s.fill('0'); @@ -274,7 +301,7 @@ ostream &operator<<(ostream &s, const date &d) return s; } -istream &operator>>(istream &s, date &d) +std::istream &operator>>(std::istream &s, date &d) { char c; int D, M, Y; diff --git a/src/date.h b/src/date.h index 0789ef4..2a500d4 100644 --- a/src/date.h +++ b/src/date.h @@ -5,7 +5,7 @@ *********************** ** FILE NAME : date.h ** SYSTEM NAME : AXE - Andromeda X-windows Encapsulation -** VERSION NUMBER : $Revision: 1.2 $ +** VERSION NUMBER : $Revision: 1.3 $ ** ** DESCRIPTION : ** @@ -23,7 +23,18 @@ /***************************** $Log: date.h,v $ - Revision 1.2 2002-09-02 06:18:20 arjen + Revision 1.3 2002-09-28 06:58:45 arjen + Bugfix: conversion of an empty string to a date or hour object + now makes the values of such an object 0 (null) instead of giving + a segmentation fault. + The class UTC combines the date and hour classes. The most basic + functions of the UTC class are now implemented. + These include constructors and conversion to and from String objects. + New functions: date::proper(), hour::proper() and UTC::proper(). + Return true if the object holds a proper clock time and/or calendar + date; false if at least one value is out of range. + + Revision 1.2 2002/09/02 06:18:20 arjen Fixed some date and time conversion functions Revision 1.1 2002/07/25 08:01:26 arjen @@ -31,7 +42,7 @@ *****************************/ -/* static const char *RCSID = "$Id: date.h,v 1.2 2002-09-02 06:18:20 arjen Exp $"; */ +/* static const char *RCSID = "$Id: date.h,v 1.3 2002-09-28 06:58:45 arjen Exp $"; */ #ifndef AXE_DATE_H #define AXE_DATE_H @@ -49,6 +60,8 @@ class date date subtract(unsigned long days); + friend class UTC; + public: date() @@ -67,6 +80,8 @@ public: date(String s); + bool proper(); // Check wether this is a proper calendar date + friend date operator+(date, date); friend date operator+(unsigned long, date); friend date operator+(date, unsigned long); @@ -186,8 +201,8 @@ public: // date operator/(double) // date& operator/=(double) - friend ostream& operator<<(ostream&, const date&); - friend istream& operator>>(istream&, date&); + friend std::ostream& operator<<(std::ostream&, const date&); + friend std::istream& operator>>(std::istream&, date&); String format(const char *fmt = "%F"); }; @@ -199,6 +214,8 @@ class hour int hours; short minutes, seconds; + friend class UTC; + public: hour() @@ -217,12 +234,14 @@ public: hour(String s); + bool proper(); // Check wether this is a proper clock time + friend hour operator+(hour &, hour &); hour operator += (hour h); friend hour operator-(hour &, hour &); - friend ostream& operator<<(ostream &, const hour &); - friend istream& operator>>(istream &, hour &); + friend std::ostream& operator<<(std::ostream &, const hour &); + friend std::istream& operator>>(std::istream &, hour &); String format(const char *fmt = "%T"); }; @@ -238,7 +257,37 @@ public: UTC() { + /* Leave everything to the default hour and date constructors */ + } + + UTC(const String s); + UTC(date dt, hour tm) + { + d = dt; + t = tm; + } + + operator date() + { + return d; } + + operator hour() + { + return t; + } + + bool proper() + { + return d.proper() && t.proper(); + } + + friend std::ostream& operator<<(std::ostream &, const UTC &); + friend std::istream& operator>>(std::istream &, UTC &); + + String format(const char *fmt = "%F %T"); }; +UTC Now(); + #endif /* AXE_DATE_H */ diff --git a/src/datelex.c b/src/datelex.c index d61cc67..ac8e492 100644 --- a/src/datelex.c +++ b/src/datelex.c @@ -1,7 +1,18 @@ /*$Log: datelex.c,v $ -/*Revision 1.1 2002-07-25 08:01:26 arjen -/*First checkin, AXE release 0.2 +/*Revision 1.2 2002-09-28 06:58:45 arjen +/*Bugfix: conversion of an empty string to a date or hour object +/*now makes the values of such an object 0 (null) instead of giving +/*a segmentation fault. +/*The class UTC combines the date and hour classes. The most basic +/*functions of the UTC class are now implemented. +/*These include constructors and conversion to and from String objects. +/*New functions: date::proper(), hour::proper() and UTC::proper(). +/*Return true if the object holds a proper clock time and/or calendar +/*date; false if at least one value is out of range. /* + *Revision 1.1 2002/07/25 08:01:26 arjen + *First checkin, AXE release 0.2 + * * Revision 1.1 84/09/01 15:01:14 wales * Initial revision * @@ -47,7 +58,7 @@ /* AJB, Aug 28 1999: Added month names in Dutch */ #ifdef RCSIDENT -static char rcsident[] = "$Header: /cvsroot/lib/AXE/src/datelex.c,v 1.1 2002-07-25 08:01:26 arjen Exp $"; +static char rcsident[] = "$Header: /cvsroot/lib/AXE/src/datelex.c,v 1.2 2002-09-28 06:58:45 arjen Exp $"; #endif /* RCSIDENT */ #include @@ -223,9 +234,12 @@ yylex () c = buffer; d = yyinbuf; /* Skip over blanks, tabs, commas, and parentheses. */ - do { *c = *d++; } - while (*c == ' ' || *c == '\t' || *c == ',' - || *c == '(' || *c == ')'); + do + { + *c = *d++; + } + while (*c != '\0' && (*c == ' ' || *c == '\t' || *c == ',' + || *c == '(' || *c == ')')); /* A zero (null) byte signals the end of the input. */ if (*c == 0) diff --git a/src/dateyacc.y b/src/dateyacc.y index 12a09a4..b3b0525 100644 --- a/src/dateyacc.y +++ b/src/dateyacc.y @@ -1,5 +1,16 @@ /*$Log: dateyacc.y,v $ -/*Revision 1.1 2002-07-25 08:01:26 arjen +/*Revision 1.2 2002-09-28 06:58:45 arjen +/*Bugfix: conversion of an empty string to a date or hour object +/*now makes the values of such an object 0 (null) instead of giving +/*a segmentation fault. +/*The class UTC combines the date and hour classes. The most basic +/*functions of the UTC class are now implemented. +/*These include constructors and conversion to and from String objects. +/*New functions: date::proper(), hour::proper() and UTC::proper(). +/*Return true if the object holds a proper clock time and/or calendar +/*date; false if at least one value is out of range. +/* +/*Revision 1.1 2002/07/25 08:01:26 arjen /*First checkin, AXE release 0.2 /* * Revision 1.1 84/09/01 15:01:22 wales @@ -39,8 +50,8 @@ %{ #ifdef RCSIDENT -static char rcsident[] = "$Header: /cvsroot/lib/AXE/src/dateyacc.y,v 1.1 2002-07-25 08:01:26 arjen Exp $"; -#endif RCSIDENT +static char rcsident[] = "$Header: /cvsroot/lib/AXE/src/dateyacc.y,v 1.2 2002-09-28 06:58:45 arjen Exp $"; +#endif /*RCSIDENT*/ #include #include "parsedate.h" diff --git a/src/hour.cpp b/src/hour.cpp index 46210c2..703c126 100644 --- a/src/hour.cpp +++ b/src/hour.cpp @@ -5,7 +5,7 @@ *********************** ** FILE NAME : hour.cpp ** SYSTEM NAME : AXE - Andromeda X-windows Encapsulation -** VERSION NUMBER : $Revision: 1.2 $ +** VERSION NUMBER : $Revision: 1.3 $ ** ** DESCRIPTION : ** @@ -22,7 +22,18 @@ /***************************** $Log: hour.cpp,v $ - Revision 1.2 2002-09-02 06:18:20 arjen + Revision 1.3 2002-09-28 06:58:45 arjen + Bugfix: conversion of an empty string to a date or hour object + now makes the values of such an object 0 (null) instead of giving + a segmentation fault. + The class UTC combines the date and hour classes. The most basic + functions of the UTC class are now implemented. + These include constructors and conversion to and from String objects. + New functions: date::proper(), hour::proper() and UTC::proper(). + Return true if the object holds a proper clock time and/or calendar + date; false if at least one value is out of range. + + Revision 1.2 2002/09/02 06:18:20 arjen Fixed some date and time conversion functions Revision 1.1 2002/07/25 08:01:27 arjen @@ -30,7 +41,7 @@ *****************************/ -static const char *RCSID = "$Id: hour.cpp,v 1.2 2002-09-02 06:18:20 arjen Exp $"; +static const char *RCSID = "$Id: hour.cpp,v 1.3 2002-09-28 06:58:45 arjen Exp $"; #include @@ -41,10 +52,19 @@ hour::hour(String s) { struct parseddate *pd; - pd = parsedate(s); - hours = pd->hour; - minutes = pd->minute; - seconds = pd->second; + if (~s == 0) + { + hours = 0; + minutes = 0; + seconds = 0; + } + else + { + pd = parsedate(s); + hours = pd->hour; + minutes = pd->minute; + seconds = pd->second; + } } hour now() @@ -58,6 +78,15 @@ hour now() return hour(tp->tm_hour, tp->tm_min, tp->tm_sec); } +/* An hour is a proper hour if it lies within the 24-hour clock. */ + +bool hour::proper() +{ + return hours >= 0 && hours < 24 && + minutes >= 0 && minutes < 60 && + seconds >= 0 && seconds < 60; +} + hour operator+(hour &t1, hour &t2) { hour t = t1; @@ -121,7 +150,7 @@ hour operator-(hour &t1, hour &t2) return t; } -ostream& operator<<(ostream &s, const hour &t) +std::ostream& operator<<(std::ostream &s, const hour &t) { s.width(2); s.fill('0'); @@ -136,7 +165,7 @@ ostream& operator<<(ostream &s, const hour &t) return s; } -istream &operator>>(istream &s, hour &h) +std::istream &operator>>(std::istream &s, hour &h) { char c; int H, M, S; diff --git a/src/parsedate.c b/src/parsedate.c index 883dfcc..61de4b1 100644 --- a/src/parsedate.c +++ b/src/parsedate.c @@ -60,7 +60,7 @@ #include #include "parsedate.h" -static char *RCSID = "$Id: parsedate.c,v 1.1 2002-07-25 08:01:27 arjen Exp $"; +static char *RCSID = "$Id: parsedate.c,v 1.2 2002-09-28 06:58:45 arjen Exp $"; extern int yyparse(); @@ -107,6 +107,7 @@ struct parseddate * parsedate (char *date) /* Parse the argument string. */ yyinbuf = date; + if (yyparse () != 0 && yyans.error == NULL) yyans.error = yyinbuf; diff --git a/src/utc.cpp b/src/utc.cpp new file mode 100644 index 0000000..e106c87 --- /dev/null +++ b/src/utc.cpp @@ -0,0 +1,119 @@ +/************************************************************************** +** (c) Copyright 1999, Andromeda Technology & Automation +*************************************************************************** +** MODULE INFORMATION * +*********************** +** FILE NAME : utc.cpp +** SYSTEM NAME : AXE - Andromeda X-windows Encapsulation +** VERSION NUMBER : $Revision: 1.1 $ +** +** DESCRIPTION : +** +** EXPORTED OBJECTS : +** LOCAL OBJECTS : +** MODULES USED : +*************************************************************************** +** ADMINISTRATIVE INFORMATION * +******************************** +** ORIGINAL AUTHOR : Arjen Baart - arjen@andromeda.nl +** CREATION DATE : Sep 16, 2002 +** LAST UPDATE : Sep 16, 2002 +**************************************************************************/ + +/***************************** + $Log: utc.cpp,v $ + Revision 1.1 2002-09-28 06:58:45 arjen + Bugfix: conversion of an empty string to a date or hour object + now makes the values of such an object 0 (null) instead of giving + a segmentation fault. + The class UTC combines the date and hour classes. The most basic + functions of the UTC class are now implemented. + These include constructors and conversion to and from String objects. + New functions: date::proper(), hour::proper() and UTC::proper(). + Return true if the object holds a proper clock time and/or calendar + date; false if at least one value is out of range. + + +*****************************/ + +static const char *RCSID = "$Id: utc.cpp,v 1.1 2002-09-28 06:58:45 arjen Exp $"; + +#include + +#include "date.h" +#include "parsedate.h" + +UTC::UTC(String s) +{ + struct parseddate *pd; + + if (~s == 0) + { + t.hours = 0; + t.minutes = 0; + t.seconds = 0; + d.year = 0; + d.month = 0; + d.day = 0; + } + else + { + pd = parsedate(s); + t.hours = pd->hour; + t.minutes = pd->minute; + t.seconds = pd->second; + d.year = pd->year; + d.month = pd->month; + d.day = pd->day; + } +} + +UTC Now() +{ + long clock; + struct tm *tp; + date d; + hour t; + + time(&clock); + tp = localtime(&clock); + + t = hour(tp->tm_hour, tp->tm_min, tp->tm_sec); + d = date(tp->tm_mday, tp->tm_mon+1, tp->tm_year+1900); + + return UTC(d, t); +} + +std::ostream& operator<<(std::ostream &s, const UTC &t) +{ + s << t.d << " " << t.t; + + return s; +} + +std::istream &operator>>(std::istream &s, UTC &t) +{ + s >> t.d >> t.t; + + return s; +} + +String UTC::format(const char *fmt) +{ + String s; + char buf[80]; + struct tm ut; + + ut.tm_sec = t.seconds; + ut.tm_min = t.minutes; + ut.tm_hour = t.hours; + ut.tm_year = d.year - 1900; + ut.tm_mon = d.month - 1; + ut.tm_mday = d.day; + + strftime(buf, 80, fmt, &ut); + s = buf; + + return s; +} +