3 #include <msp/strings/format.h>
4 #include <msp/strings/regex.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):
58 DateTime(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):
62 DateTime(y, m, d, h, n, s, 0)
65 DateTime::DateTime(int y, unsigned char m, unsigned char d, unsigned char h, unsigned char n, unsigned char s, unsigned u):
75 throw out_of_range("DateTime::DateTime usec");
77 throw out_of_range("DateTime::DateTime second");
79 throw out_of_range("DateTime::DateTime minute");
81 throw out_of_range("DateTime::DateTime hour");
82 if(month<1 || month>12)
83 throw out_of_range("DateTime::DateTime month");
84 if(mday<1 || mday>month_days(year, month))
85 throw out_of_range("DateTime::DateTime mday");
88 DateTime DateTime::parse_rfc3339(const string &str)
90 static Regex re("^([0-9]{4})-([0-9]{2})-([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})(.([0-9]+))?(Z|[-+]([0-9]{2}):([0-9]{2}))$");
92 RegMatch m = re.match(str);
94 throw invalid_argument("DateTime::parse_rfc3339");
96 unsigned year = lexical_cast<unsigned>(m[1].str);
97 unsigned month = lexical_cast<unsigned>(m[2].str);
98 unsigned mday = lexical_cast<unsigned>(m[3].str);
99 unsigned hr = lexical_cast<unsigned>(m[4].str);
100 unsigned minute = lexical_cast<unsigned>(m[5].str);
101 unsigned second = lexical_cast<unsigned>(m[6].str);
103 const string &sec_frac = m[8].str;
104 if(!sec_frac.empty())
106 us = lexical_cast<unsigned>(sec_frac);
107 for(unsigned i=sec_frac.size(); i<6; ++i)
109 for(unsigned i=sec_frac.size(); i>6; --i)
113 DateTime result = DateTime(year, month, mday, hr, minute, second, us);
118 tzoff = lexical_cast<unsigned>(m[10].str)*60+lexical_cast<unsigned>(m[11].str);
123 result.set_timezone(tzoff);
128 void DateTime::add_days(int days)
132 /* Leap years have a 400 year cycle, so any 400 consecutive years have a
133 constant number of days (400*365+97 = 146097) */
134 new_year += days/146097*400;
143 // Fudge factor for leap day
144 int fudge = (month<=2) ? 1 : 0;
146 // (Almost) every 4 year cycle has 1 leap year and 3 normal years
147 unsigned cycles = days/1461;
150 new_year += cycles*4;
152 // See how many non-leap-years we counted as leap years and reclaim the lost days
153 // XXX This breaks with negative years
154 unsigned missed_leap_days = ((year-fudge)%100+cycles*4)/100;
155 if((year-fudge)%400+cycles*4>=400)
158 days += missed_leap_days;
160 // Count single years from the 4 year cycle
166 if((year-fudge)%4+cycles>=4 && (new_year%100>=4 || new_year%400<4))
168 // We passed a leap year - decrement days
171 days = is_leap_year(new_year-fudge) ? 365 : 364;
181 while(mday+days>month_days(year, month))
183 days -= month_days(year, month);
195 void DateTime::set_timezone(const TimeZone &tz)
200 void DateTime::convert_timezone(const TimeZone &tz)
202 add_raw((tz.get_offset()-zone.get_offset()).raw());
206 DateTime DateTime::operator+(const TimeDelta &td) const
209 dt.add_raw(td.raw());
213 DateTime &DateTime::operator+=(const TimeDelta &td)
219 DateTime DateTime::operator-(const TimeDelta &td) const
222 dt.add_raw(-td.raw());
226 DateTime &DateTime::operator-=(const TimeDelta &td)
232 int DateTime::cmp(const DateTime &dt) const
234 if(int c = _cmp(year, dt.year))
236 if(int c = _cmp(month, dt.month))
238 if(int c = _cmp(mday, dt.mday))
240 if(int c = _cmp(hour, dt.hour))
242 if(int c = _cmp(minute, dt.minute))
244 if(int c = _cmp(second, dt.second))
246 if(int c = _cmp(usec, dt.usec))
251 TimeStamp DateTime::get_timestamp() const
253 if(year<-289701 || year>293641)
254 throw range_error("DateTime::get_timestamp");
256 RawTime raw = (((hour*60LL)+minute)*60+second)*1000000+usec;
257 int days = (year-1970)*365;
258 days += (year-1)/4-(year-1)/100+(year-1)/400-477;
259 for(unsigned i=1; i<month; ++i)
260 days += month_days(year, i);
263 raw += days*86400000000LL;
265 return TimeStamp(raw);
268 string DateTime::format(const string &fmt) const
271 for(auto i=fmt.begin(); i!=fmt.end(); ++i)
279 result += Msp::format("%02d", mday);
281 result += Msp::format("%02d", hour);
283 result += Msp::format("%02d", hour%12);
285 result += Msp::format("%02d", month);
287 result += Msp::format("%02d", minute);
289 result += ((hour>=12) ? "PM" : "AM");
291 result += Msp::format("%02d", second);
293 result += Msp::format("%02d", year%100);
295 result += Msp::format("%04d", year);
306 string DateTime::format_rfc3339() const
308 string result = format("%Y-%m-%dT%H:%M:%S");
310 result += Msp::format(".%06d", usec);
311 if(const TimeDelta &offs = zone.get_offset())
313 int m = abs(static_cast<int>(offs/Time::min));
314 result += Msp::format("%c%02d:%02d", (offs<zero ? '-' : '+'), m/60, m%60);
321 void DateTime::add_raw(RawTime raw)
323 int days = static_cast<int>(raw/86400000000LL);
324 raw %= 86400000000LL;
328 raw += 86400000000LL;
331 usec += raw%1000000; raw /= 1000000;
332 second += raw%60; raw /= 60;
333 minute += raw%60; raw /= 60;
334 hour += raw%24; raw /= 24;
340 void DateTime::normalize()
342 second += usec/1000000;
350 while(mday>month_days(year, month))
352 mday -= month_days(year, month);