Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members  

Date Class Reference

A simple Date class. More...

#include <date.h>

List of all members.

Public Methods

 Date ()
 Default constructor, initializes to ``now''.

 Date (time_t secs)
 Date(time_t t) assumes that t is number of seconds since Jan 1, 1970.

 Date (unsigned int year, unsigned int month, unsigned int day, unsigned int hrs=0, unsigned int min=0, unsigned int sec=0)
 Initialize Date, note that january = 0.

 Date (const string &s) throw (DateError)
 The string s is parsed as a date. More...

string str (const char *fmt=0) const
 Produce a string representing the date, according to format. More...

string gmt (const char *fmt=0) const
 Like str() but formats in GMT time zone, e.g. 03:00 MET DST is 02:00 GMT.

long time () const
 Returns number of secs since 1/1/1970.

void time (long time)
 Set time.

unsigned short year () const
 Return number of years. More...

unsigned short month () const
 Return month of date, january = 0.

unsigned short day () const
 Return day in month (1..31).

unsigned short hours () const
 Return hour of day, between 00 and 23.

unsigned short minutes () const
 Return minutes in the hour, between 0 and 59.

unsigned short seconds () const
 Return seconds in the minute, between 0 and 59.

unsigned short wday () const
 Return weekday, sunday = 0, monday = 1, ..

unsigned short yday () const
 Return day in year, between 0 and 365.

void year (unsigned short yr)
 Set year. More...

void month (unsigned short mo)
void day (unsigned short day)
void hours (unsigned short hrs)
void minutes (unsigned short mins)
void seconds (unsigned short secs)
bool operator< (const Date &d) const
bool operator== (const Date &d) const

Private Attributes

time_t time_


Detailed Description

A simple Date class.

It provides a convenient interface to system functions such as locatime_r() etc. Its only data member is a time_t value. Note that only dates since Jan 1, 1970 can be represented.

The default copy constructor and assignment operations are available.

Definition at line 31 of file date.h.


Constructor & Destructor Documentation

Date::Date ( )
 

Default constructor, initializes to ``now''.

Definition at line 20 of file date.C.

00020            {
00021 struct  timeval t;
00022 /*
00023 *       t.tv_sec:       secs since 1/1/1970
00024 *       t.tv_usec:      microseconds
00025 */
00026 gettimeofday(&t, 0);
00027 time_ = t.tv_sec;
00028 }

Date::Date ( time_t t )
 

Date(time_t t) assumes that t is number of seconds since Jan 1, 1970.

Definition at line 30 of file date.C.

00030                   : time_(t) {
00031 }

Date::Date ( unsigned int year,
unsigned int month,
unsigned int day,
unsigned int hrs = 0,
unsigned int min = 0,
unsigned int sec = 0 )
 

Initialize Date, note that january = 0.

Definition at line 33 of file date.C.

00034                                                                        : time_(0) {
00035 struct tm       tm;
00036 localtime_r(&time_,&tm);
00037 tm.tm_sec       = sec;
00038 tm.tm_min       = min;
00039 tm.tm_hour      = hrs-1; // don't ask; it just works that way
00040 tm.tm_mday      = day;
00041 tm.tm_mon       = month;
00042 tm.tm_year      = year-1900;
00043 time_           = mktime(&tm);
00044 }

Date::Date ( const string & s ) throw (DateError)
 

The string s is parsed as a date.

This constructor uses the gnu getdate parsing function which accepts a number of formats, e.g.

        1970-09-17           # ISO 8601.
        70-9-17              # This century assumed by default.
        70-09-17             # Leading zeros are ignored.
        9/17/72              # Common U.S. writing.
        24 September 1972
        24 Sept 72           # September has a special abbreviation.
        24 Sep 72            # Three-letter abbreviations always allowed.
        Sep 24, 1972
        24-sep-72
        24sep72

        20:02:0
        20:02
        8:02pm
        20:02-0500              # In EST (Eastern U.S. Standard Time)
      

and many others (see the gnu filutils documentation)

Definition at line 188 of file date.C.

00189 {
00190 static const string error(": cannot parse \"");
00191 time_ = get_date(s.c_str(),0);
00192 if (time_<0)
00193   throw DateError(error+s+"\"");
00194 }


Member Function Documentation

string Date::str ( const char * fmt = 0 ) const
 

Produce a string representing the date, according to format.

The format is any string acceptable to strftime. If fmt=0, the default is "%a %b %d %H:%M:%S %Y" e.g.

                "Wed Aug 12 18:38:10 1998"
The default format is acceptable to the parser.

Definition at line 151 of file date.C.

00151                                {
00152 static const char*      d_fmt = "%a %b %d %H:%M:%S %Y";
00153 // %Z, i.e. time zone abbreviation, not included in the default
00154 if (fmt==0)
00155   fmt = d_fmt;
00156 const int size = strlen(fmt)*20;
00157 char* buf = new char[size+1];
00158 
00159 struct tm       tm;
00160 localtime_r(&time_,&tm);
00161 
00162 strftime(buf, size, fmt, &tm);
00163 string  s(buf);
00164 delete buf;
00165 return s;
00166 }

string Date::gmt ( const char * fmt = 0 ) const
 

Like str() but formats in GMT time zone, e.g. 03:00 MET DST is 02:00 GMT.

Definition at line 169 of file date.C.

00169                                {
00170 static const char*      d_fmt = "%a %b %d %H:%M:%S %Z %Y";
00171 
00172 if (fmt==0)
00173   fmt = d_fmt;
00174 const int size = strlen(fmt)*20;
00175 char* buf = new char[size+1];
00176 
00177 struct tm       tm;
00178 gmtime_r(&time_,&tm);
00179 
00180 strftime(buf, size, fmt, &tm);
00181 string  s(buf);
00182 delete buf;
00183 return s;
00184 }

unsigned short Date::year ( ) const
 

Return number of years.

Inspection functions that return a part of the Date object.

Definition at line 47 of file date.C.

Referenced by LogRecord::parse_date().

00047                  {
00048 struct tm       tm;
00049 localtime_r(&time_,&tm);
00050 return tm.tm_year + 1900;
00051 }

unsigned short Date::month ( ) const
 

Return month of date, january = 0.

Definition at line 54 of file date.C.

Referenced by LogRecord::parse_date().

00054                   {
00055 struct tm       tm;
00056 localtime_r(&time_,&tm);
00057 return tm.tm_mon;
00058 }

unsigned short Date::day ( ) const
 

Return day in month (1..31).

Definition at line 61 of file date.C.

Referenced by LogRecord::parse_date().

00061                 {
00062 struct tm       tm;
00063 localtime_r(&time_,&tm);
00064 return tm.tm_mday;
00065 }

unsigned short Date::hours ( ) const
 

Return hour of day, between 00 and 23.

Definition at line 68 of file date.C.

Referenced by LogRecord::parse_date().

00068                   {
00069 struct tm       tm;
00070 localtime_r(&time_,&tm);
00071 return tm.tm_hour;
00072 }

unsigned short Date::minutes ( ) const
 

Return minutes in the hour, between 0 and 59.

Definition at line 75 of file date.C.

00075                     {
00076 struct tm       tm;
00077 localtime_r(&time_,&tm);
00078 return tm.tm_min;
00079 }

unsigned short Date::seconds ( ) const
 

Return seconds in the minute, between 0 and 59.

Definition at line 82 of file date.C.

00082                     {
00083 struct tm       tm;
00084 localtime_r(&time_,&tm);
00085 return tm.tm_sec;
00086 }

unsigned short Date::wday ( ) const
 

Return weekday, sunday = 0, monday = 1, ..

Definition at line 89 of file date.C.

00089                  {
00090 struct tm       tm;
00091 localtime_r(&time_,&tm);
00092 return tm.tm_wday;
00093 }

unsigned short Date::yday ( ) const
 

Return day in year, between 0 and 365.

Definition at line 96 of file date.C.

00096                  {
00097 struct tm       tm;
00098 localtime_r(&time_,&tm);
00099 return tm.tm_yday;
00100 }

void Date::year ( unsigned short yr )
 

Set year.

Functions that set part of the Date object. Values that are too large will be converted to the e.g. next day. E.g.

      Date d("Mon Jan 01 23:00:00 CET 2001");
      d.hours(36);
      cout<< d.str();
will print "Tue Jan 02 12:00:00 CET 2001".

Definition at line 103 of file date.C.

00103                             {
00104 struct tm       tm;
00105 localtime_r(&time_,&tm);
00106 tm.tm_year = yr - 1900;
00107 time_   = mktime(&tm);
00108 }

void Date::month ( unsigned short mo )
 

Definition at line 111 of file date.C.

00111                              {
00112 struct tm       tm;
00113 localtime_r(&time_,&tm);
00114 tm.tm_mon = mo;
00115 time_   = mktime(&tm);
00116 }

void Date::day ( unsigned short day )
 

Definition at line 119 of file date.C.

00119                            {
00120 struct tm       tm;
00121 localtime_r(&time_,&tm);
00122 tm.tm_mday = dy;
00123 time_   = mktime(&tm);
00124 }

void Date::hours ( unsigned short hrs )
 

Definition at line 127 of file date.C.

00127                              {
00128 struct tm       tm;
00129 localtime_r(&time_,&tm);
00130 tm.tm_hour = hr;
00131 time_   = mktime(&tm);
00132 }

void Date::minutes ( unsigned short mins )
 

Definition at line 135 of file date.C.

00135                                  {
00136 struct tm       tm;
00137 localtime_r(&time_,&tm);
00138 tm.tm_min = mins;
00139 time_   = mktime(&tm);
00140 }

void Date::seconds ( unsigned short secs )
 

Definition at line 143 of file date.C.

00143                                  {
00144 struct tm       tm;
00145 localtime_r(&time_,&tm);
00146 tm.tm_sec = secs;
00147 time_   = mktime(&tm);
00148 }

long Date::time ( ) const [inline]
 

Returns number of secs since 1/1/1970.

Definition at line 122 of file date.h.

Referenced by operator<(), and operator==().

00122 { return time_; }

void Date::time ( long time )
 

Set time.

bool Date::operator< ( const Date & d ) const [inline]
 

Date comparison respects chronological order.

Definition at line 129 of file date.h.

00129 { return (time() < d.time()); }

bool Date::operator== ( const Date & d ) const [inline]
 

Definition at line 130 of file date.h.

00130 { return (time() == d.time()); }


Member Data Documentation

time_t Date::time_ [private]
 

Definition at line 134 of file date.h.


The documentation for this class was generated from the following files:
httpstats-stage04 [ 7 April, 2001]