LocalDateTime.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. //
  2. // LocalDateTime.h
  3. //
  4. // Library: Foundation
  5. // Package: DateTime
  6. // Module: LocalDateTime
  7. //
  8. // Definition of the LocalDateTime 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_LocalDateTime_INCLUDED
  16. #define Foundation_LocalDateTime_INCLUDED
  17. #include "Poco/Foundation.h"
  18. #include "Poco/DateTime.h"
  19. namespace Poco {
  20. class Foundation_API LocalDateTime
  21. /// This class represents an instant in local time
  22. /// (as opposed to UTC), expressed in years, months, days,
  23. /// hours, minutes, seconds and milliseconds based on the
  24. /// Gregorian calendar.
  25. ///
  26. /// In addition to the date and time, the class also
  27. /// maintains a time zone differential, which denotes
  28. /// the difference in seconds from UTC to local time,
  29. /// i.e. UTC = local time - time zone differential.
  30. ///
  31. /// Although LocalDateTime supports relational and arithmetic
  32. /// operators, all date/time comparisons and date/time arithmetics
  33. /// should be done in UTC, using the DateTime or Timestamp
  34. /// class for better performance. The relational operators
  35. /// normalize the dates/times involved to UTC before carrying out
  36. /// the comparison.
  37. ///
  38. /// The time zone differential is based on the input date and
  39. /// time and current time zone. A number of constructors accept
  40. /// an explicit time zone differential parameter. These should
  41. /// not be used since daylight savings time processing is impossible
  42. /// since the time zone is unknown. Each of the constructors
  43. /// accepting a tzd parameter have been marked as deprecated and
  44. /// may be removed in a future revision.
  45. {
  46. public:
  47. LocalDateTime();
  48. /// Creates a LocalDateTime with the current date/time
  49. /// for the current time zone.
  50. LocalDateTime(int year, int month, int day, int hour = 0, int minute = 0, int second = 0, int millisecond = 0, int microsecond = 0);
  51. /// Creates a LocalDateTime for the given Gregorian local date and time.
  52. /// * year is from 0 to 9999.
  53. /// * month is from 1 to 12.
  54. /// * day is from 1 to 31.
  55. /// * hour is from 0 to 23.
  56. /// * minute is from 0 to 59.
  57. /// * second is from 0 to 60 (allowing leap seconds).
  58. /// * millisecond is from 0 to 999.
  59. /// * microsecond is from 0 to 999.
  60. //@ deprecated
  61. LocalDateTime(int tzd, int year, int month, int day, int hour, int minute, int second, int millisecond, int microsecond);
  62. /// Creates a LocalDateTime for the given Gregorian date and time in the
  63. /// time zone denoted by the time zone differential in tzd.
  64. /// * tzd is in seconds.
  65. /// * year is from 0 to 9999.
  66. /// * month is from 1 to 12.
  67. /// * day is from 1 to 31.
  68. /// * hour is from 0 to 23.
  69. /// * minute is from 0 to 59.
  70. /// * second is from 0 to 60 (allowing leap seconds).
  71. /// * millisecond is from 0 to 999.
  72. /// * microsecond is from 0 to 999.
  73. LocalDateTime(const DateTime& dateTime);
  74. /// Creates a LocalDateTime from the UTC time given in dateTime,
  75. /// using the time zone differential of the current time zone.
  76. //@ deprecated
  77. LocalDateTime(int tzd, const DateTime& dateTime);
  78. /// Creates a LocalDateTime from the UTC time given in dateTime,
  79. /// using the given time zone differential. Adjusts dateTime
  80. /// for the given time zone differential.
  81. //@ deprecated
  82. LocalDateTime(int tzd, const DateTime& dateTime, bool adjust);
  83. /// Creates a LocalDateTime from the UTC time given in dateTime,
  84. /// using the given time zone differential. If adjust is true,
  85. /// adjusts dateTime for the given time zone differential.
  86. LocalDateTime(double julianDay);
  87. /// Creates a LocalDateTime for the given Julian day in the local time zone.
  88. //@ deprecated
  89. LocalDateTime(int tzd, double julianDay);
  90. /// Creates a LocalDateTime for the given Julian day in the time zone
  91. /// denoted by the time zone differential in tzd.
  92. LocalDateTime(const LocalDateTime& dateTime);
  93. /// Copy constructor. Creates the LocalDateTime from another one.
  94. ~LocalDateTime();
  95. /// Destroys the LocalDateTime.
  96. LocalDateTime& operator = (const LocalDateTime& dateTime);
  97. /// Assigns another LocalDateTime.
  98. LocalDateTime& operator = (const Timestamp& timestamp);
  99. /// Assigns a timestamp
  100. LocalDateTime& operator = (double julianDay);
  101. /// Assigns a Julian day in the local time zone.
  102. LocalDateTime& assign(int year, int month, int day, int hour = 0, int minute = 0, int second = 0, int millisecond = 0, int microseconds = 0);
  103. /// Assigns a Gregorian local date and time.
  104. /// * year is from 0 to 9999.
  105. /// * month is from 1 to 12.
  106. /// * day is from 1 to 31.
  107. /// * hour is from 0 to 23.
  108. /// * minute is from 0 to 59.
  109. /// * second is from 0 to 60 (allowing leap seconds).
  110. /// * millisecond is from 0 to 999.
  111. /// * microsecond is from 0 to 999.
  112. //@ deprecated
  113. LocalDateTime& assign(int tzd, int year, int month, int day, int hour, int minute, int second, int millisecond, int microseconds);
  114. /// Assigns a Gregorian local date and time in the time zone denoted by
  115. /// the time zone differential in tzd.
  116. /// * tzd is in seconds.
  117. /// * year is from 0 to 9999.
  118. /// * month is from 1 to 12.
  119. /// * day is from 1 to 31.
  120. /// * hour is from 0 to 23.
  121. /// * minute is from 0 to 59.
  122. /// * second is from 0 to 60 (allowing leap seconds).
  123. /// * millisecond is from 0 to 999.
  124. /// * microsecond is from 0 to 999.
  125. //@ deprecated
  126. LocalDateTime& assign(int tzd, double julianDay);
  127. /// Assigns a Julian day in the time zone denoted by the
  128. /// time zone differential in tzd.
  129. void swap(LocalDateTime& dateTime);
  130. /// Swaps the LocalDateTime with another one.
  131. int year() const;
  132. /// Returns the year.
  133. int month() const;
  134. /// Returns the month (1 to 12).
  135. int week(int firstDayOfWeek = DateTime::MONDAY) const;
  136. /// Returns the week number within the year.
  137. /// FirstDayOfWeek should be either SUNDAY (0) or MONDAY (1).
  138. /// The returned week number will be from 0 to 53. Week number 1 is the week
  139. /// containing January 4. This is in accordance to ISO 8601.
  140. ///
  141. /// The following example assumes that firstDayOfWeek is MONDAY. For 2005, which started
  142. /// on a Saturday, week 1 will be the week starting on Monday, January 3.
  143. /// January 1 and 2 will fall within week 0 (or the last week of the previous year).
  144. ///
  145. /// For 2007, which starts on a Monday, week 1 will be the week startung on Monday, January 1.
  146. /// There will be no week 0 in 2007.
  147. int day() const;
  148. /// Returns the day witin the month (1 to 31).
  149. int dayOfWeek() const;
  150. /// Returns the weekday (0 to 6, where
  151. /// 0 = Sunday, 1 = Monday, ..., 6 = Saturday).
  152. int dayOfYear() const;
  153. /// Returns the number of the day in the year.
  154. /// January 1 is 1, February 1 is 32, etc.
  155. int hour() const;
  156. /// Returns the hour (0 to 23).
  157. int hourAMPM() const;
  158. /// Returns the hour (0 to 12).
  159. bool isAM() const;
  160. /// Returns true if hour < 12;
  161. bool isPM() const;
  162. /// Returns true if hour >= 12.
  163. int minute() const;
  164. /// Returns the minute (0 to 59).
  165. int second() const;
  166. /// Returns the second (0 to 59).
  167. int millisecond() const;
  168. /// Returns the millisecond (0 to 999)
  169. int microsecond() const;
  170. /// Returns the microsecond (0 to 999)
  171. double julianDay() const;
  172. /// Returns the julian day for the date.
  173. int tzd() const;
  174. /// Returns the time zone differential.
  175. DateTime utc() const;
  176. /// Returns the UTC equivalent for the local date and time.
  177. Timestamp timestamp() const;
  178. /// Returns the date and time expressed as a Timestamp.
  179. Timestamp::UtcTimeVal utcTime() const;
  180. /// Returns the UTC equivalent for the local date and time.
  181. bool operator == (const LocalDateTime& dateTime) const;
  182. bool operator != (const LocalDateTime& dateTime) const;
  183. bool operator < (const LocalDateTime& dateTime) const;
  184. bool operator <= (const LocalDateTime& dateTime) const;
  185. bool operator > (const LocalDateTime& dateTime) const;
  186. bool operator >= (const LocalDateTime& dateTime) const;
  187. LocalDateTime operator + (const Timespan& span) const;
  188. LocalDateTime operator - (const Timespan& span) const;
  189. Timespan operator - (const LocalDateTime& dateTime) const;
  190. LocalDateTime& operator += (const Timespan& span);
  191. LocalDateTime& operator -= (const Timespan& span);
  192. protected:
  193. LocalDateTime(Timestamp::UtcTimeVal utcTime, Timestamp::TimeDiff diff, int tzd);
  194. void determineTzd(bool adjust = false);
  195. /// Recalculate the tzd based on the _dateTime member based
  196. /// on the current timezone using the Standard C runtime functions.
  197. /// If adjust is true, then adjustForTzd() is called after the
  198. /// differential is calculated.
  199. void adjustForTzd();
  200. /// Adjust the _dateTime member based on the _tzd member.
  201. std::time_t dstOffset(int& dstOffset) const;
  202. /// Determine the DST offset for the current date/time.
  203. private:
  204. DateTime _dateTime;
  205. int _tzd;
  206. friend class DateTimeFormatter;
  207. friend class DateTimeParser;
  208. };
  209. //
  210. // inlines
  211. //
  212. inline int LocalDateTime::year() const
  213. {
  214. return _dateTime.year();
  215. }
  216. inline int LocalDateTime::month() const
  217. {
  218. return _dateTime.month();
  219. }
  220. inline int LocalDateTime::week(int firstDayOfWeek) const
  221. {
  222. return _dateTime.week(firstDayOfWeek);
  223. }
  224. inline int LocalDateTime::day() const
  225. {
  226. return _dateTime.day();
  227. }
  228. inline int LocalDateTime::dayOfWeek() const
  229. {
  230. return _dateTime.dayOfWeek();
  231. }
  232. inline int LocalDateTime::dayOfYear() const
  233. {
  234. return _dateTime.dayOfYear();
  235. }
  236. inline int LocalDateTime::hour() const
  237. {
  238. return _dateTime.hour();
  239. }
  240. inline int LocalDateTime::hourAMPM() const
  241. {
  242. return _dateTime.hourAMPM();
  243. }
  244. inline bool LocalDateTime::isAM() const
  245. {
  246. return _dateTime.isAM();
  247. }
  248. inline bool LocalDateTime::isPM() const
  249. {
  250. return _dateTime.isPM();
  251. }
  252. inline int LocalDateTime::minute() const
  253. {
  254. return _dateTime.minute();
  255. }
  256. inline int LocalDateTime::second() const
  257. {
  258. return _dateTime.second();
  259. }
  260. inline int LocalDateTime::millisecond() const
  261. {
  262. return _dateTime.millisecond();
  263. }
  264. inline int LocalDateTime::microsecond() const
  265. {
  266. return _dateTime.microsecond();
  267. }
  268. inline double LocalDateTime::julianDay() const
  269. {
  270. return _dateTime.julianDay();
  271. }
  272. inline int LocalDateTime::tzd() const
  273. {
  274. return _tzd;
  275. }
  276. inline Timestamp LocalDateTime::timestamp() const
  277. {
  278. return Timestamp::fromUtcTime(_dateTime.utcTime());
  279. }
  280. inline Timestamp::UtcTimeVal LocalDateTime::utcTime() const
  281. {
  282. return _dateTime.utcTime() - ((Timestamp::TimeDiff) _tzd)*10000000;
  283. }
  284. inline void LocalDateTime::adjustForTzd()
  285. {
  286. _dateTime += Timespan(((Timestamp::TimeDiff) _tzd)*Timespan::SECONDS);
  287. }
  288. inline void swap(LocalDateTime& d1, LocalDateTime& d2)
  289. {
  290. d1.swap(d2);
  291. }
  292. } // namespace Poco
  293. #endif // Foundation_LocalDateTime_INCLUDED