#include <date.h>
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_ |
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.
|
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(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 } |
|
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 } |
|
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 } |
|
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" 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 } |
|
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 } |
|
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 } |
|
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 } |
|
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 } |
|
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 } |
|
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 } |
|
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 } |
|
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 } |
|
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 } |
|
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. 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 } |
|
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 } |
|
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 } |
|
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 } |
|
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 } |
|
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 } |
|
Returns number of secs since 1/1/1970.
Definition at line 122 of file date.h. Referenced by operator<(), and operator==().
00122 { return time_; } |
|
Set time.
|
|
Date comparison respects chronological order. |
|
|
|
|