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)
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 DateTime DateTime::parse_rfc3339(const string &str)
110 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}))$");
112 RegMatch m = re.match(str);
114 throw invalid_argument("DateTime::parse_rfc3339");
116 unsigned year = lexical_cast<unsigned>(m[1].str);
117 unsigned month = lexical_cast<unsigned>(m[2].str);
118 unsigned mday = lexical_cast<unsigned>(m[3].str);
119 unsigned hr = lexical_cast<unsigned>(m[4].str);
120 unsigned minute = lexical_cast<unsigned>(m[5].str);
121 unsigned second = lexical_cast<unsigned>(m[6].str);
123 const string &sec_frac = m[8].str;
124 if(!sec_frac.empty())
126 us = lexical_cast<unsigned>(sec_frac);
127 for(unsigned i=sec_frac.size(); i<6; ++i)
129 for(unsigned i=sec_frac.size(); i>6; --i)
133 DateTime result = DateTime(year, month, mday, hr, minute, second, us);
138 tzoff = lexical_cast<unsigned>(m[10].str)*60+lexical_cast<unsigned>(m[11].str);
143 result.set_timezone(tzoff);
148 void DateTime::add_days(int days)
152 /* Leap years have a 400 year cycle, so any 400 consecutive years have a
153 constant number of days (400*365+97 = 146097) */
154 new_year += days/146097*400;
163 // Fudge factor for leap day
164 int fudge = (month<=2) ? 1 : 0;
166 // (Almost) every 4 year cycle has 1 leap year and 3 normal years
167 unsigned cycles = days/1461;
170 new_year += cycles*4;
172 // See how many non-leap-years we counted as leap years and reclaim the lost days
173 // XXX This breaks with negative years
174 unsigned missed_leap_days = ((year-fudge)%100+cycles*4)/100;
175 if((year-fudge)%400+cycles*4>=400)
178 days += missed_leap_days;
180 // Count single years from the 4 year cycle
186 if((year-fudge)%4+cycles>=4 && (new_year%100>=4 || new_year%400<4))
188 // We passed a leap year - decrement days
191 days = is_leap_year(new_year-fudge) ? 365 : 364;
201 while(mday+days>month_days(year, month))
203 days -= month_days(year, month);
215 void DateTime::set_timezone(const TimeZone &tz)
220 void DateTime::convert_timezone(const TimeZone &tz)
222 add_raw((tz.get_offset()-zone.get_offset()).raw());
226 DateTime DateTime::operator+(const TimeDelta &td) const
229 dt.add_raw(td.raw());
233 DateTime &DateTime::operator+=(const TimeDelta &td)
239 DateTime DateTime::operator-(const TimeDelta &td) const
242 dt.add_raw(-td.raw());
246 DateTime &DateTime::operator-=(const TimeDelta &td)
252 int DateTime::cmp(const DateTime &dt) const
254 if(int c = cmp_(year, dt.year))
256 if(int c = cmp_(month, dt.month))
258 if(int c = cmp_(mday, dt.mday))
260 if(int c = cmp_(hour, dt.hour))
262 if(int c = cmp_(minute, dt.minute))
264 if(int c = cmp_(second, dt.second))
266 if(int c = cmp_(usec, dt.usec))
271 TimeStamp DateTime::get_timestamp() const
273 if(year<-289701 || year>293641)
274 throw range_error("DateTime::get_timestamp");
276 RawTime raw = (((hour*60LL)+minute)*60+second)*1000000+usec;
277 int days = (year-1970)*365;
278 days += (year-1)/4-(year-1)/100+(year-1)/400-477;
279 for(unsigned i=1; i<month; ++i)
280 days += month_days(year, i);
283 raw += days*86400000000LL;
285 return TimeStamp(raw);
288 string DateTime::format(const string &fmt) const
291 for(auto i=fmt.begin(); i!=fmt.end(); ++i)
299 result += Msp::format("%02d", mday);
301 result += Msp::format("%02d", hour);
303 result += Msp::format("%02d", hour%12);
305 result += Msp::format("%02d", month);
307 result += Msp::format("%02d", minute);
309 result += ((hour>=12) ? "PM" : "AM");
311 result += Msp::format("%02d", second);
313 result += Msp::format("%02d", year%100);
315 result += Msp::format("%04d", year);
326 string DateTime::format_rfc3339() const
328 string result = format("%Y-%m-%dT%H:%M:%S");
330 result += Msp::format(".%06d", usec);
331 if(const TimeDelta &offs = zone.get_offset())
333 int m = abs(static_cast<int>(offs/Time::min));
334 result += Msp::format("%c%02d:%02d", (offs<zero ? '-' : '+'), m/60, m%60);
341 void DateTime::add_raw(RawTime raw)
343 int days = static_cast<int>(raw/86400000000LL);
344 raw %= 86400000000LL;
348 raw += 86400000000LL;
351 usec += raw%1000000; raw /= 1000000;
352 second += raw%60; raw /= 60;
353 minute += raw%60; raw /= 60;
354 hour += raw%24; raw /= 24;
360 void DateTime::normalize()
362 second += usec/1000000;
370 while(mday>month_days(year, month))
372 mday -= month_days(year, month);