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::add_days(int days)
84 /* Leap years have a 400 year cycle, so any 400 consecutive years have a
85 constant number of days (400*365+97=146097) */
86 new_year+=days/146097*400;
95 // Fudge factor for leap day
96 int fudge=(month<=2)?1:0;
98 // (Almost) every 4 year cycle has 1 leap year and 3 normal years
99 unsigned cycles=days/1461;
104 // See how many non-leap-years we counted as leap years and reclaim the lost days
105 // XXX This breaks with negative years
106 unsigned missed_leap_days=((year-fudge)%100+cycles*4)/100;
107 if((year-fudge)%400+cycles*4>=400)
110 days+=missed_leap_days;
112 // Count single years from the 4 year cycle
118 if((year-fudge)%4+cycles>=4)
120 // We passed a leap year - decrement days
123 days=is_leap_year(new_year-fudge)?365:364;
133 while(mday+days>month_days(year, month))
135 days-=month_days(year, month);
147 void DateTime::set_timezone(const TimeZone &tz)
152 void DateTime::convert_timezone(const TimeZone &tz)
154 add_raw((zone.get_offset()-tz.get_offset()).raw());
158 DateTime DateTime::operator+(const TimeDelta &td) const
161 dt.add_raw(td.raw());
165 DateTime &DateTime::operator+=(const TimeDelta &td)
171 DateTime DateTime::operator-(const TimeDelta &td) const
174 dt.add_raw(-td.raw());
178 DateTime &DateTime::operator-=(const TimeDelta &td)
184 int DateTime::cmp(const DateTime &dt) const
186 if(int c=cmp_(year, dt.year))
188 if(int c=cmp_(month, dt.month))
190 if(int c=cmp_(mday, dt.mday))
192 if(int c=cmp_(hour, dt.hour))
194 if(int c=cmp_(minute, dt.minute))
196 if(int c=cmp_(second, dt.second))
198 if(int c=cmp_(usec, dt.usec))
203 TimeStamp DateTime::get_timestamp() const
205 if(year<-289701 || year>293641)
206 throw Exception("DateTime is not representable as a TimeStamp");
208 RawTime raw=(((hour*60LL)+minute)*60+second)*1000000+usec;
209 int days=(year-1970)*365;
210 days+=(year-1)/4-(year-1)/100+(year-1)/400-477;
211 for(unsigned i=1; i<month; ++i)
212 days+=month_days(year, i);
215 raw+=days*86400000000LL;
217 return TimeStamp(raw);
220 string DateTime::format(const string &fmt) const
224 for(string::const_iterator i=fmt.begin(); i!=fmt.end(); ++i)
232 ss<<setw(2)<<int(mday);
234 ss<<setw(2)<<int(hour);
236 ss<<setw(2)<<hour%12;
238 ss<<setw(2)<<int(month);
240 ss<<setw(2)<<int(minute);
242 ss<<((hour>=12) ? "PM" : "AM");
244 ss<<setw(2)<<int(second);
246 ss<<setw(2)<<year%100;
248 ss<<setw(4)<<internal<<year;
259 string DateTime::format_rfc3339() const
261 string result=format("%Y-%m-%dT%H:%M:%S");
262 if(const TimeDelta &offs=zone.get_offset())
266 int m=abs(static_cast<int>(offs/Time::min));
267 ss<<(offs<zero ? '+' : '-')<<setw(2)<<m/60<<':'<<setw(2)<<m%60;
275 void DateTime::init(const TimeStamp &ts)
287 void DateTime::init(int y, unsigned char m, unsigned char d, unsigned char h, unsigned char n, unsigned char s, unsigned u)
299 void DateTime::add_raw(RawTime raw)
301 int days=static_cast<int>(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);
340 void DateTime::validate() const
343 throw InvalidParameterValue("Microseconds out of range");
345 throw InvalidParameterValue("Seconds out of range");
347 throw InvalidParameterValue("Minutes out of range");
349 throw InvalidParameterValue("Hours out of range");
350 if(month<1 || month>12)
351 throw InvalidParameterValue("Month out of range");
352 if(mday<1 || mday>month_days(year, month))
353 throw InvalidParameterValue("Day of month out of range");