date.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. //
  2. // Copyright (c) 2019-2024 Ruben Perez Hidalgo (rubenperez038 at gmail dot com)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. #ifndef BOOST_MYSQL_DATE_HPP
  8. #define BOOST_MYSQL_DATE_HPP
  9. #include <boost/mysql/days.hpp>
  10. #include <boost/mysql/detail/config.hpp>
  11. #include <boost/mysql/detail/datetime.hpp>
  12. #include <boost/assert.hpp>
  13. #include <boost/config.hpp>
  14. #include <boost/throw_exception.hpp>
  15. #include <chrono>
  16. #include <cstdint>
  17. #include <iosfwd>
  18. #include <stdexcept>
  19. namespace boost {
  20. namespace mysql {
  21. /**
  22. * \brief Type representing MySQL `DATE` data type.
  23. * \details
  24. * Represents a Gregorian date broken by its year, month and day components, without a time zone.
  25. * \n
  26. * This type is close to the protocol and should not be used as a vocabulary type.
  27. * Instead, cast it to a `std::chrono::time_point` by calling \ref as_time_point,
  28. * \ref get_time_point, \ref as_local_time_point or \ref get_local_time_point.
  29. * \n
  30. * Dates retrieved from MySQL don't include any time zone information. Determining the time zone
  31. * is left to the application. Thus, any time point obtained from this class should be
  32. * interpreted as a local time in an unspecified time zone, like `std::chrono::local_time`.
  33. * For compatibility with older compilers, \ref as_time_point and \ref get_time_point return
  34. * `system_clock` time points. These should be interpreted as local times rather
  35. * than UTC. Prefer using \ref as_local_time_point or \ref get_local_time_point
  36. * if your compiler supports them, as they provide more accurate semantics.
  37. * \n
  38. * As opposed to `time_point`, this type allows representing MySQL invalid and zero dates.
  39. * These values are allowed by MySQL but don't represent real dates.
  40. * \n
  41. * Note: using `std::chrono` time zone functionality under MSVC may cause memory leaks to be reported.
  42. * See <a href="https://github.com/microsoft/STL/issues/2047">this issue</a> for an explanation and
  43. * <a href="https://github.com/microsoft/STL/issues/2504">this other issue</a> for a workaround.
  44. */
  45. class date
  46. {
  47. public:
  48. /**
  49. * \brief A `std::chrono::time_point` that can represent any valid `date`.
  50. * \details
  51. * Time points used by this class are always local times, even if defined
  52. * to use the system clock.
  53. */
  54. using time_point = std::chrono::time_point<std::chrono::system_clock, days>;
  55. /**
  56. * \brief Constructs a zero date.
  57. * \details
  58. * Results in a date with all of its components set to zero.
  59. * The resulting object has `this->valid() == false`.
  60. *
  61. * \par Exception safety
  62. * No-throw guarantee.
  63. */
  64. constexpr date() noexcept = default;
  65. /**
  66. * \brief Constructs a date from its year, month and date components.
  67. * \details
  68. * Component values that yield invalid dates (like zero or out-of-range
  69. * values) are allowed, resulting in an object with `this->valid() == false`.
  70. *
  71. * \par Exception safety
  72. * No-throw guarantee.
  73. */
  74. constexpr date(std::uint16_t year, std::uint8_t month, std::uint8_t day) noexcept
  75. : year_(year), month_(month), day_(day)
  76. {
  77. }
  78. /**
  79. * \brief Constructs a date from a `time_point`.
  80. * \details
  81. * The time point is interpreted as a local time. No time zone conversion is performed.
  82. *
  83. * \par Exception safety
  84. * Strong guarantee. Throws on invalid input.
  85. * \throws std::out_of_range If the resulting `date` would be
  86. * out of the [\ref min_date, \ref max_date] range.
  87. */
  88. BOOST_CXX14_CONSTEXPR explicit date(time_point tp)
  89. {
  90. bool ok = detail::days_to_ymd(tp.time_since_epoch().count(), year_, month_, day_);
  91. if (!ok)
  92. BOOST_THROW_EXCEPTION(std::out_of_range("date::date: time_point was out of range"));
  93. }
  94. #ifdef BOOST_MYSQL_HAS_LOCAL_TIME
  95. /**
  96. * \brief Constructs a date from a local time point.
  97. * \details
  98. * Equivalent to constructing a `date` from a `time_point` with the same
  99. * `time_since_epoch()` as `tp`.
  100. * \n
  101. * Requires C++20 calendar types.
  102. *
  103. * \par Exception safety
  104. * Strong guarantee. Throws on invalid input.
  105. * \throws std::out_of_range If the resulting `date` would be
  106. * out of the [\ref min_date, \ref max_date] range.
  107. */
  108. constexpr explicit date(std::chrono::local_days tp) : date(time_point(tp.time_since_epoch())) {}
  109. #endif
  110. /**
  111. * \brief Retrieves the year component.
  112. * \details
  113. * Represents the year number in the Gregorian calendar.
  114. * If `this->valid() == true`, this value is within the `[0, 9999]` range.
  115. *
  116. * \par Exception safety
  117. * No-throw guarantee.
  118. */
  119. constexpr std::uint16_t year() const noexcept { return year_; }
  120. /**
  121. * \brief Retrieves the month component (1-based).
  122. * \details
  123. * A value of 1 represents January.
  124. * If `this->valid() == true`, this value is within the `[1, 12]` range.
  125. *
  126. * \par Exception safety
  127. * No-throw guarantee.
  128. */
  129. constexpr std::uint8_t month() const noexcept { return month_; }
  130. /**
  131. * \brief Retrieves the day component (1-based).
  132. * \details
  133. * A value of 1 represents the first day of the month.
  134. * If `this->valid() == true`, this value is within the `[1, last_month_day]` range
  135. * (where `last_month_day` is the last day of the month).
  136. *
  137. * \par Exception safety
  138. * No-throw guarantee.
  139. */
  140. constexpr std::uint8_t day() const noexcept { return day_; }
  141. /**
  142. * \brief Returns `true` if `*this` represents a valid `time_point`.
  143. * \details If any of the individual components is out of range, the date
  144. * doesn't represent an actual `time_point` (e.g. `date(2020, 2, 30)`) or
  145. * the date is not in the [\ref min_date, \ref max_date] validity range,
  146. * returns `false`. Otherwise, returns `true`.
  147. * \par Exception safety
  148. * No-throw guarantee.
  149. */
  150. constexpr bool valid() const noexcept { return detail::is_valid(year_, month_, day_); }
  151. /**
  152. * \brief Converts `*this` into a `time_point` (unchecked access).
  153. * \details
  154. * If your compiler supports it, prefer using \ref get_local_time_point,
  155. * as it provides more accurate semantics.
  156. *
  157. * \par Preconditions
  158. * `this->valid() == true` (if violated, results in undefined behavior).
  159. *
  160. * \par Exception safety
  161. * No-throw guarantee.
  162. */
  163. BOOST_CXX14_CONSTEXPR time_point get_time_point() const noexcept
  164. {
  165. BOOST_ASSERT(valid());
  166. return time_point(unch_get_days());
  167. }
  168. /**
  169. * \brief Converts `*this` into a `time_point` (checked access).
  170. * \details
  171. * If your compiler supports it, prefer using \ref as_local_time_point,
  172. * as it provides more accurate semantics.
  173. *
  174. * \par Exception safety
  175. * Strong guarantee.
  176. * \throws std::invalid_argument If `!this->valid()`.
  177. */
  178. BOOST_CXX14_CONSTEXPR time_point as_time_point() const
  179. {
  180. if (!valid())
  181. BOOST_THROW_EXCEPTION(std::invalid_argument("date::as_time_point: invalid date"));
  182. return time_point(unch_get_days());
  183. }
  184. #ifdef BOOST_MYSQL_HAS_LOCAL_TIME
  185. /**
  186. * \brief Converts `*this` into a local time point (unchecked access).
  187. * \details
  188. * The returned object has the same `time_since_epoch()` as `this->get_time_point()`,
  189. * but uses the `std::chrono::local_t` pseudo-clock to better represent
  190. * the absence of time zone information.
  191. * \n
  192. * Requires C++20 calendar types.
  193. *
  194. * \par Preconditions
  195. * `this->valid() == true` (if violated, results in undefined behavior).
  196. *
  197. * \par Exception safety
  198. * No-throw guarantee.
  199. */
  200. constexpr std::chrono::local_days get_local_time_point() const noexcept
  201. {
  202. BOOST_ASSERT(valid());
  203. return std::chrono::local_days(unch_get_days());
  204. }
  205. /**
  206. * \brief Converts `*this` into a local time point (checked access).
  207. * \details
  208. * The returned object has the same `time_since_epoch()` as `this->as_time_point()`,
  209. * but uses the `std::chrono::local_t` pseudo-clock to better represent
  210. * the absence of time zone information.
  211. * \n
  212. * Requires C++20 calendar types.
  213. *
  214. * \par Exception safety
  215. * Strong guarantee.
  216. * \throws std::invalid_argument If `!this->valid()`.
  217. */
  218. constexpr std::chrono::local_days as_local_time_point() const
  219. {
  220. if (!valid())
  221. BOOST_THROW_EXCEPTION(std::invalid_argument("date::as_local_time_point: invalid date"));
  222. return std::chrono::local_days(unch_get_days());
  223. }
  224. #endif
  225. /**
  226. * \brief Tests for equality.
  227. * \details Two dates are considered equal if all of its individual components
  228. * are equal. This function works for invalid dates, too.
  229. *
  230. * \par Exception safety
  231. * No-throw guarantee.
  232. */
  233. constexpr bool operator==(const date& rhs) const noexcept
  234. {
  235. return year_ == rhs.year_ && month_ == rhs.month_ && day_ == rhs.day_;
  236. }
  237. /**
  238. * \brief Tests for inequality.
  239. *
  240. * \par Exception safety
  241. * No-throw guarantee.
  242. */
  243. constexpr bool operator!=(const date& rhs) const noexcept { return !(rhs == *this); }
  244. /**
  245. * \brief Returns the current system time as a date object.
  246. * \par Exception safety
  247. * Strong guarantee. Only throws if obtaining the current time throws.
  248. */
  249. static date now()
  250. {
  251. auto now = time_point::clock::now();
  252. return date(std::chrono::time_point_cast<time_point::duration>(now));
  253. }
  254. private:
  255. std::uint16_t year_{};
  256. std::uint8_t month_{};
  257. std::uint8_t day_{};
  258. BOOST_CXX14_CONSTEXPR days unch_get_days() const
  259. {
  260. return days(detail::ymd_to_days(year_, month_, day_));
  261. }
  262. };
  263. /**
  264. * \relates date
  265. * \brief Streams a date.
  266. * \details This function works for invalid dates, too.
  267. */
  268. BOOST_MYSQL_DECL
  269. std::ostream& operator<<(std::ostream& os, const date& v);
  270. /// The minimum allowed value for \ref date.
  271. BOOST_INLINE_CONSTEXPR date min_date{0u, 1u, 1u};
  272. /// The maximum allowed value for \ref date.
  273. BOOST_INLINE_CONSTEXPR date max_date{9999u, 12u, 31u};
  274. } // namespace mysql
  275. } // namespace boost
  276. #ifdef BOOST_MYSQL_HEADER_ONLY
  277. #include <boost/mysql/impl/date.ipp>
  278. #endif
  279. #endif