3 #include <msp/strings/format.h>
11 inline bool is_leap_year(int y)
12 { return y%4==0 && (y%100 || y%400==0); }
14 inline unsigned char month_days(int y, unsigned char m)
24 return is_leap_year(y)?29:28;
31 inline int cmp_(T a, T b)
45 DateTime::DateTime(const TimeStamp &ts)
50 DateTime::DateTime(const TimeStamp &ts, const TimeZone &tz)
56 DateTime::DateTime(int y, unsigned char m, unsigned char d)
58 init(y, m, d, 0, 0, 0, 0);
61 DateTime::DateTime(int y, unsigned char m, unsigned char d, unsigned char h, unsigned char n, unsigned char s)
63 init(y, m, d, h, n, s, 0);
66 DateTime::DateTime(int y, unsigned char m, unsigned char d, unsigned char h, unsigned char n, unsigned char s, unsigned u)
68 init(y, m, d, h, n, s, u);
71 void DateTime::init(const TimeStamp &ts)
83 void DateTime::init(int y, unsigned char m, unsigned char d, unsigned char h, unsigned char n, unsigned char s, unsigned u)
94 throw out_of_range("DateTime::DateTime usec");
96 throw out_of_range("DateTime::DateTime second");
98 throw out_of_range("DateTime::DateTime minute");
100 throw out_of_range("DateTime::DateTime hour");
101 if(month<1 || month>12)
102 throw out_of_range("DateTime::DateTime month");
103 if(mday<1 || mday>month_days(year, month))
104 throw out_of_range("DateTime::DateTime mday");
107 void DateTime::add_days(int days)
111 /* Leap years have a 400 year cycle, so any 400 consecutive years have a
112 constant number of days (400*365+97 = 146097) */
113 new_year += days/146097*400;
122 // Fudge factor for leap day
123 int fudge = (month<=2) ? 1 : 0;
125 // (Almost) every 4 year cycle has 1 leap year and 3 normal years
126 unsigned cycles = days/1461;
129 new_year += cycles*4;
131 // See how many non-leap-years we counted as leap years and reclaim the lost days
132 // XXX This breaks with negative years
133 unsigned missed_leap_days = ((year-fudge)%100+cycles*4)/100;
134 if((year-fudge)%400+cycles*4>=400)
137 days += missed_leap_days;
139 // Count single years from the 4 year cycle
145 if((year-fudge)%4+cycles>=4 && (new_year%100>=4 || new_year%400<4))
147 // We passed a leap year - decrement days
150 days = is_leap_year(new_year-fudge) ? 365 : 364;
160 while(mday+days>month_days(year, month))
162 days -= month_days(year, month);
174 void DateTime::set_timezone(const TimeZone &tz)
179 void DateTime::convert_timezone(const TimeZone &tz)
181 add_raw((tz.get_offset()-zone.get_offset()).raw());
185 DateTime DateTime::operator+(const TimeDelta &td) const
188 dt.add_raw(td.raw());
192 DateTime &DateTime::operator+=(const TimeDelta &td)
198 DateTime DateTime::operator-(const TimeDelta &td) const
201 dt.add_raw(-td.raw());
205 DateTime &DateTime::operator-=(const TimeDelta &td)
211 int DateTime::cmp(const DateTime &dt) const
213 if(int c = cmp_(year, dt.year))
215 if(int c = cmp_(month, dt.month))
217 if(int c = cmp_(mday, dt.mday))
219 if(int c = cmp_(hour, dt.hour))
221 if(int c = cmp_(minute, dt.minute))
223 if(int c = cmp_(second, dt.second))
225 if(int c = cmp_(usec, dt.usec))
230 TimeStamp DateTime::get_timestamp() const
232 if(year<-289701 || year>293641)
233 throw range_error("DateTime::get_timestamp");
235 RawTime raw = (((hour*60LL)+minute)*60+second)*1000000+usec;
236 int days = (year-1970)*365;
237 days += (year-1)/4-(year-1)/100+(year-1)/400-477;
238 for(unsigned i=1; i<month; ++i)
239 days += month_days(year, i);
242 raw += days*86400000000LL;
244 return TimeStamp(raw);
247 string DateTime::format(const string &fmt) const
250 for(string::const_iterator i=fmt.begin(); i!=fmt.end(); ++i)
258 result += Msp::format("%02d", mday);
260 result += Msp::format("%02d", hour);
262 result += Msp::format("%02d", hour%12);
264 result += Msp::format("%02d", month);
266 result += Msp::format("%02d", minute);
268 result += ((hour>=12) ? "PM" : "AM");
270 result += Msp::format("%02d", second);
272 result += Msp::format("%02d", year%100);
274 result += Msp::format("%04d", year);
285 string DateTime::format_rfc3339() const
287 string result = format("%Y-%m-%dT%H:%M:%S");
288 if(const TimeDelta &offs = zone.get_offset())
290 int m = abs(static_cast<int>(offs/Time::min));
291 result += Msp::format("%c%02d:%02d", (offs<zero ? '-' : '+'), m/60, m%60);
298 void DateTime::add_raw(RawTime raw)
300 int days = static_cast<int>(raw/86400000000LL);
301 raw %= 86400000000LL;
305 raw += 86400000000LL;
308 usec += raw%1000000; raw /= 1000000;
309 second += raw%60; raw /= 60;
310 minute += raw%60; raw /= 60;
311 hour += raw%24; raw /= 24;
317 void DateTime::normalize()
319 second += usec/1000000;
327 while(mday>month_days(year, month))
329 mday -= month_days(year, month);