From: arjen Date: Mon, 2 Sep 2002 06:18:07 +0000 (+0000) Subject: Fixed some date and time conversion functions X-Git-Tag: V0_3~17 X-Git-Url: http://www.andromeda.nl/gitweb/?p=AXE.git;a=commitdiff_plain;h=6ad12d90579d11d1fb37c8d3b4702d727fa74a00 Fixed some date and time conversion functions --- diff --git a/ChangeLog b/ChangeLog index 3902e25..1ca3b06 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +Sep 02, 2002 - Patch 0.2.2 +-------------------------- + + - date::format() is now properly implemented + - New operator to read an 'hour' object from an istream: + istream & operator >> (istream &, hour &) + - New function 'String hour::format()'. The default format is HH:MM:SS + in 24 hour clock. + - New function 'hour now()' read the current time of day. + Jul 27, 2002 - Patch 0.2.1 --------------------------- diff --git a/demos/acltest.cpp b/demos/acltest.cpp index 689ddb1..299f655 100644 --- a/demos/acltest.cpp +++ b/demos/acltest.cpp @@ -5,7 +5,7 @@ *********************** ** FILE NAME : acltest.cpp ** SYSTEM NAME : AXE - Andromeda X-windows Encapsulation -** VERSION NUMBER : $Revision: 1.2 $ +** VERSION NUMBER : $Revision: 1.3 $ ** ** DESCRIPTION : Test routine for non-X classes ** @@ -22,7 +22,10 @@ /***************************** $Log: acltest.cpp,v $ - Revision 1.2 2002-07-27 06:34:06 arjen + Revision 1.3 2002-09-02 06:18:12 arjen + Fixed some date and time conversion functions + + Revision 1.2 2002/07/27 06:34:06 arjen Removed the obsolete complex number test from acltest. BUG FIX: testaxe crashed when no configuration file is available. @@ -31,7 +34,7 @@ *****************************/ -static const char *RCSID = "$Id: acltest.cpp,v 1.2 2002-07-27 06:34:06 arjen Exp $"; +static const char *RCSID = "$Id: acltest.cpp,v 1.3 2002-09-02 06:18:12 arjen Exp $"; #include "String.h" #include "integer.h" @@ -109,6 +112,7 @@ int main() cout << "c = b : c = " << c << "\n"; cout << "***********************\nDate Test\n*********************\n"; + date d1, d2(22,7,1964); cout << "Default constructor: " << d1 << ", with (d,m,y): " << d2 << "\n"; @@ -120,7 +124,7 @@ int main() d2 = date(datestring); cout << datestring << " parses into (DD-MM-YYYY): " << d2 << "\n"; - datestring = d2.format(); + datestring = d2.format("%F"); cout << "Formatted date: " << datestring << "\n"; d1 = today(); @@ -160,5 +164,9 @@ int main() cout << "t1 - t2 = " << t1 - t2 << "\n"; cout << "t2 - t1 = " << t2 - t1 << "\n"; + d1 = today(); + t1 = now(); + + cout << "\nThe time now is " << d1 << " " << t1 << "\n"; return 0; } diff --git a/src/configuration.h b/src/configuration.h index f2ea550..47e81a6 100644 --- a/src/configuration.h +++ b/src/configuration.h @@ -5,7 +5,7 @@ *********************** ** FILE NAME : configuration.h ** SYSTEM NAME : AXE - Andromeda X-windows Encapsulation -** VERSION NUMBER : $Revision: 1.1 $ +** VERSION NUMBER : $Revision: 1.2 $ ** ** DESCRIPTION : Definition of configuration class ** @@ -23,12 +23,15 @@ /***************************** $Log: configuration.h,v $ - Revision 1.1 2002-07-25 08:01:26 arjen + 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 First checkin, AXE release 0.2 *****************************/ -/* static const char *RCSID = "$Id: configuration.h,v 1.1 2002-07-25 08:01:26 arjen Exp $"; */ +/* static const char *RCSID = "$Id: configuration.h,v 1.2 2002-09-02 06:18:20 arjen Exp $"; */ #ifndef CONFIGURATION_H #define CONFIGURATION_H @@ -44,6 +47,7 @@ // MEMBERS : // OPERATORS : // METHODS : read +// find_parameter // // DESCRIPTION : Handle configurational parameters for the application. // Many applications need some permanently stored configurational @@ -51,7 +55,7 @@ // wide configuration file and a configuration file per user. // The content of the configuration file is in XML format. // The configuration base class takes care of finding the configuration -// files, e.g. in /etc/app.conf or in /usr/loca/etc/app.conf, along +// files, e.g. in /etc/app.conf or in /usr/local/etc/app.conf, along // with the configuration file in the user's home directory. // The config files are parsed with the gnome XML parser and a // framework is provided to find configurational items. diff --git a/src/date.cpp b/src/date.cpp index 5094fca..5213fce 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.1 $ +** VERSION NUMBER : $Revision: 1.2 $ ** ** DESCRIPTION : ** @@ -22,12 +22,15 @@ /***************************** $Log: date.cpp,v $ - Revision 1.1 2002-07-25 08:01:26 arjen + 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 First checkin, AXE release 0.2 *****************************/ -static const char *RCSID = "$Id: date.cpp,v 1.1 2002-07-25 08:01:26 arjen Exp $"; +static const char *RCSID = "$Id: date.cpp,v 1.2 2002-09-02 06:18:20 arjen Exp $"; #include #include @@ -307,16 +310,15 @@ String date::MonthName(void) String date::format(const char *fmt) { String s; - //char buf[40]; + char buf[40]; struct tm t; - t.tm_year = year; - t.tm_mon = month; + t.tm_year = year - 1900; + t.tm_mon = month - 1; t.tm_mday = day; - s = String(int(day)) + String(" ") + MonthName() + String(" ") + String(year); - //strftime(buf, 40, fmt, &t); - //s = buf; + strftime(buf, 40, fmt, &t); + s = buf; return s; } diff --git a/src/date.h b/src/date.h index 1a4f6b0..0789ef4 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.1 $ +** VERSION NUMBER : $Revision: 1.2 $ ** ** DESCRIPTION : ** @@ -23,12 +23,15 @@ /***************************** $Log: date.h,v $ - Revision 1.1 2002-07-25 08:01:26 arjen + 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 First checkin, AXE release 0.2 *****************************/ -/* static const char *RCSID = "$Id: date.h,v 1.1 2002-07-25 08:01:26 arjen Exp $"; */ +/* static const char *RCSID = "$Id: date.h,v 1.2 2002-09-02 06:18:20 arjen Exp $"; */ #ifndef AXE_DATE_H #define AXE_DATE_H @@ -186,10 +189,10 @@ public: friend ostream& operator<<(ostream&, const date&); friend istream& operator>>(istream&, date&); - String format(const char *fmt = "%x"); + String format(const char *fmt = "%F"); }; -date today(void); +date today(); class hour { @@ -219,8 +222,13 @@ public: friend hour operator-(hour &, hour &); friend ostream& operator<<(ostream &, const hour &); + friend istream& operator>>(istream &, hour &); + + String format(const char *fmt = "%T"); }; +hour now(); + class UTC { hour t; diff --git a/src/hour.cpp b/src/hour.cpp index 3394ff7..46210c2 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.1 $ +** VERSION NUMBER : $Revision: 1.2 $ ** ** DESCRIPTION : ** @@ -22,12 +22,17 @@ /***************************** $Log: hour.cpp,v $ - Revision 1.1 2002-07-25 08:01:27 arjen + 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 First checkin, AXE release 0.2 *****************************/ -static const char *RCSID = "$Id: hour.cpp,v 1.1 2002-07-25 08:01:27 arjen Exp $"; +static const char *RCSID = "$Id: hour.cpp,v 1.2 2002-09-02 06:18:20 arjen Exp $"; + +#include #include "date.h" #include "parsedate.h" @@ -42,6 +47,17 @@ hour::hour(String s) seconds = pd->second; } +hour now() +{ + long clock; + struct tm *tp; + + time(&clock); + tp = localtime(&clock); + + return hour(tp->tm_hour, tp->tm_min, tp->tm_sec); +} + hour operator+(hour &t1, hour &t2) { hour t = t1; @@ -107,7 +123,45 @@ hour operator-(hour &t1, hour &t2) ostream& operator<<(ostream &s, const hour &t) { - s << t.hours << ":" << t.minutes << ":" << t.seconds; + s.width(2); + s.fill('0'); + s << t.hours << ":"; + s.width(2); + s.fill('0'); + s << t.minutes << ":"; + s.width(2); + s.fill('0'); + s << t.seconds; + + return s; +} + +istream &operator>>(istream &s, hour &h) +{ + char c; + int H, M, S; + + s >> H >> c >> M >> c >> S; + + h.hours = H; + h.minutes = M; + h.seconds = S; + + return s; +} + +String hour::format(const char *fmt) +{ + String s; + char buf[40]; + struct tm t; + + t.tm_sec = seconds; + t.tm_min = minutes; + t.tm_hour = hours; + + strftime(buf, 40, fmt, &t); + s = buf; return s; }