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 21 of file date.C.

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

Date::Date ( time_t t )
 

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

Definition at line 31 of file date.C.

00031                   : time_(t) {
00032 }

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 34 of file date.C.

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

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 189 of file date.C.

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


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 152 of file date.C.

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

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 170 of file date.C.

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

unsigned short Date::year ( ) const
 

Return number of years.

Inspection functions that return a part of the Date object.

Definition at line 48 of file date.C.

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

unsigned short Date::month ( ) const
 

Return month of date, january = 0.

Definition at line 55 of file date.C.

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

unsigned short Date::day ( ) const
 

Return day in month (1..31).

Definition at line 62 of file date.C.

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

unsigned short Date::hours ( ) const
 

Return hour of day, between 00 and 23.

Definition at line 69 of file date.C.

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

unsigned short Date::minutes ( ) const
 

Return minutes in the hour, between 0 and 59.

Definition at line 76 of file date.C.

Referenced by LogRecord::parse_date().

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

unsigned short Date::seconds ( ) const
 

Return seconds in the minute, between 0 and 59.

Definition at line 83 of file date.C.

Referenced by LogRecord::parse_date().

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

unsigned short Date::wday ( ) const
 

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

Definition at line 90 of file date.C.

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

unsigned short Date::yday ( ) const
 

Return day in year, between 0 and 365.

Definition at line 97 of file date.C.

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

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 104 of file date.C.

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

void Date::month ( unsigned short mo )
 

Definition at line 112 of file date.C.

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

void Date::day ( unsigned short day )
 

Definition at line 120 of file date.C.

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

void Date::hours ( unsigned short hrs )
 

Definition at line 128 of file date.C.

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

void Date::minutes ( unsigned short mins )
 

Definition at line 136 of file date.C.

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

void Date::seconds ( unsigned short secs )
 

Definition at line 144 of file date.C.

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

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-stage01 [ 7 April, 2001]