LocalDateTime.h 11 KB

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