3 This file is part of libmspcore
4 Copyright © 2006 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
10 #include <msp/strings/format.h>
12 #include "timestamp.h"
19 inline bool is_leap_year(int y)
20 { return y%4==0 && (y%100 || y%400==0); }
22 inline unsigned char month_days(int y, unsigned char m)
32 return is_leap_year(y)?29:28;
39 inline int cmp_(T a, T b)
53 DateTime::DateTime(const TimeStamp &ts)
58 DateTime::DateTime(const TimeStamp &ts, const TimeZone &tz)
64 DateTime::DateTime(int y, unsigned char m, unsigned char d)
66 init(y, m, d, 0, 0, 0, 0);
69 DateTime::DateTime(int y, unsigned char m, unsigned char d, unsigned char h, unsigned char n, unsigned char s)
71 init(y, m, d, h, n, s, 0);
74 DateTime::DateTime(int y, unsigned char m, unsigned char d, unsigned char h, unsigned char n, unsigned char s, unsigned u)
76 init(y, m, d, h, n, s, u);
79 void DateTime::init(const TimeStamp &ts)
91 void DateTime::init(int y, unsigned char m, unsigned char d, unsigned char h, unsigned char n, unsigned char s, unsigned u)
102 throw out_of_range("DateTime::DateTime usec");
104 throw out_of_range("DateTime::DateTime second");
106 throw out_of_range("DateTime::DateTime minute");
108 throw out_of_range("DateTime::DateTime hour");
109 if(month<1 || month>12)
110 throw out_of_range("DateTime::DateTime month");
111 if(mday<1 || mday>month_days(year, month))
112 throw out_of_range("DateTime::DateTime mday");
115 void DateTime::add_days(int days)
119 /* Leap years have a 400 year cycle, so any 400 consecutive years have a
120 constant number of days (400*365+97 = 146097) */
121 new_year += days/146097*400;
130 // Fudge factor for leap day
131 int fudge = (month<=2)?1:0;
133 // (Almost) every 4 year cycle has 1 leap year and 3 normal years
134 unsigned cycles = days/1461;
137 new_year += cycles*4;
139 // See how many non-leap-years we counted as leap years and reclaim the lost days
140 // XXX This breaks with negative years
141 unsigned missed_leap_days = ((year-fudge)%100+cycles*4)/100;
142 if((year-fudge)%400+cycles*4>=400)
145 days += missed_leap_days;
147 // Count single years from the 4 year cycle
153 if((year-fudge)%4+cycles>=4 && (new_year%100>=4 || new_year%400<4))
155 // We passed a leap year - decrement days
158 days = is_leap_year(new_year-fudge)?365:364;
168 while(mday+days>month_days(year, month))
170 days -= month_days(year, month);
182 void DateTime::set_timezone(const TimeZone &tz)
187 void DateTime::convert_timezone(const TimeZone &tz)
189 add_raw((tz.get_offset()-zone.get_offset()).raw());
193 DateTime DateTime::operator+(const TimeDelta &td) const
196 dt.add_raw(td.raw());
200 DateTime &DateTime::operator+=(const TimeDelta &td)
206 DateTime DateTime::operator-(const TimeDelta &td) const
209 dt.add_raw(-td.raw());
213 DateTime &DateTime::operator-=(const TimeDelta &td)
219 int DateTime::cmp(const DateTime &dt) const
221 if(int c = cmp_(year, dt.year))
223 if(int c = cmp_(month, dt.month))
225 if(int c = cmp_(mday, dt.mday))
227 if(int c = cmp_(hour, dt.hour))
229 if(int c = cmp_(minute, dt.minute))
231 if(int c = cmp_(second, dt.second))
233 if(int c = cmp_(usec, dt.usec))
238 TimeStamp DateTime::get_timestamp() const
240 if(year<-289701 || year>293641)
241 throw range_error("DateTime::get_timestamp");
243 RawTime raw = (((hour*60LL)+minute)*60+second)*1000000+usec;
244 int days = (year-1970)*365;
245 days += (year-1)/4-(year-1)/100+(year-1)/400-477;
246 for(unsigned i=1; i<month; ++i)
247 days += month_days(year, i);
250 raw += days*86400000000LL;
252 return TimeStamp(raw);
255 string DateTime::format(const string &fmt) const
258 for(string::const_iterator i=fmt.begin(); i!=fmt.end(); ++i)
266 result += Msp::format("%02d", mday);
268 result += Msp::format("%02d", hour);
270 result += Msp::format("%02d", hour%12);
272 result += Msp::format("%02d", month);
274 result += Msp::format("%02d", minute);
276 result += ((hour>=12) ? "PM" : "AM");
278 result += Msp::format("%02d", second);
280 result += Msp::format("%02d", year%100);
282 result += Msp::format("%04d", year);
293 string DateTime::format_rfc3339() const
295 string result = format("%Y-%m-%dT%H:%M:%S");
296 if(const TimeDelta &offs = zone.get_offset())
298 int m = abs(static_cast<int>(offs/Time::min));
299 result += Msp::format("%c%02d:%02d", (offs<zero ? '-' : '+'), m/60, m%60);
306 void DateTime::add_raw(RawTime raw)
308 int days = static_cast<int>(raw/86400000000LL);
309 raw %= 86400000000LL;
313 raw += 86400000000LL;
316 usec+=raw%1000000; raw /= 1000000;
317 second+=raw%60; raw /= 60;
318 minute+=raw%60; raw /= 60;
319 hour+=raw%24; raw /= 24;
325 void DateTime::normalize()
327 second += usec/1000000;
335 while(mday>month_days(year, month))
337 mday -= month_days(year, month);