3 #include <msp/strings/format.h>
12 inline bool is_leap_year(int y)
13 { return y%4==0 && (y%100 || y%400==0); }
15 inline unsigned char month_days(int y, unsigned char m)
25 return is_leap_year(y)?29:28;
32 inline int cmp_(T a, T b)
46 DateTime::DateTime(const TimeStamp &ts)
51 DateTime::DateTime(const TimeStamp &ts, const TimeZone &tz)
57 DateTime::DateTime(int y, unsigned char m, unsigned char d)
59 init(y, m, d, 0, 0, 0, 0);
62 DateTime::DateTime(int y, unsigned char m, unsigned char d, unsigned char h, unsigned char n, unsigned char s)
64 init(y, m, d, h, n, s, 0);
67 DateTime::DateTime(int y, unsigned char m, unsigned char d, unsigned char h, unsigned char n, unsigned char s, unsigned u)
69 init(y, m, d, h, n, s, u);
72 void DateTime::init(const TimeStamp &ts)
84 void DateTime::init(int y, unsigned char m, unsigned char d, unsigned char h, unsigned char n, unsigned char s, unsigned u)
95 throw out_of_range("DateTime::DateTime usec");
97 throw out_of_range("DateTime::DateTime second");
99 throw out_of_range("DateTime::DateTime minute");
101 throw out_of_range("DateTime::DateTime hour");
102 if(month<1 || month>12)
103 throw out_of_range("DateTime::DateTime month");
104 if(mday<1 || mday>month_days(year, month))
105 throw out_of_range("DateTime::DateTime mday");
108 void DateTime::add_days(int days)
112 /* Leap years have a 400 year cycle, so any 400 consecutive years have a
113 constant number of days (400*365+97 = 146097) */
114 new_year += days/146097*400;
123 // Fudge factor for leap day
124 int fudge = (month<=2) ? 1 : 0;
126 // (Almost) every 4 year cycle has 1 leap year and 3 normal years
127 unsigned cycles = days/1461;
130 new_year += cycles*4;
132 // See how many non-leap-years we counted as leap years and reclaim the lost days
133 // XXX This breaks with negative years
134 unsigned missed_leap_days = ((year-fudge)%100+cycles*4)/100;
135 if((year-fudge)%400+cycles*4>=400)
138 days += missed_leap_days;
140 // Count single years from the 4 year cycle
146 if((year-fudge)%4+cycles>=4 && (new_year%100>=4 || new_year%400<4))
148 // We passed a leap year - decrement days
151 days = is_leap_year(new_year-fudge) ? 365 : 364;
161 while(mday+days>month_days(year, month))
163 days -= month_days(year, month);
175 void DateTime::set_timezone(const TimeZone &tz)
180 void DateTime::convert_timezone(const TimeZone &tz)
182 add_raw((tz.get_offset()-zone.get_offset()).raw());
186 DateTime DateTime::operator+(const TimeDelta &td) const
189 dt.add_raw(td.raw());
193 DateTime &DateTime::operator+=(const TimeDelta &td)
199 DateTime DateTime::operator-(const TimeDelta &td) const
202 dt.add_raw(-td.raw());
206 DateTime &DateTime::operator-=(const TimeDelta &td)
212 int DateTime::cmp(const DateTime &dt) const
214 if(int c = cmp_(year, dt.year))
216 if(int c = cmp_(month, dt.month))
218 if(int c = cmp_(mday, dt.mday))
220 if(int c = cmp_(hour, dt.hour))
222 if(int c = cmp_(minute, dt.minute))
224 if(int c = cmp_(second, dt.second))
226 if(int c = cmp_(usec, dt.usec))
231 TimeStamp DateTime::get_timestamp() const
233 if(year<-289701 || year>293641)
234 throw range_error("DateTime::get_timestamp");
236 RawTime raw = (((hour*60LL)+minute)*60+second)*1000000+usec;
237 int days = (year-1970)*365;
238 days += (year-1)/4-(year-1)/100+(year-1)/400-477;
239 for(unsigned i=1; i<month; ++i)
240 days += month_days(year, i);
243 raw += days*86400000000LL;
245 return TimeStamp(raw);
248 string DateTime::format(const string &fmt) const
251 for(string::const_iterator i=fmt.begin(); i!=fmt.end(); ++i)
259 result += Msp::format("%02d", mday);
261 result += Msp::format("%02d", hour);
263 result += Msp::format("%02d", hour%12);
265 result += Msp::format("%02d", month);
267 result += Msp::format("%02d", minute);
269 result += ((hour>=12) ? "PM" : "AM");
271 result += Msp::format("%02d", second);
273 result += Msp::format("%02d", year%100);
275 result += Msp::format("%04d", year);
286 string DateTime::format_rfc3339() const
288 string result = format("%Y-%m-%dT%H:%M:%S");
289 if(const TimeDelta &offs = zone.get_offset())
291 int m = abs(static_cast<int>(offs/Time::min));
292 result += Msp::format("%c%02d:%02d", (offs<zero ? '-' : '+'), m/60, m%60);
299 void DateTime::add_raw(RawTime raw)
301 int days = static_cast<int>(raw/86400000000LL);
302 raw %= 86400000000LL;
306 raw += 86400000000LL;
309 usec += raw%1000000; raw /= 1000000;
310 second += raw%60; raw /= 60;
311 minute += raw%60; raw /= 60;
312 hour += raw%24; raw /= 24;
318 void DateTime::normalize()
320 second += usec/1000000;
328 while(mday>month_days(year, month))
330 mday -= month_days(year, month);