4 #include "../core/except.h"
12 inline bool is_leap_year(int32_t y)
13 { return y%4==0 && (y%100 || y%400==0); }
15 inline uint8_t month_days(int32_t y, uint8_t m)
25 return is_leap_year(y)?29:28;
32 inline int cmp_(T a, T b)
46 DateTime::DateTime(const TimeStamp &ts):
58 DateTime::DateTime(int32_t y, uint8_t m, uint8_t d):
70 DateTime::DateTime(int32_t y, uint8_t m, uint8_t d, uint8_t h, uint8_t n, uint8_t s):
82 DateTime::DateTime(int32_t y, uint8_t m, uint8_t d, uint8_t h, uint8_t n, uint8_t s, uint32_t u):
94 void DateTime::add_days(int32_t days)
96 unsigned new_year=year;
98 /* Leap years have a 400 year cycle, so any 400 consecutive years have a
99 constant number of days (400*365+97=146097) */
100 new_year+=days/146097*400;
109 // Fudge factor for leap day
110 int fudge=(month<=2)?1:0;
112 // (Almost) every 4 year cycle has 1 leap year and 3 normal years
113 unsigned cycles=days/1461;
118 // See how many non-leap-years we counted as leap years and reclaim the lost days
119 unsigned missed_leap_days=((year-fudge)%100+cycles*4)/100;
120 if((year-fudge)%400+cycles*4>=400)
123 days+=missed_leap_days;
125 // Count single years from the 4 year cycle
131 if((year-fudge)%4+cycles>=4)
133 // We passed a leap year - decrement days
137 days=is_leap_year(new_year)?365:364;
146 while(mday+days>month_days(year, month))
148 days-=month_days(year, month);
160 DateTime DateTime::operator+(const TimeDelta &td) const
163 dt.add_raw(td.raw());
167 DateTime &DateTime::operator+=(const TimeDelta &td)
173 int DateTime::cmp(const DateTime &dt) const
175 if(int c=cmp_(year, dt.year))
177 if(int c=cmp_(month, dt.month))
179 if(int c=cmp_(mday, dt.mday))
181 if(int c=cmp_(hour, dt.hour))
183 if(int c=cmp_(minute, dt.minute))
185 if(int c=cmp_(second, dt.second))
187 if(int c=cmp_(usec, dt.usec))
192 TimeStamp DateTime::get_timestamp() const
194 if(year<-289701 || year>293641)
195 throw Exception("DateTime is not representable as a TimeStamp");
197 int64_t raw=(((hour*60LL)+minute)*60+second)*1000000+usec;
198 int days=(year-1970)*365;
199 days+=(year-1)/4-(year-1)/100+(year-1)/400-477;
200 for(unsigned i=1; i<month; ++i)
201 days+=month_days(year, i);
204 raw+=days*86400000000LL;
206 return TimeStamp(raw);
209 string DateTime::format(const string &fmt) const
213 for(string::const_iterator i=fmt.begin(); i!=fmt.end(); ++i)
221 ss<<setw(2)<<int(mday);
223 ss<<setw(2)<<int(hour);
225 ss<<setw(2)<<hour%12;
227 ss<<setw(2)<<int(month);
229 ss<<setw(2)<<int(minute);
231 ss<<((hour>=12) ? "PM" : "AM");
233 ss<<setw(2)<<int(second);
235 ss<<setw(2)<<year%100;
248 void DateTime::add_raw(int64_t raw)
250 int32_t days=raw/86400000000LL;
258 usec+=raw%1000000; raw/=1000000;
259 second+=raw%60; raw/=60;
260 minute+=raw%60; raw/=60;
261 hour+=raw%24; raw/=24;
267 void DateTime::normalize()
269 second+=usec/1000000;
277 while(mday>month_days(year, month))
279 mday-=month_days(year, month);
289 void DateTime::validate() const
292 throw InvalidParameterValue("Microseconds out of range");
294 throw InvalidParameterValue("Seconds out of range");
296 throw InvalidParameterValue("Minutes out of range");
298 throw InvalidParameterValue("Hours out of range");
299 if(month<1 || month>12)
300 throw InvalidParameterValue("Month out of range");
301 if(mday<1 || mday>month_days(year, month))
302 throw InvalidParameterValue("Day of month out of range");