00001 #ifndef DV_DATE_H 00002 #define DV_DATE_H 00003 // $Id: date.h,v 1.30 2008/03/12 17:45:16 dvermeir Exp $ 00004 00005 #include <sys/time.h> 00006 00007 #include <stdexcept> 00008 #include <iostream> 00009 #include <string> 00010 00011 /** @file 00012 * A simple Dv::Date class that provides a convenient interface to 00013 * system functions such as locatime_r() etc. Note that only 00014 * dates since 1970 can be represented. 00015 */ 00016 namespace Dv { 00017 class Duration; 00018 00019 /** A simple Date class. 00020 * It provides a convenient interface to system functions such as locatime_r() etc. 00021 * Its only data member is a time_t value. Note that only dates since 00022 * Jan 1, 1970 can be represented. 00023 * 00024 * The default copy constructor and assignment operations are available. 00025 */ 00026 class Date { 00027 public: 00028 /** Default constructor, initializes to ``now''. */ 00029 Date(); 00030 /** Date(time_t t) assumes that t is number of seconds since Jan 1, 1970 */ 00031 Date(time_t secs); // Date(0) is NULL date 00032 /** Initialize Date, note that january = 0. */ 00033 Date(unsigned int year, unsigned int month, unsigned int day, 00034 unsigned int hrs=0, unsigned int min=0, unsigned int sec=0); 00035 00036 /** Construct a date from a string representation. 00037 * This constructor uses the gnu getdate parsing function 00038 * which accepts a number of formats, e.g. 00039 * 00040 * @verbatim 00041 * 1970-09-17 # ISO 8601. 00042 * 1970-09-17 20:02:00 00043 * 70-9-17 # This century assumed by default. 00044 * 70-09-17 # Leading zeros are ignored. 00045 * 9/17/72 # Common U.S. writing. 00046 * 24 September 1972 00047 * 24 Sept 72 # September has a special abbreviation. 00048 * 24 Sep 72 # Three-letter abbreviations always allowed. 00049 * Sep 24, 1972 00050 * 24-sep-72 00051 * 24sep72 00052 * 00053 * 20:02:0 00054 * 20:02 00055 * 8:02pm 00056 * 20:02-0500 # In EST (Eastern U.S. Standard Time) 00057 * @endverbatim 00058 * 00059 * and many others (see the gnu fileutils documentation). 00060 * 00061 * As a special case, it also accepts the Mysql timestamp format, 00062 * i.e. strings of the form YYYYMMDDHHMMSS, e.g. "20051023170328". 00063 */ 00064 Date(const std::string& s) throw (std::runtime_error); 00065 00066 /** Produce a string representing the date, according to format. 00067 * The format is any string acceptable to strftime. 00068 * If fmt=0, the default is "\%a \%b \%d \%H:\%M:\%S \%Y" 00069 * e.g. 00070 * @code 00071 * "Wed Aug 12 18:38:10 1998" 00072 * @endcode 00073 * The default format is acceptable to the parser. 00074 */ 00075 std::string str(const char*fmt=0) const; 00076 00077 /** Like str() but formats in GMT time zone, e.g. 03:00 MET DST is 02:00 GMT. */ 00078 std::string gmt(const char*fmt=0) const; 00079 00080 /** Seconds east of UTC (GMT) of this date. 00081 * @return the offset of the current timezone in seconds east from UTC 00082 */ 00083 long int gmt_offset() const; 00084 00085 /** Return number of years. 00086 * @return the number of years of this Date. 00087 */ 00088 unsigned short year() const; 00089 00090 /** Return month of Date. 00091 * @return the month (january = 0) of this Date. 00092 */ 00093 unsigned short month() const; 00094 00095 /** Is daylight savings time (DST) in effect? 00096 * See man mktime. 00097 * @return -1 if unknown 00098 * @return 0 if not 00099 * @return >0 if DST is in effect 00100 */ 00101 int isdst() const; 00102 00103 /** Return day-in-month of Date. 00104 * @return the day in the month (1..31) of this Date. 00105 */ 00106 unsigned short day() const; 00107 00108 /** Return the hour in the day of Date. 00109 * @return the hour in the day (0..23) of this Date. 00110 */ 00111 unsigned short hours() const; 00112 00113 /** Return the minutes in the hour of this Date. 00114 * @return the minutes (0..59) in the hour of this Date. 00115 */ 00116 unsigned short minutes() const; 00117 00118 /** Return the seconds in the minute of this Date. 00119 * @return the seconds (0..59) in the minute of this Date. 00120 */ 00121 unsigned short seconds() const; 00122 00123 /** Return the day in the week of this Date. 00124 * @param monday_is_0 if true, use 00125 * (monday = 0, tuesday = 1, .., sunday = 6) representation, 00126 * otherwise use 00127 * (sunday = 0, monday = 1, tuesday = 1, .., saturday = 6) representation. 00128 * @return number of the day in the week of this Date, using the 00129 * specified convention. 00130 */ 00131 unsigned short wday(bool monday_is_0 = false) const; 00132 00133 /** Return the day in the year of this Date. 00134 * @return the day in the year (0..365) of this Date. 00135 */ 00136 unsigned short yday() const; 00137 00138 /** Return the number of the week in the year of this Date. 00139 * From http://www.merlyn.demon.co.uk/: All ISO weeks are Monday 00140 * = 1 to Sunday = 7, and week 01 of the numbering year is the 00141 * one containing the first Thursday of the Gregorian year. Thus 00142 * up to three days of the first and last weeks for a year 00143 * number can be in the adjacent Gregorian year; December 29 to 00144 * January 03 can have a numbering year differing from the 00145 * Gregorian. 00146 * @return The ISO 8601:1988 week number of the current year as a 00147 * decimal number, range 01 to 53, where week 1 is the first week that 00148 * has at least 4 days in the current year, and with Monday as the 00149 * first day of the week. 00150 */ 00151 unsigned short yweek() const; 00152 00153 /** Return the number of the week since the epoch. 00154 * Jan 1st 1970 is a thursday, so the first ISO week 00155 * (with number 1) starts on monday Jan 5th 1970. 00156 * @return the number of the week since the epoch 00157 * (Jan 1st 1970 00:00 GMT, or Jan 1st 1970 01:00 MET). 00158 */ 00159 unsigned long week_since_epoch() const; 00160 00161 /** Return the number days since the epoch. 00162 * Jan 1st 1970 is day 0. 00163 * From http://www.merlyn.demon.co.uk/: All ISO weeks are Monday 00164 * = 1 to Sunday = 7, and week 01 of the numbering year is the 00165 * one containing the first Thursday of the Gregorian year. Thus 00166 * up to three days of the first and last weeks for a year 00167 * number can be in the adjacent Gregorian year; December 29 to 00168 * January 03 can have a numbering year differing from the 00169 * Gregorian. 00170 * @return the number days since the epoch 00171 * (Jan 1st 1970 00:00 GMT, or Jan 1st 1970 01:00 MET). 00172 */ 00173 unsigned long days_since_epoch() const; 00174 00175 /** Set year. 00176 * Functions that set part of the Date object. 00177 * Values that are too large will be converted, e.g. 00178 * to next day. E.g. 00179 * @code 00180 * Date d("Mon Jan 01 23:00:00 CET 2001"); 00181 * d.hours(36); 00182 * cout<< d.str(); 00183 * @endcode 00184 * will print "Tue Jan 02 12:00:00 CET 2001". 00185 * @warning daylight savings time may cause strange behaviour 00186 */ 00187 void year(unsigned short yr); 00188 00189 /** Set month. 00190 * Functions that set part of the Date object. 00191 * Values that are too large will be converted, e.g. 00192 * to next day. E.g. 00193 * @code 00194 * Date d("Mon Jan 01 23:00:00 CET 2001"); 00195 * d.hours(36); 00196 * cout<< d.str(); 00197 * @endcode 00198 * will print "Tue Jan 02 12:00:00 CET 2001". 00199 * @param mo month (jan=0, .., dec=11) 00200 * @warning daylight savings time may cause strange behaviour 00201 */ 00202 void month(unsigned short mo); 00203 00204 /** Set day in month. 00205 * Functions that set part of the Date object. 00206 * Values that are too large will be converted, e.g. 00207 * to next day. E.g. 00208 * @code 00209 * Date d("Mon Jan 01 23:00:00 CET 2001"); 00210 * d.hours(36); 00211 * cout<< d.str(); 00212 * @endcode 00213 * will print "Tue Jan 02 12:00:00 CET 2001". 00214 * @param day (1..31) 00215 * @warning daylight savings time may cause strange behaviour 00216 */ 00217 void day(unsigned short day); 00218 00219 /** Set hours. 00220 * Functions that set part of the Date object. 00221 * Values that are too large will be converted, e.g. 00222 * to next day. E.g. 00223 * @code 00224 * Date d("Mon Jan 01 23:00:00 CET 2001"); 00225 * d.hours(36); 00226 * cout<< d.str(); 00227 * @endcode 00228 * will print "Tue Jan 02 12:00:00 CET 2001". 00229 * @param hrs (0..23) 00230 * @warning daylight savings time may cause strange behaviour 00231 */ 00232 void hours(unsigned short hrs); 00233 00234 /** Set minutes. 00235 * Functions that set part of the Date object. 00236 * Values that are too large will be converted, e.g. 00237 * to next day. E.g. 00238 * @code 00239 * Date d("Mon Jan 01 23:00:00 CET 2001"); 00240 * d.hours(36); 00241 * cout<< d.str(); 00242 * @endcode 00243 * will print "Tue Jan 02 12:00:00 CET 2001". 00244 * @param mins (0..59) 00245 * @warning daylight savings time may cause strange behaviour 00246 */ 00247 void minutes(unsigned short mins); 00248 00249 /** Set seconds. 00250 * Functions that set part of the Date object. 00251 * Values that are too large will be converted, e.g. 00252 * to next day. E.g. 00253 * @code 00254 * Date d("Mon Jan 01 23:00:00 CET 2001"); 00255 * d.hours(36); 00256 * cout<< d.str(); 00257 * @endcode 00258 * will print "Tue Jan 02 12:00:00 CET 2001". 00259 * @param secs (0..59) 00260 * @warning daylight savings time may cause strange behaviour 00261 */ 00262 void seconds(unsigned short secs); 00263 00264 /** Set date to date with this yday in same year. 00265 * Values that are too large will be converted, e.g. 00266 * to next day. E.g. 00267 * @code 00268 * Date d("Mon Jan 01 23:00:00 CET 2001"); 00269 * d.hours(36); 00270 * cout<< d.str(); 00271 * @endcode 00272 * will print "Tue Jan 02 12:00:00 CET 2001". 00273 * @param yday (0..365) 00274 * @warning daylight savings time may cause strange behaviour 00275 */ 00276 void yday(unsigned short yday); 00277 00278 /** Returns number of secs since 1/1/1970. 00279 * @return number of secs since the epoch 00280 * (Jan 1st 1970 00:00 GMT, or Jan 1st 1970 01:00 MET). 00281 */ 00282 time_t time() const { return time_; } 00283 00284 /** Set time. 00285 * @param time number of secs since the epoch 00286 * (Jan 1st 1970 00:00 GMT, or Jan 1st 1970 01:00 MET). 00287 */ 00288 void time(time_t time); 00289 00290 /** Date comparison respects chronological order. */ 00291 bool operator<(const Date& d) const { return (time() < d.time()); } 00292 /** Date comparison respects chronological order. */ 00293 bool operator==(const Date& d) const { return (time() == d.time()); } 00294 /** Date comparison respects chronological order. */ 00295 bool operator>(const Date& d) const { return d < *this; } 00296 /** Date comparison respects chronological order. */ 00297 bool operator!=(const Date& d) const { return ! (d == *this); } 00298 /** Date comparison respects chronological order. */ 00299 bool operator>=(const Date& d) const { return ! (*this < d); } 00300 /** Date comparison respects chronological order. */ 00301 bool operator<=(const Date& d) const { return ! (*this > d); } 00302 00303 /** Add a duration to a Date. 00304 * @param duration to add 00305 */ 00306 Date& operator+=(const Duration& duration); 00307 00308 private: 00309 /* Nr of secs since the epoch 00310 * (Jan 1st 1970 00:00 GMT, or Jan 1st 1970 01:00 MET). 00311 */ 00312 time_t time_; 00313 }; 00314 00315 /** Read a date from an input stream. 00316 * @warning a whole line is read and nothing is put back. In other 00317 * words, this function assumes that the date representation occupies 00318 * the rest of the current line. 00319 * @param is input stream 00320 * @param date Dv::Util::Date object that will contain the read date 00321 * @return is 00322 */ 00323 std::istream& operator>>(std::istream& is, Date& date); 00324 00325 /** Write a date to an output stream. The format used is "%F %T", i.e. 00326 * YYY-MM-DD (ISO 8601), followed by HH:MM:SS. 00327 * @param os output stream 00328 * @param date to write 00329 * @return os 00330 * @see Dv::Util::Date::str 00331 */ 00332 std::ostream& operator<<(std::ostream& os, const Date& date); 00333 } 00334 #endif
dvutil-1.0.10 | [ 5 December, 2009] |