00001 #ifndef DV_DATE_H 00002 #define DV_DATE_H 00003 // $Id: date.h,v 1.18 2004/12/30 11:42:30 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. 00014 */ 00015 00016 namespace Dv { 00017 namespace Util { 00018 class Duration; 00019 00020 //! A runtime exception thrown by some Date operations. 00021 class DateError: public std::runtime_error { 00022 public: 00023 //! Name of class, is prepended to each message argument of the constructor. 00024 static const std::string NAME; 00025 //! Constructor, prepends NAME to message to obtain runtime_error::what(). 00026 DateError(const std::string& message): std::runtime_error(NAME+": "+message) {} 00027 }; 00028 00029 //! A simple Date class. 00030 /*! It provides a convenient interface to system functions such as locatime_r() etc. 00031 Its only data member is a time_t value. Note that only dates since 00032 Jan 1, 1970 can be represented. 00033 00034 The default copy constructor and assignment operations are available. 00035 */ 00036 class Date { 00037 public: 00038 //! Default constructor, initializes to ``now''. 00039 Date(); 00040 //! Date(time_t t) assumes that t is number of seconds since Jan 1, 1970 00041 Date(time_t secs); // Date(0) is NULL date 00042 //! Initialize Date, note that january = 0. 00043 Date(unsigned int year, unsigned int month, unsigned int day, 00044 unsigned int hrs=0, unsigned int min=0, unsigned int sec=0); 00045 00046 //! The string s is parsed as a date. 00047 /*! This constructor uses the gnu getdate parsing function 00048 which accepts a number of formats, e.g. 00049 00050 \verbatim 00051 1970-09-17 # ISO 8601. 00052 70-9-17 # This century assumed by default. 00053 70-09-17 # Leading zeros are ignored. 00054 9/17/72 # Common U.S. writing. 00055 24 September 1972 00056 24 Sept 72 # September has a special abbreviation. 00057 24 Sep 72 # Three-letter abbreviations always allowed. 00058 Sep 24, 1972 00059 24-sep-72 00060 24sep72 00061 00062 20:02:0 00063 20:02 00064 8:02pm 00065 20:02-0500 # In EST (Eastern U.S. Standard Time) 00066 \endverbatim 00067 00068 and many others (see the gnu filutils documentation) 00069 */ 00070 Date(const std::string& s) throw (DateError); 00071 00072 //! Produce a string representing the date, according to format. 00073 /*! The format is any string acceptable to strftime. 00074 If fmt=0, the default is "\%a \%b \%d \%H:\%M:\%S \%Y" 00075 e.g. 00076 \code 00077 "Wed Aug 12 18:38:10 1998" 00078 \endcode 00079 The default format is acceptable to the parser. 00080 */ 00081 std::string str(const char*fmt=0) const; 00082 //! Like str() but formats in GMT time zone, e.g. 03:00 MET DST is 02:00 GMT. 00083 std::string gmt(const char*fmt=0) const; 00084 //@{ 00085 /*! Inspection functions that return a part of the Date object. 00086 */ 00087 //! Return number of years. 00088 unsigned short year() const; 00089 //! Return month of date, january = 0. 00090 unsigned short month() const; 00091 //! Return day in month (1..31). 00092 unsigned short day() const; 00093 //! Return hour of day, between 00 and 23. 00094 unsigned short hours() const; 00095 //! Return minutes in the hour, between 0 and 59. 00096 unsigned short minutes() const; 00097 //! Return seconds in the minute, between 0 and 59. 00098 unsigned short seconds() const; 00099 //! Return weekday, sunday = 0, monday = 1, .. 00100 unsigned short wday() const; 00101 //! Return day in year, between 0 and 365. 00102 unsigned short yday() const; 00103 //! Return number of week (0..53) in year, week 1 starts with first monday, 00104 unsigned short yweek() const; 00105 //@} 00106 00107 //@{ 00108 //! Set year, month, day-in-month, hours, minutes or seconds. 00109 /*! Functions that set part of the Date object. 00110 Values that are too large will be converted, e.g. 00111 to e.g. next day. E.g. 00112 \code 00113 Date d("Mon Jan 01 23:00:00 CET 2001"); 00114 d.hours(36); 00115 cout<< d.str(); 00116 \endcode 00117 will print "Tue Jan 02 12:00:00 CET 2001". 00118 */ 00119 void year(unsigned short yr); 00120 void month(unsigned short mo); 00121 void day(unsigned short day); 00122 void hours(unsigned short hrs); 00123 void minutes(unsigned short mins); 00124 void seconds(unsigned short secs); 00125 //@} 00126 //! Set date to date with this yday in same year. 00127 void yday(unsigned short yday); 00128 00129 00130 //! Returns number of secs since 1/1/1970. 00131 time_t time() const { return time_; } 00132 //! Set time. 00133 void time(time_t time); 00134 00135 //@{ 00136 /*! Date comparison respects chronological order. 00137 */ 00138 bool operator<(const Date& d) const { return (time() < d.time()); } 00139 bool operator==(const Date& d) const { return (time() == d.time()); } 00140 bool operator>(const Date& d) const { return d < *this; } 00141 bool operator!=(const Date& d) const { return ! (d == *this); } 00142 bool operator>=(const Date& d) const { return ! (*this < d); } 00143 bool operator<=(const Date& d) const { return ! (*this > d); } 00144 //@} 00145 00146 //! Add a duration to a Date. 00147 Date& operator+=(const Duration&); 00148 private: 00149 time_t time_; // nr of secs since Jan 1, 1970 00150 }; 00151 }} 00152 00153 #endif
dvutil-0.13.15 | [30 December, 2004] |