date.hpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. //
  2. // Copyright (c) 2019-2023 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 BHO_MYSQL_DATE_HPP
  8. #define BHO_MYSQL_DATE_HPP
  9. #include <asio2/bho/mysql/days.hpp>
  10. #include <asio2/bho/mysql/detail/config.hpp>
  11. #include <asio2/bho/mysql/detail/datetime.hpp>
  12. #include <asio2/bho/assert.hpp>
  13. #include <asio2/bho/config.hpp>
  14. #include <asio2/bho/throw_exception.hpp>
  15. #include <chrono>
  16. #include <cstdint>
  17. #include <iosfwd>
  18. #include <stdexcept>
  19. namespace bho {
  20. namespace mysql {
  21. /**
  22. * \brief Type representing MySQL `DATE` data type.
  23. * \details Represents a broken date by its year, month and day components.
  24. * This type is close to the protocol and should not be used as a vocabulary type.
  25. * Instead, cast it to a `std::chrono::time_point` by calling \ref as_time_point
  26. * or \ref get_time_point.
  27. * \n
  28. * As opposed to `time_point`, this type allows representing invalid and zero dates.
  29. */
  30. class date
  31. {
  32. public:
  33. /// A `std::chrono::time_point` that can represent any valid `date`.
  34. using time_point = std::chrono::time_point<std::chrono::system_clock, days>;
  35. /**
  36. * \brief Constructs a zero date.
  37. * \details
  38. * Results in a date with all of its components set to zero.
  39. *
  40. * \par Exception safety
  41. * No-throw guarantee.
  42. */
  43. constexpr date() noexcept = default;
  44. /**
  45. * \brief Constructs a date from its year, month and date components.
  46. * \par Exception safety
  47. * No-throw guarantee.
  48. */
  49. constexpr date(std::uint16_t year, std::uint8_t month, std::uint8_t day) noexcept
  50. : year_(year), month_(month), day_(day)
  51. {
  52. }
  53. /**
  54. * \brief Constructs a date from a `time_point`.
  55. * \par Exception safety
  56. * Strong guarantee. Throws on invalid input.
  57. * \throws std::out_of_range If the resulting `date` would be
  58. * out of the [\ref min_date, \ref max_date] range.
  59. */
  60. BHO_CXX14_CONSTEXPR explicit date(time_point tp)
  61. {
  62. bool ok = detail::days_to_ymd(tp.time_since_epoch().count(), year_, month_, day_);
  63. if (!ok)
  64. BHO_THROW_EXCEPTION(std::out_of_range("date::date: time_point was out of range"));
  65. }
  66. /**
  67. * \brief Retrieves the year component.
  68. * \par Exception safety
  69. * No-throw guarantee.
  70. */
  71. constexpr std::uint16_t year() const noexcept { return year_; }
  72. /**
  73. * \brief Retrieves the month component.
  74. * \par Exception safety
  75. * No-throw guarantee.
  76. */
  77. constexpr std::uint8_t month() const noexcept { return month_; }
  78. /**
  79. * \brief Retrieves the day component.
  80. * \par Exception safety
  81. * No-throw guarantee.
  82. */
  83. constexpr std::uint8_t day() const noexcept { return day_; }
  84. /**
  85. * \brief Returns `true` if `*this` represents a valid `time_point`.
  86. * \details If any of the individual components is out of range, the date
  87. * doesn't represent an actual `time_point` (e.g. `date(2020, 2, 30)`) or
  88. * the date is not in the [\ref min_date, \ref max_date] validity range,
  89. * returns `false`. Otherwise, returns `true`.
  90. * \par Exception safety
  91. * No-throw guarantee.
  92. */
  93. constexpr bool valid() const noexcept { return detail::is_valid(year_, month_, day_); }
  94. /**
  95. * \brief Converts `*this` into a `time_point` (unchecked access).
  96. * \par Preconditions
  97. * `this->valid() == true` (if violated, results in undefined behavior).
  98. *
  99. * \par Exception safety
  100. * No-throw guarantee.
  101. */
  102. BHO_CXX14_CONSTEXPR time_point get_time_point() const noexcept
  103. {
  104. BHO_ASSERT(valid());
  105. return unch_get_time_point();
  106. }
  107. /**
  108. * \brief Converts `*this` into a `time_point` (checked access).
  109. * \par Exception safety
  110. * Strong guarantee.
  111. * \throws std::invalid_argument If `!this->valid()`.
  112. */
  113. BHO_CXX14_CONSTEXPR time_point as_time_point() const
  114. {
  115. if (!valid())
  116. BHO_THROW_EXCEPTION(std::invalid_argument("date::as_time_point: invalid date"));
  117. return unch_get_time_point();
  118. }
  119. /**
  120. * \brief Tests for equality.
  121. * \details Two dates are considered equal if all of its individual components
  122. * are equal. This function works for invalid dates, too.
  123. *
  124. * \par Exception safety
  125. * No-throw guarantee.
  126. */
  127. constexpr bool operator==(const date& rhs) const noexcept
  128. {
  129. return year_ == rhs.year_ && month_ == rhs.month_ && day_ == rhs.day_;
  130. }
  131. /**
  132. * \brief Tests for inequality.
  133. *
  134. * \par Exception safety
  135. * No-throw guarantee.
  136. */
  137. constexpr bool operator!=(const date& rhs) const noexcept { return !(rhs == *this); }
  138. /**
  139. * \brief Returns the current system time as a date object.
  140. * \par Exception safety
  141. * Strong guarantee. Only throws if obtaining the current time throws.
  142. */
  143. static date now()
  144. {
  145. auto now = time_point::clock::now();
  146. return date(std::chrono::time_point_cast<time_point::duration>(now));
  147. }
  148. private:
  149. std::uint16_t year_{};
  150. std::uint8_t month_{};
  151. std::uint8_t day_{};
  152. BHO_CXX14_CONSTEXPR time_point unch_get_time_point() const noexcept
  153. {
  154. return time_point(days(detail::ymd_to_days(year_, month_, day_)));
  155. }
  156. };
  157. /**
  158. * \relates date
  159. * \brief Streams a date.
  160. * \details This function works for invalid dates, too.
  161. */
  162. BHO_MYSQL_DECL
  163. std::ostream& operator<<(std::ostream& os, const date& v);
  164. /// The minimum allowed value for \ref date.
  165. constexpr date min_date{0u, 1u, 1u};
  166. /// The maximum allowed value for \ref date.
  167. constexpr date max_date{9999u, 12u, 31u};
  168. } // namespace mysql
  169. } // namespace bho
  170. #ifdef BHO_MYSQL_HEADER_ONLY
  171. #include <asio2/bho/mysql/impl/date.ipp>
  172. #endif
  173. #endif