00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef _ptime_h_
00021 #define _ptime_h_
00022
00023 #include <pclasses/pexport.h>
00024 #include <pclasses/pexception.h>
00025 #include <string>
00026 #include <iostream>
00027 #include <time.h>
00028
00029 namespace P {
00030
00032
00037 class PCORE_EXPORT TimeSpan {
00038 public:
00039 TimeSpan(unsigned int days = 0, unsigned int hours = 0, unsigned int minutes = 0,
00040 unsigned int seconds = 0, unsigned int usecs = 0) throw(OverflowError);
00041 ~TimeSpan() throw();
00042
00043 void setDays(unsigned int days) throw(OverflowError);
00044 unsigned int days() const throw();
00045
00046 void setHours(unsigned int hours) throw(OverflowError);
00047 unsigned int hours() const throw();
00048
00049 void setMinutes(unsigned int minutes) throw(OverflowError);
00050 unsigned int minutes() const throw();
00051
00052 void setSeconds(unsigned int seconds) throw(OverflowError);
00053 unsigned int seconds() const throw();
00054
00055 void setUsecs(unsigned int usec) throw(OverflowError);
00056 unsigned int usecs() const throw();
00057
00059 void normalize() throw(OverflowError);
00060
00061 bool operator>(const TimeSpan& sp) const throw();
00062 bool operator<(const TimeSpan& sp) const throw();
00063 bool operator>=(const TimeSpan& sp) const throw();
00064 bool operator<=(const TimeSpan& sp) const throw();
00065 bool operator==(const TimeSpan& sp) const throw();
00066 bool operator!=(const TimeSpan& sp) const throw();
00067
00068 TimeSpan& operator+=(const TimeSpan& sp) throw(OverflowError);
00069 TimeSpan& operator-=(const TimeSpan& sp) throw();
00070
00071 PCORE_EXPORT friend TimeSpan operator+(const TimeSpan& sp1, const TimeSpan& sp2) throw(OverflowError);
00072 PCORE_EXPORT friend TimeSpan operator-(const TimeSpan& sp1, const TimeSpan& sp2) throw();
00073
00074 private:
00075 unsigned int m_days;
00076 unsigned int m_hours;
00077 unsigned int m_minutes;
00078 unsigned int m_seconds;
00079 unsigned int m_usecs;
00080 };
00081
00082
00084
00087 class PCORE_EXPORT InvalidDate: public RuntimeError {
00088 public:
00089 InvalidDate(const char* _what, const SourceInfo& si)
00090 : RuntimeError(_what, si) {}
00091 };
00092
00093
00095
00098 class PCORE_EXPORT Date {
00099 public:
00101 enum weekDay_t {
00102 Saturday = 0,
00103 Sunday,
00104 Monday,
00105 Tuesday,
00106 Wednesday,
00107 Thursday,
00108 Firday
00109 };
00110
00112 enum month_t {
00113 January = 1,
00114 February,
00115 March,
00116 April,
00117 May,
00118 June,
00119 July,
00120 August,
00121 September,
00122 October,
00123 November,
00124 December
00125 };
00126
00127 Date(unsigned short year = 1970, unsigned char month = 1, unsigned char day = 1)
00128 throw(InvalidDate);
00129
00130 ~Date() throw();
00131
00132 double julianDayNumber() const throw();
00133
00134 void setYear(unsigned short year);
00135 unsigned short year() const throw();
00136
00137 void setMonth(unsigned char month) throw(InvalidDate);
00138 unsigned char month() const throw();
00139
00141 unsigned char daysInMonth() throw();
00142
00143 void setDay(unsigned char day) throw(InvalidDate);
00144 unsigned char day() const throw();
00145
00146 unsigned short dayOfYear() const throw();
00147
00148 unsigned char dayOfWeek() const throw();
00149
00151 bool isInLeapYear() const throw();
00152
00153 Date& operator+=(const TimeSpan& sp);
00154 Date& operator-=(const TimeSpan& sp);
00155
00156 bool operator>(const Date& d) const throw();
00157 bool operator<(const Date& d) const throw();
00158 bool operator>=(const Date& d) const throw();
00159 bool operator<=(const Date& d) const throw();
00160 bool operator==(const Date& d) const throw();
00161 bool operator!=(const Date& d) const throw();
00162
00164 static unsigned char daysInMonth(unsigned char month, unsigned short year) throw(InvalidDate);
00165
00167 static bool isLeapYear(unsigned short year) throw();
00168
00169 PCORE_EXPORT friend std::ostream& operator << (std::ostream& os, const Date& d);
00170
00171 private:
00172 unsigned short m_year;
00173 unsigned char m_month;
00174 unsigned char m_day;
00175 };
00176
00177
00179
00182 class PCORE_EXPORT InvalidTime: public RuntimeError {
00183 public:
00184 InvalidTime(const char* _what, const SourceInfo& si)
00185 : RuntimeError(_what, si) {}
00186 };
00187
00188
00190
00193 class PCORE_EXPORT Time {
00194 public:
00195 Time(unsigned char hour = 0, unsigned char minute = 0,
00196 unsigned char second = 0, unsigned int usec = 0) throw(InvalidTime);
00197 ~Time() throw();
00198
00199 void setHour(unsigned char hour);
00200 unsigned char hour() const throw();
00201
00202 void setMinute(unsigned char minute);
00203 unsigned char minute() const throw();
00204
00205 void setSecond(unsigned char second);
00206 unsigned char second() const throw();
00207
00208 void setUsec(unsigned int usec);
00209 unsigned int usec() const throw();
00210
00211 Time& operator+=(const TimeSpan& sp);
00212 Time& operator-=(const TimeSpan& sp);
00213
00214 bool operator>(const Time& t) const throw();
00215 bool operator<(const Time& t) const throw();
00216 bool operator>=(const Time& t) const throw();
00217 bool operator<=(const Time& t) const throw();
00218 bool operator==(const Time& t) const throw();
00219 bool operator!=(const Time& t) const throw();
00220
00221 PCORE_EXPORT friend std::ostream& operator << (std::ostream& os, const Time& t);
00222
00223 private:
00224 unsigned char m_hour;
00225 unsigned char m_minute;
00226 unsigned char m_second;
00227 unsigned int m_usec;
00228 };
00229
00230
00232
00235 class PCORE_EXPORT DateTime:
00236 public Date,
00237 public Time
00238 {
00239 public:
00240 DateTime();
00241 DateTime(const Date& d);
00242 DateTime(const Time& t);
00243 DateTime(const Date& d, const Time& t);
00244 ~DateTime();
00245
00247
00251 DateTime toLocalTime() const;
00252
00254 DateTime toTimeZone(const std::string& tzname) const;
00255
00257 inline const std::string& timeZone() const throw()
00258 { return m_tzname; }
00259
00260 DateTime& operator+=(const TimeSpan& sp);
00261 DateTime& operator-=(const TimeSpan& sp);
00262
00263 bool operator>(const DateTime& dt) const throw();
00264 bool operator<(const DateTime& dt) const throw();
00265 bool operator>=(const DateTime& dt) const throw();
00266 bool operator<=(const DateTime& dt) const throw();
00267 bool operator==(const DateTime& dt) const throw();
00268 bool operator!=(const DateTime& dt) const throw();
00269
00271 enum currentMode_t {
00272 Local,
00273 UTC
00274 };
00275
00277 static DateTime current(currentMode_t mode = Local);
00278
00280 static std::string currentTimeZone();
00281
00282 PCORE_EXPORT friend std::ostream& operator << (std::ostream& os, const DateTime& dt);
00283
00284 private:
00285 DateTime(const Date& d, const Time& t, const std::string& tzname);
00286 std::string m_tzname;
00287 };
00288
00289 }
00290
00291 #endif