Coverage Report

Created: 2026-12-03 06:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/poco/Foundation/include/Poco/DateTime.h
Line
Count
Source
1
//
2
// DateTime.h
3
//
4
// Library: Foundation
5
// Package: DateTime
6
// Module:  DateTime
7
//
8
// Definition of the DateTime class.
9
//
10
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
11
// and Contributors.
12
//
13
// SPDX-License-Identifier: BSL-1.0
14
//
15
16
17
#ifndef Foundation_DateTime_INCLUDED
18
#define Foundation_DateTime_INCLUDED
19
20
21
#include "Poco/Foundation.h"
22
#include "Poco/Timestamp.h"
23
#include "Poco/Timespan.h"
24
25
26
struct tm;
27
28
29
namespace Poco {
30
31
32
class Foundation_API DateTime
33
  /// This class represents an instant in time, expressed
34
  /// in years, months, days, hours, minutes, seconds
35
  /// and milliseconds based on the Gregorian calendar.
36
  /// The class is mainly useful for conversions between
37
  /// UTC, Julian day and Gregorian calendar dates.
38
  ///
39
  /// The date and time stored in a DateTime is always in UTC
40
  /// (Coordinated Universal Time) and thus independent of the
41
  /// timezone in effect on the system.
42
  ///
43
  /// Conversion calculations are based on algorithms
44
  /// collected and described by Peter Baum at
45
  /// http://vsg.cape.com/~pbaum/date/date0.htm
46
  ///
47
  /// Internally, this class stores a date/time in two
48
  /// forms (UTC and broken down) for performance reasons. Only use
49
  /// this class for conversions between date/time representations.
50
  /// Use the Timestamp class for everything else.
51
  ///
52
  /// Notes:
53
  ///   * Zero is a valid year (in accordance with ISO 8601 and astronomical year numbering)
54
  ///   * Year zero (0) is a leap year
55
  ///   * Minimum date/time that can be represented is 12:00:00 UTC Monday, 1 January 4713 BC
56
  ///    (Julian Day 0, Gregorian -4713-11-24 12:00:00)
57
  ///
58
  /// For more information, please see:
59
  ///   * http://en.wikipedia.org/wiki/Gregorian_Calendar
60
  ///   * http://en.wikipedia.org/wiki/Julian_day
61
  ///   * http://en.wikipedia.org/wiki/UTC
62
  ///   * http://en.wikipedia.org/wiki/ISO_8601
63
{
64
public:
65
  enum Months
66
    /// Symbolic names for month numbers (1 to 12).
67
  {
68
    JANUARY = 1,
69
    FEBRUARY,
70
    MARCH,
71
    APRIL,
72
    MAY,
73
    JUNE,
74
    JULY,
75
    AUGUST,
76
    SEPTEMBER,
77
    OCTOBER,
78
    NOVEMBER,
79
    DECEMBER
80
  };
81
82
  enum DaysOfWeek
83
    /// Symbolic names for week day numbers (0 to 6).
84
  {
85
    SUNDAY = 0,
86
    MONDAY,
87
    TUESDAY,
88
    WEDNESDAY,
89
    THURSDAY,
90
    FRIDAY,
91
    SATURDAY
92
  };
93
94
  DateTime();
95
    /// Creates a DateTime for the current date and time.
96
97
  DateTime(const tm& tmStruct);
98
    /// Creates a DateTime from tm struct.
99
100
  DateTime(const Timestamp& timestamp);
101
    /// Creates a DateTime for the date and time given in
102
    /// a Timestamp.
103
104
  DateTime(int year, int month, int day, int hour = 0, int minute = 0, int second = 0, int millisecond = 0, int microsecond = 0);
105
    /// Creates a DateTime for the given Gregorian date and time.
106
    ///   * year is from 0 to 9999.
107
    ///   * month is from 1 to 12.
108
    ///   * day is from 1 to 31.
109
    ///   * hour is from 0 to 23.
110
    ///   * minute is from 0 to 59.
111
    ///   * second is from 0 to 60.
112
    ///   * millisecond is from 0 to 999.
113
    ///   * microsecond is from 0 to 999.
114
    ///
115
    /// Throws an InvalidArgumentException if an argument date is out of range.
116
117
  DateTime(double julianDay);
118
    /// Creates a DateTime for the given Julian day.
119
    ///
120
    /// Throws an InvalidArgumentException if the Julian day is not finite
121
    /// or out of range.
122
123
  DateTime(Timestamp::UtcTimeVal utcTime, Timestamp::TimeDiff diff);
124
    /// Creates a DateTime from an UtcTimeVal and a TimeDiff.
125
    ///
126
    /// Mainly used internally by DateTime and friends.
127
128
  DateTime(const DateTime& dateTime);
129
    /// Copy constructor. Creates the DateTime from another one.
130
131
  ~DateTime();
132
    /// Destroys the DateTime.
133
134
  DateTime& operator = (const DateTime& dateTime);
135
    /// Assigns another DateTime.
136
137
  DateTime& operator = (const Timestamp& timestamp);
138
    /// Assigns a Timestamp.
139
140
  DateTime& operator = (double julianDay);
141
    /// Assigns a Julian day.
142
    ///
143
    /// Throws an InvalidArgumentException if the Julian day is not finite
144
    /// or out of range; the DateTime is left unchanged.
145
146
  DateTime& assign(int year, int month, int day, int hour = 0, int minute = 0, int second = 0, int millisecond = 0, int microseconds = 0);
147
    /// Assigns a Gregorian date and time.
148
    ///   * year is from 0 to 9999.
149
    ///   * month is from 1 to 12.
150
    ///   * day is from 1 to 31.
151
    ///   * hour is from 0 to 23.
152
    ///   * minute is from 0 to 59.
153
    ///   * second is from 0 to 60.
154
    ///   * millisecond is from 0 to 999.
155
    ///   * microsecond is from 0 to 999.
156
    ///
157
    /// Throws an InvalidArgumentException if an argument date is out of range.
158
    /// The DateTime is not modified in that case.
159
160
  void swap(DateTime& dateTime) noexcept;
161
    /// Swaps the DateTime with another one.
162
163
  [[nodiscard]] int year() const;
164
    /// Returns the year.
165
166
  [[nodiscard]] int month() const;
167
    /// Returns the month (1 to 12).
168
169
  [[nodiscard]] int week(int firstDayOfWeek = MONDAY) const;
170
    /// Returns the week number within the year.
171
    /// FirstDayOfWeek should be either SUNDAY (0) or MONDAY (1).
172
    /// The returned week number will be from 0 to 53. Week number 1 is the week
173
    /// containing January 4. This is in accordance to ISO 8601.
174
    ///
175
    /// The following example assumes that firstDayOfWeek is MONDAY. For 2005, which started
176
    /// on a Saturday, week 1 will be the week starting on Monday, January 3.
177
    /// January 1 and 2 will fall within week 0 (or the last week of the previous year).
178
    ///
179
    /// For 2007, which starts on a Monday, week 1 will be the week starting on Monday, January 1.
180
    /// There will be no week 0 in 2007.
181
182
  [[nodiscard]] int day() const;
183
    /// Returns the day within the month (1 to 31).
184
185
  [[nodiscard]] int dayOfWeek() const;
186
    /// Returns the weekday (0 to 6, where
187
    /// 0 = Sunday, 1 = Monday, ..., 6 = Saturday).
188
189
  [[nodiscard]] int dayOfYear() const;
190
    /// Returns the number of the day in the year.
191
    /// January 1 is 1, February 1 is 32, etc.
192
193
  [[nodiscard]] int hour() const;
194
    /// Returns the hour (0 to 23).
195
196
  [[nodiscard]] int hourAMPM() const;
197
    /// Returns the hour (0 to 12).
198
199
  [[nodiscard]] bool isAM() const;
200
    /// Returns true if hour < 12;
201
202
  [[nodiscard]] bool isPM() const;
203
    /// Returns true if hour >= 12.
204
205
  [[nodiscard]] int minute() const;
206
    /// Returns the minute (0 to 59).
207
208
  [[nodiscard]] int second() const;
209
    /// Returns the second (0 to 59).
210
211
  [[nodiscard]] int millisecond() const;
212
    /// Returns the millisecond (0 to 999)
213
214
  [[nodiscard]] int microsecond() const;
215
    /// Returns the microsecond (0 to 999)
216
217
  [[nodiscard]] double julianDay() const;
218
    /// Returns the Julian day for the date and time.
219
220
  [[nodiscard]] Timestamp timestamp() const;
221
    /// Returns the date and time expressed as a Timestamp.
222
223
  [[nodiscard]] Timestamp::UtcTimeVal utcTime() const;
224
    /// Returns the date and time expressed in UTC-based
225
    /// time. UTC base time is midnight, October 15, 1582.
226
    /// Resolution is 100 nanoseconds.
227
228
  [[nodiscard]] bool operator == (const DateTime& dateTime) const;
229
  [[nodiscard]] bool operator != (const DateTime& dateTime) const;
230
  [[nodiscard]] bool operator <  (const DateTime& dateTime) const;
231
  [[nodiscard]] bool operator <= (const DateTime& dateTime) const;
232
  [[nodiscard]] bool operator >  (const DateTime& dateTime) const;
233
  [[nodiscard]] bool operator >= (const DateTime& dateTime) const;
234
235
  DateTime  operator +  (const Timespan& span) const;
236
  DateTime  operator -  (const Timespan& span) const;
237
  Timespan  operator -  (const DateTime& dateTime) const;
238
  DateTime& operator += (const Timespan& span);
239
  DateTime& operator -= (const Timespan& span);
240
241
  [[nodiscard]] tm makeTM() const;
242
    /// Converts DateTime to tm struct.
243
244
  void makeUTC(int tzd);
245
    /// Converts a local time into UTC, by applying the given time zone differential.
246
247
  void makeLocal(int tzd);
248
    /// Converts a UTC time into a local time, by applying the given time zone differential.
249
250
  [[nodiscard]] static bool isLeapYear(int year);
251
    /// Returns true if the given year is a leap year;
252
    /// false otherwise.
253
254
  [[nodiscard]] static int daysOfMonth(int year, int month);
255
    /// Returns the number of days in the given month
256
    /// and year. Month is from 1 to 12.
257
258
  [[nodiscard]] static bool isValid(int year, int month, int day, int hour = 0, int minute = 0, int second = 0, int millisecond = 0, int microsecond = 0);
259
    /// Checks if the given date and time is valid
260
    /// (all arguments are within a proper range).
261
    ///
262
    /// Returns true if all arguments are valid, false otherwise.
263
264
protected:
265
  void checkValid();
266
    /// Checks if the given date and time is valid (all arguments are within a proper range).
267
    /// Expects all members to be set.
268
    ///
269
    /// Throws Poco::InvalidArgumentException if any of the arguments is not valid.
270
271
  static void checkValid(int year, int month, int day, int hour, int minute, int second, int millisecond, int microsecond);
272
    /// Checks if the given date and time is valid (all arguments are within a proper range).
273
    ///
274
    /// Throws Poco::InvalidArgumentException if any of the arguments is not valid.
275
276
  [[nodiscard]] static double toJulianDay(Timestamp::UtcTimeVal utcTime);
277
    /// Computes the Julian day for an UTC time.
278
279
  [[nodiscard]] static double toJulianDay(int year, int month, int day, int hour = 0, int minute = 0, int second = 0, int millisecond = 0, int microsecond = 0);
280
    /// Computes the Julian day for a Gregorian calendar date and time.
281
    /// See <http://vsg.cape.com/~pbaum/date/jdimp.htm>, section 2.3.1 for the algorithm.
282
283
  [[nodiscard]] static Timestamp::UtcTimeVal toUtcTime(double julianDay);
284
    /// Computes the UTC time for a Julian day.
285
    ///
286
    /// Throws Poco::InvalidArgumentException if the Julian day is not finite
287
    /// or outside the range of a DateTime (-4713-01-01 to 9999-12-31).
288
289
  void computeGregorian(double julianDay);
290
    /// Computes the Gregorian date for the given Julian day.
291
    /// See <http://vsg.cape.com/~pbaum/date/injdimp.htm>, section 3.3.1 for the algorithm.
292
293
  void computeDaytime();
294
    /// Extracts the daytime (hours, minutes, seconds, etc.) from the stored utcTime.
295
296
private:
297
  void checkLimit(short& lower, short& higher, short limit);
298
  void normalize();
299
    ///utility functions used to correct the overflow in computeGregorian
300
301
  Timestamp::UtcTimeVal _utcTime;
302
  short _year;
303
  short _month;
304
  short _day;
305
  short _hour;
306
  short _minute;
307
  short _second;
308
  short _millisecond;
309
  short _microsecond;
310
};
311
312
313
//
314
// inlines
315
//
316
317
318
inline double DateTime::toJulianDay(Timestamp::UtcTimeVal utcTime)
319
263k
{
320
263k
  double utcDays = double(utcTime)/864000000000.0;
321
263k
  return utcDays + 2299160.5; // first day of Gregorian reform (Oct 15 1582)
322
263k
}
323
324
325
inline Timestamp DateTime::timestamp() const
326
32.7k
{
327
32.7k
  return Timestamp::fromUtcTime(_utcTime);
328
32.7k
}
329
330
331
inline Timestamp::UtcTimeVal DateTime::utcTime() const
332
0
{
333
0
  return _utcTime;
334
0
}
335
336
337
inline int DateTime::year() const
338
33.0k
{
339
33.0k
  return _year;
340
33.0k
}
341
342
343
inline int DateTime::month() const
344
33.0k
{
345
33.0k
  return _month;
346
33.0k
}
347
348
349
inline int DateTime::day() const
350
33.0k
{
351
33.0k
  return _day;
352
33.0k
}
353
354
355
inline int DateTime::hour() const
356
33.0k
{
357
33.0k
  return _hour;
358
33.0k
}
359
360
361
inline int DateTime::hourAMPM() const
362
1.99k
{
363
1.99k
  if (_hour < 1)
364
1.80k
    return 12;
365
187
  else if (_hour > 12)
366
121
    return _hour - 12;
367
66
  else
368
66
    return _hour;
369
1.99k
}
370
371
372
inline bool DateTime::isAM() const
373
1.99k
{
374
1.99k
  return _hour < 12;
375
1.99k
}
376
377
378
inline bool DateTime::isPM() const
379
0
{
380
0
  return _hour >= 12;
381
0
}
382
383
384
inline int DateTime::minute() const
385
35.0k
{
386
35.0k
  return _minute;
387
35.0k
}
388
389
390
inline int DateTime::second() const
391
31.0k
{
392
31.0k
  return _second;
393
31.0k
}
394
395
396
inline int DateTime::millisecond() const
397
3.99k
{
398
3.99k
  return _millisecond;
399
3.99k
}
400
401
402
inline int DateTime::microsecond() const
403
3.99k
{
404
3.99k
  return _microsecond;
405
3.99k
}
406
407
408
inline bool DateTime::operator == (const DateTime& dateTime) const
409
0
{
410
0
  return _utcTime == dateTime._utcTime;
411
0
}
412
413
414
inline bool DateTime::operator != (const DateTime& dateTime) const
415
0
{
416
0
  return _utcTime != dateTime._utcTime;
417
0
}
418
419
420
inline bool DateTime::operator <  (const DateTime& dateTime) const
421
0
{
422
0
  return _utcTime < dateTime._utcTime;
423
0
}
424
425
426
inline bool DateTime::operator <= (const DateTime& dateTime) const
427
0
{
428
0
  return _utcTime <= dateTime._utcTime;
429
0
}
430
431
432
inline bool DateTime::operator >  (const DateTime& dateTime) const
433
0
{
434
0
  return _utcTime > dateTime._utcTime;
435
0
}
436
437
438
inline bool DateTime::operator >= (const DateTime& dateTime) const
439
0
{
440
0
  return _utcTime >= dateTime._utcTime;
441
0
}
442
443
444
inline bool DateTime::isLeapYear(int year)
445
16.6k
{
446
16.6k
  return (year % 4) == 0 && ((year % 100) != 0 || (year % 400) == 0);
447
16.6k
}
448
449
450
inline void swap(DateTime& d1, DateTime& d2) noexcept
451
0
{
452
0
  d1.swap(d2);
453
0
}
454
455
456
} // namespace Poco
457
458
459
#endif // Foundation_DateTime_INCLUDED