]> git.tdb.fi Git - libs/core.git/blob - source/time/datetime.cpp
Add Debug::Profiler
[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
68 DateTime::DateTime(int32_t y, uint8_t m, uint8_t d, uint8_t h, uint8_t n, uint8_t s):
69         year(y),
70         month(m),
71         mday(d),
72         hour(h),
73         minute(n),
74         second(s),
75         usec(0)
76 { }
77
78 DateTime::DateTime(int32_t y, uint8_t m, uint8_t d, uint8_t h, uint8_t n, uint8_t s, uint32_t u):
79         year(y),
80         month(m),
81         mday(d),
82         hour(h),
83         minute(n),
84         second(s),
85         usec(u)
86 { }
87
88 void DateTime::add_days(int32_t days)
89 {
90         unsigned new_year=year;
91
92         /* Leap years have a 400 year cycle, so any 400 consecutive years have a
93         constant number of days (400*365+97=146097) */
94         new_year+=days/146097*400;
95         days%=146097;
96
97         if(days<0)
98         {
99                 new_year-=400;
100                 days+=146097;
101         }
102
103         // Fudge factor for leap day
104         int fudge=(month<=2)?1:0;
105
106         // (Almost) every 4 year cycle has 1 leap year and 3 normal years
107         unsigned cycles=days/1461;
108         days%=1461;
109
110         new_year+=cycles*4;
111
112         // See how many non-leap-years we counted as leap years and reclaim the lost days
113         unsigned missed_leap_days=((year-fudge)%100+cycles*4)/100;
114         if((year-fudge)%400+cycles*4>=400)
115                 --missed_leap_days;
116
117         days+=missed_leap_days;
118
119         // Count single years from the 4 year cycle
120         cycles=days/365;
121         days%=365;
122
123         new_year+=cycles;
124
125         if((year-fudge)%4+cycles>=4)
126         {
127                 // We passed a leap year - decrement days
128                 if(days==0)
129                 {
130                         --new_year;
131                         days=is_leap_year(new_year)?365:364;
132                 }
133                 else
134                         --days;
135         }
136
137         year=new_year;
138
139         // Step months
140         while(mday+days>month_days(year, month))
141         {
142                 days-=month_days(year, month);
143                 ++month;
144                 if(month>12)
145                 {
146                         ++year;
147                         month=1;
148                 }
149         }
150
151         mday+=days;
152 }
153
154 DateTime DateTime::operator+(const TimeDelta &td) const
155 {
156         DateTime dt(*this);
157         dt.add_raw(td.raw());
158         return dt;
159 }
160
161 DateTime &DateTime::operator+=(const TimeDelta &td)
162 {
163         add_raw(td.raw());
164         return *this;
165 }
166
167 int DateTime::cmp(const DateTime &dt) const
168 {
169         if(int c=cmp_(year, dt.year))
170                 return c;
171         if(int c=cmp_(month, dt.month))
172                 return c;
173         if(int c=cmp_(mday, dt.mday))
174                 return c;
175         if(int c=cmp_(hour, dt.hour))
176                 return c;
177         if(int c=cmp_(minute, dt.minute))
178                 return c;
179         if(int c=cmp_(second, dt.second))
180                 return c;
181         if(int c=cmp_(usec, dt.usec))
182                 return c;
183         return 0;
184 }
185
186 TimeStamp DateTime::get_timestamp() const
187 {
188         if(year<-289701 || year>293641)
189                 throw Exception("DateTime is not representable as a TimeStamp");
190
191         int64_t raw=(((hour*60LL)+minute)*60+second)*1000000+usec;
192         int days=(year-1970)*365;
193         days+=(year-1)/4-(year-1)/100+(year-1)/400-477;
194         for(unsigned i=1; i<month; ++i)
195                 days+=month_days(year, i);
196         days+=mday-1;
197
198         raw+=days*86400000000LL;
199
200         return TimeStamp(raw);
201 }
202
203 string DateTime::format(const string &fmt) const
204 {
205         ostringstream ss;
206         ss.fill('0');
207         for(string::const_iterator i=fmt.begin(); i!=fmt.end(); ++i)
208         {
209                 if(*i=='%')
210                 {
211                         ++i;
212                         if(i==fmt.end())
213                                 break;
214                         else if(*i=='d')
215                                 ss<<setw(2)<<int(mday);
216                         else if(*i=='H')
217                                 ss<<setw(2)<<int(hour);
218                         else if(*i=='I')
219                                 ss<<setw(2)<<hour%12;
220                         else if(*i=='m')
221                                 ss<<setw(2)<<int(month);
222                         else if(*i=='M')
223                                 ss<<setw(2)<<int(minute);
224                         else if(*i=='p')
225                                 ss<<((hour>=12) ? "PM" : "AM");
226                         else if(*i=='S')
227                                 ss<<setw(2)<<int(second);
228                         else if(*i=='y')
229                                 ss<<setw(2)<<year%100;
230                         else if(*i=='Y')
231                                 ss<<year;
232                         else if(*i=='%')
233                                 ss<<'%';
234                 }
235                 else
236                         ss<<*i;
237         }
238
239         return ss.str();
240 }
241
242 void DateTime::add_raw(int64_t raw)
243 {
244         int32_t days=raw/86400000000LL;
245         raw%=86400000000LL;
246         if(raw<0)
247         {
248                 ++days;
249                 raw+=86400000000LL;
250         }
251
252         usec+=raw%1000000; raw/=1000000;
253         second+=raw%60;    raw/=60;
254         minute+=raw%60;    raw/=60;
255         hour+=raw%24;      raw/=24;
256
257         add_days(days);
258         normalize();
259 }
260
261 void DateTime::normalize()
262 {
263         second+=usec/1000000;
264         usec%=1000000;
265         minute+=second/60;
266         second%=60;
267         hour+=minute/60;
268         minute%=60;
269         mday+=hour/24;
270         hour%=24;
271         while(mday>month_days(year, month))
272         {
273                 mday-=month_days(year, month);
274                 ++month;
275                 if(month>12)
276                 {
277                         ++year;
278                         month=1;
279                 }
280         }
281 }
282
283 } // namespace Time
284 } // namespace Msp