]> git.tdb.fi Git - libs/core.git/blob - source/time/datetime.cpp
Add validation for DateTime constructor parameters
[libs/core.git] / source / time / datetime.cpp
1 /* $Id$ */
2 #include <sstream>
3 #include <iomanip>
4 #include "../core/except.h"
5 #include "datetime.h"
6 #include "timestamp.h"
7
8 using namespace std;
9
10 namespace {
11
12 inline bool is_leap_year(int32_t y)
13 { return y%4==0 && (y%100 || y%400==0); }
14
15 inline uint8_t month_days(int32_t y, uint8_t m)
16 {
17         switch(m)
18         {
19         case 4:
20         case 6:
21         case 9:
22         case 11:
23                 return 30;
24         case 2:
25                 return is_leap_year(y)?29:28;
26         default:
27                 return 31;
28         }
29 }
30
31 template<typename T>
32 inline int cmp_(T a, T b)
33 {
34         if(a<b)
35                 return -1;
36         if(a>b)
37                 return 1;
38         return 0;
39 }
40
41 }
42
43 namespace Msp {
44 namespace Time {
45
46 DateTime::DateTime(const TimeStamp &ts):
47         year(1970),
48         month(1),
49         mday(1),
50         hour(0),
51         minute(0),
52         second(0),
53         usec(0)
54 {
55         add_raw(ts.raw());
56 }
57
58 DateTime::DateTime(int32_t y, uint8_t m, uint8_t d):
59         year(y),
60         month(m),
61         mday(d),
62         hour(0),
63         minute(0),
64         second(0),
65         usec(0)
66 {
67         validate();
68 }
69
70 DateTime::DateTime(int32_t y, uint8_t m, uint8_t d, uint8_t h, uint8_t n, uint8_t s):
71         year(y),
72         month(m),
73         mday(d),
74         hour(h),
75         minute(n),
76         second(s),
77         usec(0)
78 {
79         validate();
80 }
81
82 DateTime::DateTime(int32_t y, uint8_t m, uint8_t d, uint8_t h, uint8_t n, uint8_t s, uint32_t u):
83         year(y),
84         month(m),
85         mday(d),
86         hour(h),
87         minute(n),
88         second(s),
89         usec(u)
90 {
91         validate();
92 }
93
94 void DateTime::add_days(int32_t days)
95 {
96         unsigned new_year=year;
97
98         /* Leap years have a 400 year cycle, so any 400 consecutive years have a
99         constant number of days (400*365+97=146097) */
100         new_year+=days/146097*400;
101         days%=146097;
102
103         if(days<0)
104         {
105                 new_year-=400;
106                 days+=146097;
107         }
108
109         // Fudge factor for leap day
110         int fudge=(month<=2)?1:0;
111
112         // (Almost) every 4 year cycle has 1 leap year and 3 normal years
113         unsigned cycles=days/1461;
114         days%=1461;
115
116         new_year+=cycles*4;
117
118         // See how many non-leap-years we counted as leap years and reclaim the lost days
119         unsigned missed_leap_days=((year-fudge)%100+cycles*4)/100;
120         if((year-fudge)%400+cycles*4>=400)
121                 --missed_leap_days;
122
123         days+=missed_leap_days;
124
125         // Count single years from the 4 year cycle
126         cycles=days/365;
127         days%=365;
128
129         new_year+=cycles;
130
131         if((year-fudge)%4+cycles>=4)
132         {
133                 // We passed a leap year - decrement days
134                 if(days==0)
135                 {
136                         --new_year;
137                         days=is_leap_year(new_year)?365:364;
138                 }
139                 else
140                         --days;
141         }
142
143         year=new_year;
144
145         // Step months
146         while(mday+days>month_days(year, month))
147         {
148                 days-=month_days(year, month);
149                 ++month;
150                 if(month>12)
151                 {
152                         ++year;
153                         month=1;
154                 }
155         }
156
157         mday+=days;
158 }
159
160 DateTime DateTime::operator+(const TimeDelta &td) const
161 {
162         DateTime dt(*this);
163         dt.add_raw(td.raw());
164         return dt;
165 }
166
167 DateTime &DateTime::operator+=(const TimeDelta &td)
168 {
169         add_raw(td.raw());
170         return *this;
171 }
172
173 int DateTime::cmp(const DateTime &dt) const
174 {
175         if(int c=cmp_(year, dt.year))
176                 return c;
177         if(int c=cmp_(month, dt.month))
178                 return c;
179         if(int c=cmp_(mday, dt.mday))
180                 return c;
181         if(int c=cmp_(hour, dt.hour))
182                 return c;
183         if(int c=cmp_(minute, dt.minute))
184                 return c;
185         if(int c=cmp_(second, dt.second))
186                 return c;
187         if(int c=cmp_(usec, dt.usec))
188                 return c;
189         return 0;
190 }
191
192 TimeStamp DateTime::get_timestamp() const
193 {
194         if(year<-289701 || year>293641)
195                 throw Exception("DateTime is not representable as a TimeStamp");
196
197         int64_t raw=(((hour*60LL)+minute)*60+second)*1000000+usec;
198         int days=(year-1970)*365;
199         days+=(year-1)/4-(year-1)/100+(year-1)/400-477;
200         for(unsigned i=1; i<month; ++i)
201                 days+=month_days(year, i);
202         days+=mday-1;
203
204         raw+=days*86400000000LL;
205
206         return TimeStamp(raw);
207 }
208
209 string DateTime::format(const string &fmt) const
210 {
211         ostringstream ss;
212         ss.fill('0');
213         for(string::const_iterator i=fmt.begin(); i!=fmt.end(); ++i)
214         {
215                 if(*i=='%')
216                 {
217                         ++i;
218                         if(i==fmt.end())
219                                 break;
220                         else if(*i=='d')
221                                 ss<<setw(2)<<int(mday);
222                         else if(*i=='H')
223                                 ss<<setw(2)<<int(hour);
224                         else if(*i=='I')
225                                 ss<<setw(2)<<hour%12;
226                         else if(*i=='m')
227                                 ss<<setw(2)<<int(month);
228                         else if(*i=='M')
229                                 ss<<setw(2)<<int(minute);
230                         else if(*i=='p')
231                                 ss<<((hour>=12) ? "PM" : "AM");
232                         else if(*i=='S')
233                                 ss<<setw(2)<<int(second);
234                         else if(*i=='y')
235                                 ss<<setw(2)<<year%100;
236                         else if(*i=='Y')
237                                 ss<<year;
238                         else if(*i=='%')
239                                 ss<<'%';
240                 }
241                 else
242                         ss<<*i;
243         }
244
245         return ss.str();
246 }
247
248 void DateTime::add_raw(int64_t raw)
249 {
250         int32_t days=raw/86400000000LL;
251         raw%=86400000000LL;
252         if(raw<0)
253         {
254                 ++days;
255                 raw+=86400000000LL;
256         }
257
258         usec+=raw%1000000; raw/=1000000;
259         second+=raw%60;    raw/=60;
260         minute+=raw%60;    raw/=60;
261         hour+=raw%24;      raw/=24;
262
263         add_days(days);
264         normalize();
265 }
266
267 void DateTime::normalize()
268 {
269         second+=usec/1000000;
270         usec%=1000000;
271         minute+=second/60;
272         second%=60;
273         hour+=minute/60;
274         minute%=60;
275         mday+=hour/24;
276         hour%=24;
277         while(mday>month_days(year, month))
278         {
279                 mday-=month_days(year, month);
280                 ++month;
281                 if(month>12)
282                 {
283                         ++year;
284                         month=1;
285                 }
286         }
287 }
288
289 void DateTime::validate() const
290 {
291         if(usec>=1000000)
292                 throw InvalidParameterValue("Microseconds out of range");
293         if(second>=60)
294                 throw InvalidParameterValue("Seconds out of range");
295         if(minute>=60)
296                 throw InvalidParameterValue("Minutes out of range");
297         if(hour>=24)
298                 throw InvalidParameterValue("Hours out of range");
299         if(month<1 || month>12)
300                 throw InvalidParameterValue("Month out of range");
301         if(mday<1 || mday>month_days(year, month))
302                 throw InvalidParameterValue("Day of month out of range");
303 }
304
305 } // namespace Time
306 } // namespace Msp