3 This file is part of libmspcore
4 Copyright © 2006 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
11 #include "../core/except.h"
13 #include "timestamp.h"
20 inline bool is_leap_year(int y)
21 { return y%4==0 && (y%100 || y%400==0); }
23 inline unsigned char month_days(int y, unsigned char m)
33 return is_leap_year(y)?29:28;
40 inline int cmp_(T a, T b)
54 DateTime::DateTime(const TimeStamp &ts)
59 DateTime::DateTime(const TimeStamp &ts, const TimeZone &tz)
65 DateTime::DateTime(int y, unsigned char m, unsigned char d)
67 init(y, m, d, 0, 0, 0, 0);
70 DateTime::DateTime(int y, unsigned char m, unsigned char d, unsigned char h, unsigned char n, unsigned char s)
72 init(y, m, d, h, n, s, 0);
75 DateTime::DateTime(int y, unsigned char m, unsigned char d, unsigned char h, unsigned char n, unsigned char s, unsigned u)
77 init(y, m, d, h, n, s, u);
80 void DateTime::init(const TimeStamp &ts)
92 void DateTime::init(int y, unsigned char m, unsigned char d, unsigned char h, unsigned char n, unsigned char s, unsigned u)
103 throw InvalidParameterValue("Microseconds out of range");
105 throw InvalidParameterValue("Seconds out of range");
107 throw InvalidParameterValue("Minutes out of range");
109 throw InvalidParameterValue("Hours out of range");
110 if(month<1 || month>12)
111 throw InvalidParameterValue("Month out of range");
112 if(mday<1 || mday>month_days(year, month))
113 throw InvalidParameterValue("Day of month out of range");
116 void DateTime::add_days(int days)
120 /* Leap years have a 400 year cycle, so any 400 consecutive years have a
121 constant number of days (400*365+97 = 146097) */
122 new_year += days/146097*400;
131 // Fudge factor for leap day
132 int fudge = (month<=2)?1:0;
134 // (Almost) every 4 year cycle has 1 leap year and 3 normal years
135 unsigned cycles = days/1461;
138 new_year += cycles*4;
140 // See how many non-leap-years we counted as leap years and reclaim the lost days
141 // XXX This breaks with negative years
142 unsigned missed_leap_days = ((year-fudge)%100+cycles*4)/100;
143 if((year-fudge)%400+cycles*4>=400)
146 days += missed_leap_days;
148 // Count single years from the 4 year cycle
154 if((year-fudge)%4+cycles>=4)
156 // We passed a leap year - decrement days
159 days = is_leap_year(new_year-fudge)?365:364;
169 while(mday+days>month_days(year, month))
171 days -= month_days(year, month);
183 void DateTime::set_timezone(const TimeZone &tz)
188 void DateTime::convert_timezone(const TimeZone &tz)
190 add_raw((zone.get_offset()-tz.get_offset()).raw());
194 DateTime DateTime::operator+(const TimeDelta &td) const
197 dt.add_raw(td.raw());
201 DateTime &DateTime::operator+=(const TimeDelta &td)
207 DateTime DateTime::operator-(const TimeDelta &td) const
210 dt.add_raw(-td.raw());
214 DateTime &DateTime::operator-=(const TimeDelta &td)
220 int DateTime::cmp(const DateTime &dt) const
222 if(int c = cmp_(year, dt.year))
224 if(int c = cmp_(month, dt.month))
226 if(int c = cmp_(mday, dt.mday))
228 if(int c = cmp_(hour, dt.hour))
230 if(int c = cmp_(minute, dt.minute))
232 if(int c = cmp_(second, dt.second))
234 if(int c = cmp_(usec, dt.usec))
239 TimeStamp DateTime::get_timestamp() const
241 if(year<-289701 || year>293641)
242 throw InvalidState("DateTime is not representable as a TimeStamp");
244 RawTime raw = (((hour*60LL)+minute)*60+second)*1000000+usec;
245 int days = (year-1970)*365;
246 days += (year-1)/4-(year-1)/100+(year-1)/400-477;
247 for(unsigned i=1; i<month; ++i)
248 days += month_days(year, i);
251 raw += days*86400000000LL;
253 return TimeStamp(raw);
256 string DateTime::format(const string &fmt) const
260 for(string::const_iterator i=fmt.begin(); i!=fmt.end(); ++i)
268 ss<<setw(2)<<int(mday);
270 ss<<setw(2)<<int(hour);
272 ss<<setw(2)<<hour%12;
274 ss<<setw(2)<<int(month);
276 ss<<setw(2)<<int(minute);
278 ss<<((hour>=12) ? "PM" : "AM");
280 ss<<setw(2)<<int(second);
282 ss<<setw(2)<<year%100;
284 ss<<setw(4)<<internal<<year;
295 string DateTime::format_rfc3339() const
297 string result = format("%Y-%m-%dT%H:%M:%S");
298 if(const TimeDelta &offs = zone.get_offset())
302 int m = abs(static_cast<int>(offs/Time::min));
303 ss<<(offs<zero ? '+' : '-')<<setw(2)<<m/60<<':'<<setw(2)<<m%60;
311 void DateTime::add_raw(RawTime raw)
313 int days = static_cast<int>(raw/86400000000LL);
314 raw %= 86400000000LL;
318 raw += 86400000000LL;
321 usec+=raw%1000000; raw /= 1000000;
322 second+=raw%60; raw /= 60;
323 minute+=raw%60; raw /= 60;
324 hour+=raw%24; raw /= 24;
330 void DateTime::normalize()
332 second += usec/1000000;
340 while(mday>month_days(year, month))
342 mday -= month_days(year, month);