DateTime.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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. #ifndef Foundation_DateTime_INCLUDED
  16. #define Foundation_DateTime_INCLUDED
  17. #include "Poco/Foundation.h"
  18. #include "Poco/Timestamp.h"
  19. #include "Poco/Timespan.h"
  20. namespace Poco {
  21. class Foundation_API DateTime
  22. /// This class represents an instant in time, expressed
  23. /// in years, months, days, hours, minutes, seconds
  24. /// and milliseconds based on the Gregorian calendar.
  25. /// The class is mainly useful for conversions between
  26. /// UTC, Julian day and Gregorian calendar dates.
  27. ///
  28. /// The date and time stored in a DateTime is always in UTC
  29. /// (Coordinated Universal Time) and thus independent of the
  30. /// timezone in effect on the system.
  31. ///
  32. /// Conversion calculations are based on algorithms
  33. /// collected and described by Peter Baum at
  34. /// http://vsg.cape.com/~pbaum/date/date0.htm
  35. ///
  36. /// Internally, this class stores a date/time in two
  37. /// forms (UTC and broken down) for performance reasons. Only use
  38. /// this class for conversions between date/time representations.
  39. /// Use the Timestamp class for everything else.
  40. ///
  41. /// Notes:
  42. /// * Zero is a valid year (in accordance with ISO 8601 and astronomical year numbering)
  43. /// * Year zero (0) is a leap year
  44. /// * Negative years (years preceding 1 BC) are not supported
  45. ///
  46. /// For more information, please see:
  47. /// * http://en.wikipedia.org/wiki/Gregorian_Calendar
  48. /// * http://en.wikipedia.org/wiki/Julian_day
  49. /// * http://en.wikipedia.org/wiki/UTC
  50. /// * http://en.wikipedia.org/wiki/ISO_8601
  51. {
  52. public:
  53. enum Months
  54. /// Symbolic names for month numbers (1 to 12).
  55. {
  56. JANUARY = 1,
  57. FEBRUARY,
  58. MARCH,
  59. APRIL,
  60. MAY,
  61. JUNE,
  62. JULY,
  63. AUGUST,
  64. SEPTEMBER,
  65. OCTOBER,
  66. NOVEMBER,
  67. DECEMBER
  68. };
  69. enum DaysOfWeek
  70. /// Symbolic names for week day numbers (0 to 6).
  71. {
  72. SUNDAY = 0,
  73. MONDAY,
  74. TUESDAY,
  75. WEDNESDAY,
  76. THURSDAY,
  77. FRIDAY,
  78. SATURDAY
  79. };
  80. DateTime();
  81. /// Creates a DateTime for the current date and time.
  82. DateTime(const Timestamp& timestamp);
  83. /// Creates a DateTime for the date and time given in
  84. /// a Timestamp.
  85. DateTime(int year, int month, int day, int hour = 0, int minute = 0, int second = 0, int millisecond = 0, int microsecond = 0);
  86. /// Creates a DateTime for the given Gregorian date and time.
  87. /// * year is from 0 to 9999.
  88. /// * month is from 1 to 12.
  89. /// * day is from 1 to 31.
  90. /// * hour is from 0 to 23.
  91. /// * minute is from 0 to 59.
  92. /// * second is from 0 to 60 (allowing leap seconds).
  93. /// * millisecond is from 0 to 999.
  94. /// * microsecond is from 0 to 999.
  95. DateTime(double julianDay);
  96. /// Creates a DateTime for the given Julian day.
  97. DateTime(Timestamp::UtcTimeVal utcTime, Timestamp::TimeDiff diff);
  98. /// Creates a DateTime from an UtcTimeVal and a TimeDiff.
  99. ///
  100. /// Mainly used internally by DateTime and friends.
  101. DateTime(const DateTime& dateTime);
  102. /// Copy constructor. Creates the DateTime from another one.
  103. ~DateTime();
  104. /// Destroys the DateTime.
  105. DateTime& operator = (const DateTime& dateTime);
  106. /// Assigns another DateTime.
  107. DateTime& operator = (const Timestamp& timestamp);
  108. /// Assigns a Timestamp.
  109. DateTime& operator = (double julianDay);
  110. /// Assigns a Julian day.
  111. DateTime& assign(int year, int month, int day, int hour = 0, int minute = 0, int second = 0, int millisecond = 0, int microseconds = 0);
  112. /// Assigns a Gregorian date and time.
  113. /// * year is from 0 to 9999.
  114. /// * month is from 1 to 12.
  115. /// * day is from 1 to 31.
  116. /// * hour is from 0 to 23.
  117. /// * minute is from 0 to 59.
  118. /// * second is from 0 to 60 (allowing leap seconds).
  119. /// * millisecond is from 0 to 999.
  120. /// * microsecond is from 0 to 999.
  121. void swap(DateTime& dateTime);
  122. /// Swaps the DateTime with another one.
  123. int year() const;
  124. /// Returns the year.
  125. int month() const;
  126. /// Returns the month (1 to 12).
  127. int week(int firstDayOfWeek = MONDAY) const;
  128. /// Returns the week number within the year.
  129. /// FirstDayOfWeek should be either SUNDAY (0) or MONDAY (1).
  130. /// The returned week number will be from 0 to 53. Week number 1 is the week
  131. /// containing January 4. This is in accordance to ISO 8601.
  132. ///
  133. /// The following example assumes that firstDayOfWeek is MONDAY. For 2005, which started
  134. /// on a Saturday, week 1 will be the week starting on Monday, January 3.
  135. /// January 1 and 2 will fall within week 0 (or the last week of the previous year).
  136. ///
  137. /// For 2007, which starts on a Monday, week 1 will be the week startung on Monday, January 1.
  138. /// There will be no week 0 in 2007.
  139. int day() const;
  140. /// Returns the day witin the month (1 to 31).
  141. int dayOfWeek() const;
  142. /// Returns the weekday (0 to 6, where
  143. /// 0 = Sunday, 1 = Monday, ..., 6 = Saturday).
  144. int dayOfYear() const;
  145. /// Returns the number of the day in the year.
  146. /// January 1 is 1, February 1 is 32, etc.
  147. int hour() const;
  148. /// Returns the hour (0 to 23).
  149. int hourAMPM() const;
  150. /// Returns the hour (0 to 12).
  151. bool isAM() const;
  152. /// Returns true if hour < 12;
  153. bool isPM() const;
  154. /// Returns true if hour >= 12.
  155. int minute() const;
  156. /// Returns the minute (0 to 59).
  157. int second() const;
  158. /// Returns the second (0 to 59).
  159. int millisecond() const;
  160. /// Returns the millisecond (0 to 999)
  161. int microsecond() const;
  162. /// Returns the microsecond (0 to 999)
  163. double julianDay() const;
  164. /// Returns the julian day for the date and time.
  165. Timestamp timestamp() const;
  166. /// Returns the date and time expressed as a Timestamp.
  167. Timestamp::UtcTimeVal utcTime() const;
  168. /// Returns the date and time expressed in UTC-based
  169. /// time. UTC base time is midnight, October 15, 1582.
  170. /// Resolution is 100 nanoseconds.
  171. bool operator == (const DateTime& dateTime) const;
  172. bool operator != (const DateTime& dateTime) const;
  173. bool operator < (const DateTime& dateTime) const;
  174. bool operator <= (const DateTime& dateTime) const;
  175. bool operator > (const DateTime& dateTime) const;
  176. bool operator >= (const DateTime& dateTime) const;
  177. DateTime operator + (const Timespan& span) const;
  178. DateTime operator - (const Timespan& span) const;
  179. Timespan operator - (const DateTime& dateTime) const;
  180. DateTime& operator += (const Timespan& span);
  181. DateTime& operator -= (const Timespan& span);
  182. void makeUTC(int tzd);
  183. /// Converts a local time into UTC, by applying the given time zone differential.
  184. void makeLocal(int tzd);
  185. /// Converts a UTC time into a local time, by applying the given time zone differential.
  186. static bool isLeapYear(int year);
  187. /// Returns true if the given year is a leap year;
  188. /// false otherwise.
  189. static int daysOfMonth(int year, int month);
  190. /// Returns the number of days in the given month
  191. /// and year. Month is from 1 to 12.
  192. static bool isValid(int year, int month, int day, int hour = 0, int minute = 0, int second = 0, int millisecond = 0, int microsecond = 0);
  193. /// Checks if the given date and time is valid
  194. /// (all arguments are within a proper range).
  195. ///
  196. /// Returns true if all arguments are valid, false otherwise.
  197. protected:
  198. static double toJulianDay(Timestamp::UtcTimeVal utcTime);
  199. /// Computes the Julian day for an UTC time.
  200. static double toJulianDay(int year, int month, int day, int hour = 0, int minute = 0, int second = 0, int millisecond = 0, int microsecond = 0);
  201. /// Computes the Julian day for a gregorian calendar date and time.
  202. /// See <http://vsg.cape.com/~pbaum/date/jdimp.htm>, section 2.3.1 for the algorithm.
  203. static Timestamp::UtcTimeVal toUtcTime(double julianDay);
  204. /// Computes the UTC time for a Julian day.
  205. void computeGregorian(double julianDay);
  206. /// Computes the Gregorian date for the given Julian day.
  207. /// See <http://vsg.cape.com/~pbaum/date/injdimp.htm>, section 3.3.1 for the algorithm.
  208. void computeDaytime();
  209. /// Extracts the daytime (hours, minutes, seconds, etc.) from the stored utcTime.
  210. private:
  211. void checkLimit(short& lower, short& higher, short limit);
  212. void normalize();
  213. ///utility functions used to correct the overflow in computeGregorian
  214. Timestamp::UtcTimeVal _utcTime;
  215. short _year;
  216. short _month;
  217. short _day;
  218. short _hour;
  219. short _minute;
  220. short _second;
  221. short _millisecond;
  222. short _microsecond;
  223. };
  224. //
  225. // inlines
  226. //
  227. inline Timestamp DateTime::timestamp() const
  228. {
  229. return Timestamp::fromUtcTime(_utcTime);
  230. }
  231. inline Timestamp::UtcTimeVal DateTime::utcTime() const
  232. {
  233. return _utcTime;
  234. }
  235. inline int DateTime::year() const
  236. {
  237. return _year;
  238. }
  239. inline int DateTime::month() const
  240. {
  241. return _month;
  242. }
  243. inline int DateTime::day() const
  244. {
  245. return _day;
  246. }
  247. inline int DateTime::hour() const
  248. {
  249. return _hour;
  250. }
  251. inline int DateTime::hourAMPM() const
  252. {
  253. if (_hour < 1)
  254. return 12;
  255. else if (_hour > 12)
  256. return _hour - 12;
  257. else
  258. return _hour;
  259. }
  260. inline bool DateTime::isAM() const
  261. {
  262. return _hour < 12;
  263. }
  264. inline bool DateTime::isPM() const
  265. {
  266. return _hour >= 12;
  267. }
  268. inline int DateTime::minute() const
  269. {
  270. return _minute;
  271. }
  272. inline int DateTime::second() const
  273. {
  274. return _second;
  275. }
  276. inline int DateTime::millisecond() const
  277. {
  278. return _millisecond;
  279. }
  280. inline int DateTime::microsecond() const
  281. {
  282. return _microsecond;
  283. }
  284. inline bool DateTime::operator == (const DateTime& dateTime) const
  285. {
  286. return _utcTime == dateTime._utcTime;
  287. }
  288. inline bool DateTime::operator != (const DateTime& dateTime) const
  289. {
  290. return _utcTime != dateTime._utcTime;
  291. }
  292. inline bool DateTime::operator < (const DateTime& dateTime) const
  293. {
  294. return _utcTime < dateTime._utcTime;
  295. }
  296. inline bool DateTime::operator <= (const DateTime& dateTime) const
  297. {
  298. return _utcTime <= dateTime._utcTime;
  299. }
  300. inline bool DateTime::operator > (const DateTime& dateTime) const
  301. {
  302. return _utcTime > dateTime._utcTime;
  303. }
  304. inline bool DateTime::operator >= (const DateTime& dateTime) const
  305. {
  306. return _utcTime >= dateTime._utcTime;
  307. }
  308. inline bool DateTime::isLeapYear(int year)
  309. {
  310. return (year % 4) == 0 && ((year % 100) != 0 || (year % 400) == 0);
  311. }
  312. inline void swap(DateTime& d1, DateTime& d2)
  313. {
  314. d1.swap(d2);
  315. }
  316. } // namespace Poco
  317. #endif // Foundation_DateTime_INCLUDED