1//
2// DateTime.cpp
3//
4// Library: Foundation
5// Package: DateTime
6// Module: DateTime
7//
8// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
9// and Contributors.
10//
11// SPDX-License-Identifier: BSL-1.0
12//
13
14
15#include "Poco/DateTime.h"
16#include "Poco/Timespan.h"
17#include "Poco/Exception.h"
18#include "Poco/Format.h"
19#include <algorithm>
20#include <cmath>
21#include <ctime>
22
23
24namespace Poco {
25
26
27DateTime::DateTime()
28{
29 Timestamp now;
30 _utcTime = now.utcTime();
31 computeGregorian(julianDay());
32 computeDaytime();
33}
34
35
36DateTime::DateTime(const tm& tmStruct):
37 _year(tmStruct.tm_year + 1900),
38 _month(tmStruct.tm_mon + 1),
39 _day(tmStruct.tm_mday),
40 _hour(tmStruct.tm_hour),
41 _minute(tmStruct.tm_min),
42 _second(tmStruct.tm_sec),
43 _millisecond(0),
44 _microsecond(0)
45{
46 if (isValid(_year, _month, _day, _hour, _minute, _second, _millisecond, _microsecond))
47 {
48 _utcTime = toUtcTime(toJulianDay(_year, _month, _day)) + 10*(_hour*Timespan::HOURS + _minute*Timespan::MINUTES + _second*Timespan::SECONDS);
49 }
50 else
51 {
52 throw Poco::InvalidArgumentException(Poco::format("Date time is %hd-%hd-%hdT%hd:%hd:%hd.%hd.%hd\n"
53 "Valid values:\n"
54 "0 <= year <= 9999\n"
55 "1 <= month <= 12\n"
56 "1 <= day <= %d\n"
57 "0 <= hour <= 23\n"
58 "0 <= minute <= 59\n"
59 "0 <= second <= 60\n"
60 "0 <= millisecond <= 999\n"
61 "0 <= microsecond <= 999",
62 _year, _month, _day, _hour, _minute,
63 _second, _millisecond, _microsecond,
64 daysOfMonth(_year, _month)));
65 }
66}
67
68
69DateTime::DateTime(const Timestamp& timestamp):
70 _utcTime(timestamp.utcTime())
71{
72 computeGregorian(julianDay());
73 computeDaytime();
74}
75
76
77DateTime::DateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, int microsecond):
78 _year(year),
79 _month(month),
80 _day(day),
81 _hour(hour),
82 _minute(minute),
83 _second(second),
84 _millisecond(millisecond),
85 _microsecond(microsecond)
86{
87 if (isValid(_year, _month, _day, _hour, _minute, _second, _millisecond, _microsecond))
88 {
89 _utcTime = toUtcTime(toJulianDay(_year, _month, _day)) +
90 10 * (_hour * Timespan::HOURS + _minute * Timespan::MINUTES + _second * Timespan::SECONDS +
91 _millisecond * Timespan::MILLISECONDS + _microsecond);
92 }
93 else
94 {
95 throw Poco::InvalidArgumentException(Poco::format("Date time is %hd-%hd-%hdT%hd:%hd:%hd.%hd.%hd\n"
96 "Valid values:\n"
97 "0 <= year <= 9999\n"
98 "1 <= month <= 12\n"
99 "1 <= day <= %d\n"
100 "0 <= hour <= 23\n"
101 "0 <= minute <= 59\n"
102 "0 <= second <= 60\n"
103 "0 <= millisecond <= 999\n"
104 "0 <= microsecond <= 999",
105 _year, _month, _day, _hour, _minute,
106 _second, _millisecond, _microsecond,
107 daysOfMonth(_year, _month)));
108 }
109}
110
111DateTime::DateTime(double otherJulianDay):
112 _utcTime(toUtcTime(otherJulianDay))
113{
114 computeGregorian(otherJulianDay);
115}
116
117
118DateTime::DateTime(Timestamp::UtcTimeVal otherUtcTime, Timestamp::TimeDiff diff):
119 _utcTime(otherUtcTime + diff*10)
120{
121 computeGregorian(julianDay());
122 computeDaytime();
123}
124
125
126DateTime::DateTime(const DateTime& dateTime):
127 _utcTime(dateTime._utcTime),
128 _year(dateTime._year),
129 _month(dateTime._month),
130 _day(dateTime._day),
131 _hour(dateTime._hour),
132 _minute(dateTime._minute),
133 _second(dateTime._second),
134 _millisecond(dateTime._millisecond),
135 _microsecond(dateTime._microsecond)
136{
137}
138
139
140DateTime::~DateTime()
141{
142}
143
144
145DateTime& DateTime::operator = (const DateTime& dateTime)
146{
147 if (&dateTime != this)
148 {
149 _utcTime = dateTime._utcTime;
150 _year = dateTime._year;
151 _month = dateTime._month;
152 _day = dateTime._day;
153 _hour = dateTime._hour;
154 _minute = dateTime._minute;
155 _second = dateTime._second;
156 _millisecond = dateTime._millisecond;
157 _microsecond = dateTime._microsecond;
158 }
159 return *this;
160}
161
162
163DateTime& DateTime::operator = (const Timestamp& otherTimestamp)
164{
165 _utcTime = otherTimestamp.utcTime();
166 computeGregorian(julianDay());
167 computeDaytime();
168 return *this;
169}
170
171
172DateTime& DateTime::operator = (double otherJulianDay)
173{
174 _utcTime = toUtcTime(otherJulianDay);
175 computeGregorian(otherJulianDay);
176 return *this;
177}
178
179
180DateTime& DateTime::assign(int otherYear, int otherMonth, int otherDay, int otherHour, int otherMinute, int otherSecond, int otherMillisecond, int otherMicrosecond)
181{
182 if(isValid(otherYear, otherMonth, otherDay, otherHour, otherMinute, otherSecond, otherMillisecond, otherMicrosecond))
183 {
184 _utcTime = toUtcTime(toJulianDay(otherYear, otherMonth, otherDay)) + 10*(otherHour*Timespan::HOURS + otherMinute*Timespan::MINUTES + otherSecond*Timespan::SECONDS + otherMillisecond*Timespan::MILLISECONDS + otherMicrosecond);
185 _year = static_cast<short>(otherYear);
186 _month = static_cast<short>(otherMonth);
187 _day = static_cast<short>(otherDay);
188 _hour = static_cast<short>(otherHour);
189 _minute = static_cast<short>(otherMinute);
190 _second = static_cast<short>(otherSecond);
191 _millisecond = static_cast<short>(otherMillisecond);
192 _microsecond = static_cast<short>(otherMicrosecond);
193 }
194 else
195 {
196 throw Poco::InvalidArgumentException(Poco::format("Date time is %d-%d-%dT%d:%d:%d:%d:%d\n"
197 "Valid values:\n"
198 "0 <= year <= 9999\n"
199 "1 <= month <= 12\n"
200 "1 <= day <= %d\n"
201 "0 <= hour <= 23\n"
202 "0 <= minute <= 59\n"
203 "0 <= second <= 60\n"
204 "0 <= millisecond <= 999\n"
205 "0 <= microsecond <= 999",
206 _year, _month, _day, _hour, _minute,
207 _second, _millisecond, _microsecond,
208 daysOfMonth(_year, _month)));
209 }
210
211 return *this;
212}
213
214
215void DateTime::swap(DateTime& dateTime)
216{
217 std::swap(_utcTime, dateTime._utcTime);
218 std::swap(_year, dateTime._year);
219 std::swap(_month, dateTime._month);
220 std::swap(_day, dateTime._day);
221 std::swap(_hour, dateTime._hour);
222 std::swap(_minute, dateTime._minute);
223 std::swap(_second, dateTime._second);
224 std::swap(_millisecond, dateTime._millisecond);
225 std::swap(_microsecond, dateTime._microsecond);
226}
227
228
229int DateTime::dayOfWeek() const
230{
231 return int((std::floor(julianDay() + 1.5))) % 7;
232}
233
234
235int DateTime::dayOfYear() const
236{
237 int doy = 0;
238 for (int currentMonth = 1; currentMonth < _month; ++currentMonth)
239 doy += daysOfMonth(_year, currentMonth);
240 doy += _day;
241 return doy;
242}
243
244
245int DateTime::daysOfMonth(int year, int month)
246{
247 poco_assert (month >= 1 && month <= 12);
248
249 static int daysOfMonthTable[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
250
251 if (month == 2 && isLeapYear(year))
252 return 29;
253 else
254 return daysOfMonthTable[month];
255}
256
257
258bool DateTime::isValid(int year, int month, int day, int hour, int minute, int second, int millisecond, int microsecond)
259{
260 return
261 (year >= 0 && year <= 9999) &&
262 (month >= 1 && month <= 12) &&
263 (day >= 1 && day <= daysOfMonth(year, month)) &&
264 (hour >= 0 && hour <= 23) &&
265 (minute >= 0 && minute <= 59) &&
266 (second >= 0 && second <= 60) &&
267 (millisecond >= 0 && millisecond <= 999) &&
268 (microsecond >= 0 && microsecond <= 999);
269}
270
271
272int DateTime::week(int firstDayOfWeek) const
273{
274 poco_assert (firstDayOfWeek >= 0 && firstDayOfWeek <= 6);
275
276 /// find the first firstDayOfWeek.
277 int baseDay = 1;
278 while (DateTime(_year, 1, baseDay).dayOfWeek() != firstDayOfWeek) ++baseDay;
279
280 int doy = dayOfYear();
281 int offs = baseDay <= 4 ? 0 : 1;
282 if (doy < baseDay)
283 return offs;
284 else
285 return (doy - baseDay)/7 + 1 + offs;
286}
287
288
289double DateTime::julianDay() const
290{
291 return toJulianDay(_utcTime);
292}
293
294
295DateTime DateTime::operator + (const Timespan& span) const
296{
297 return DateTime(_utcTime, span.totalMicroseconds());
298}
299
300
301DateTime DateTime::operator - (const Timespan& span) const
302{
303 return DateTime(_utcTime, -span.totalMicroseconds());
304}
305
306
307Timespan DateTime::operator - (const DateTime& dateTime) const
308{
309 return Timespan((_utcTime - dateTime._utcTime)/10);
310}
311
312
313DateTime& DateTime::operator += (const Timespan& span)
314{
315 _utcTime += span.totalMicroseconds()*10;
316 computeGregorian(julianDay());
317 computeDaytime();
318 return *this;
319}
320
321
322DateTime& DateTime::operator -= (const Timespan& span)
323{
324 _utcTime -= span.totalMicroseconds()*10;
325 computeGregorian(julianDay());
326 computeDaytime();
327 return *this;
328}
329
330
331tm DateTime::makeTM() const
332{
333 tm tmStruct;
334
335 tmStruct.tm_sec = _second;
336 tmStruct.tm_min = _minute;
337 tmStruct.tm_hour = _hour;
338 tmStruct.tm_mday = _day;
339 poco_assert (_month > 0);
340 tmStruct.tm_mon = _month - 1;
341 poco_assert (_year >= 1900);
342 tmStruct.tm_year = _year - 1900;
343 tmStruct.tm_wday = dayOfWeek();
344 int doy = dayOfYear();
345 poco_assert (_year >0);
346 tmStruct.tm_yday = doy - 1;
347 tmStruct.tm_isdst = -1;
348
349 return tmStruct;
350}
351
352
353void DateTime::makeUTC(int tzd)
354{
355 operator -= (Timespan(((Timestamp::TimeDiff) tzd)*Timespan::SECONDS));
356}
357
358
359void DateTime::makeLocal(int tzd)
360{
361 operator += (Timespan(((Timestamp::TimeDiff) tzd)*Timespan::SECONDS));
362}
363
364
365double DateTime::toJulianDay(int year, int month, int day, int hour, int minute, int second, int millisecond, int microsecond)
366{
367 // lookup table for (153*month - 457)/5 - note that 3 <= month <= 14.
368 static int lookup[] = {-91, -60, -30, 0, 31, 61, 92, 122, 153, 184, 214, 245, 275, 306, 337};
369
370 // day to double
371 double dday = double(day) + ((double((hour*60 + minute)*60 + second)*1000 + millisecond)*1000 + microsecond)/86400000000.0;
372 if (month < 3)
373 {
374 month += 12;
375 --year;
376 }
377 double dyear = double(year);
378 return dday + lookup[month] + 365*year + std::floor(dyear/4) - std::floor(dyear/100) + std::floor(dyear/400) + 1721118.5;
379}
380
381
382void DateTime::checkLimit(short& lower, short& higher, short limit)
383{
384 if (lower >= limit)
385 {
386 higher += short(lower / limit);
387 lower = short(lower % limit);
388 }
389}
390
391
392void DateTime::normalize()
393{
394 checkLimit(_microsecond, _millisecond, 1000);
395 checkLimit(_millisecond, _second, 1000);
396 checkLimit(_second, _minute, 60);
397 checkLimit(_minute, _hour, 60);
398 checkLimit(_hour, _day, 24);
399
400 if (_day > daysOfMonth(_year, _month))
401 {
402 _day -= static_cast<short>(daysOfMonth(_year, _month));
403 if (++_month > 12)
404 {
405 ++_year;
406 _month -= 12;
407 }
408 }
409}
410
411
412void DateTime::computeGregorian(double otherJulianDay)
413{
414 double z = std::floor(otherJulianDay - 1721118.5);
415 double r = otherJulianDay - 1721118.5 - z;
416 double g = z - 0.25;
417 double a = std::floor(g / 36524.25);
418 double b = a - std::floor(a/4);
419 _year = short(std::floor((b + g)/365.25));
420 double c = b + z - std::floor(365.25*_year);
421 _month = short(std::floor((5*c + 456)/153));
422 double dday = c - std::floor((153.0*_month - 457)/5) + r;
423 _day = short(dday);
424 if (_month > 12)
425 {
426 ++_year;
427 _month -= 12;
428 }
429 r *= 24;
430 _hour = short(std::floor(r));
431 r -= std::floor(r);
432 r *= 60;
433 _minute = short(std::floor(r));
434 r -= std::floor(r);
435 r *= 60;
436 _second = short(std::floor(r));
437 r -= std::floor(r);
438 r *= 1000;
439 _millisecond = short(std::floor(r));
440 r -= std::floor(r);
441 r *= 1000;
442 _microsecond = short(r + 0.5);
443
444 normalize();
445
446 poco_assert_dbg (_month >= 1 && _month <= 12);
447 poco_assert_dbg (_day >= 1 && _day <= daysOfMonth(_year, _month));
448 poco_assert_dbg (_hour >= 0 && _hour <= 23);
449 poco_assert_dbg (_minute >= 0 && _minute <= 59);
450 poco_assert_dbg (_second >= 0 && _second <= 59);
451 poco_assert_dbg (_millisecond >= 0 && _millisecond <= 999);
452 poco_assert_dbg (_microsecond >= 0 && _microsecond <= 999);
453}
454
455
456void DateTime::computeDaytime()
457{
458 Timespan span(_utcTime/10);
459 int spanHour = span.hours();
460 // Due to double rounding issues, the previous call to computeGregorian()
461 // may have crossed into the next or previous day. We need to correct that.
462 if (spanHour == 23 && _hour == 0)
463 {
464 _day--;
465 if (_day == 0)
466 {
467 _month--;
468 if (_month == 0)
469 {
470 _month = 12;
471 _year--;
472 }
473 _day = static_cast<short>(daysOfMonth(_year, _month));
474 }
475 }
476 else if (spanHour == 0 && _hour == 23)
477 {
478 _day++;
479 if (_day > daysOfMonth(_year, _month))
480 {
481 _month++;
482 if (_month > 12)
483 {
484 _month = 1;
485 _year++;
486 }
487 _day = 1;
488 }
489 }
490 _hour = static_cast<short>(spanHour);
491 _minute = static_cast<short>(span.minutes());
492 _second = static_cast<short>(span.seconds());
493 _millisecond = static_cast<short>(span.milliseconds());
494 _microsecond = static_cast<short>(span.microseconds());
495}
496
497
498} // namespace Poco
499