123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309 |
- #ifndef BOOST_MYSQL_DATE_HPP
- #define BOOST_MYSQL_DATE_HPP
- #include <boost/mysql/days.hpp>
- #include <boost/mysql/detail/config.hpp>
- #include <boost/mysql/detail/datetime.hpp>
- #include <boost/assert.hpp>
- #include <boost/config.hpp>
- #include <boost/throw_exception.hpp>
- #include <chrono>
- #include <cstdint>
- #include <iosfwd>
- #include <stdexcept>
- namespace boost {
- namespace mysql {
- class date
- {
- public:
-
- using time_point = std::chrono::time_point<std::chrono::system_clock, days>;
-
- constexpr date() noexcept = default;
-
- constexpr date(std::uint16_t year, std::uint8_t month, std::uint8_t day) noexcept
- : year_(year), month_(month), day_(day)
- {
- }
-
- BOOST_CXX14_CONSTEXPR explicit date(time_point tp)
- {
- bool ok = detail::days_to_ymd(tp.time_since_epoch().count(), year_, month_, day_);
- if (!ok)
- BOOST_THROW_EXCEPTION(std::out_of_range("date::date: time_point was out of range"));
- }
- #ifdef BOOST_MYSQL_HAS_LOCAL_TIME
-
- constexpr explicit date(std::chrono::local_days tp) : date(time_point(tp.time_since_epoch())) {}
- #endif
-
- constexpr std::uint16_t year() const noexcept { return year_; }
-
- constexpr std::uint8_t month() const noexcept { return month_; }
-
- constexpr std::uint8_t day() const noexcept { return day_; }
-
- constexpr bool valid() const noexcept { return detail::is_valid(year_, month_, day_); }
-
- BOOST_CXX14_CONSTEXPR time_point get_time_point() const noexcept
- {
- BOOST_ASSERT(valid());
- return time_point(unch_get_days());
- }
-
- BOOST_CXX14_CONSTEXPR time_point as_time_point() const
- {
- if (!valid())
- BOOST_THROW_EXCEPTION(std::invalid_argument("date::as_time_point: invalid date"));
- return time_point(unch_get_days());
- }
- #ifdef BOOST_MYSQL_HAS_LOCAL_TIME
-
- constexpr std::chrono::local_days get_local_time_point() const noexcept
- {
- BOOST_ASSERT(valid());
- return std::chrono::local_days(unch_get_days());
- }
-
- constexpr std::chrono::local_days as_local_time_point() const
- {
- if (!valid())
- BOOST_THROW_EXCEPTION(std::invalid_argument("date::as_local_time_point: invalid date"));
- return std::chrono::local_days(unch_get_days());
- }
- #endif
-
- constexpr bool operator==(const date& rhs) const noexcept
- {
- return year_ == rhs.year_ && month_ == rhs.month_ && day_ == rhs.day_;
- }
-
- constexpr bool operator!=(const date& rhs) const noexcept { return !(rhs == *this); }
-
- static date now()
- {
- auto now = time_point::clock::now();
- return date(std::chrono::time_point_cast<time_point::duration>(now));
- }
- private:
- std::uint16_t year_{};
- std::uint8_t month_{};
- std::uint8_t day_{};
- BOOST_CXX14_CONSTEXPR days unch_get_days() const
- {
- return days(detail::ymd_to_days(year_, month_, day_));
- }
- };
- BOOST_MYSQL_DECL
- std::ostream& operator<<(std::ostream& os, const date& v);
- BOOST_INLINE_CONSTEXPR date min_date{0u, 1u, 1u};
- BOOST_INLINE_CONSTEXPR date max_date{9999u, 12u, 31u};
- }
- }
- #ifdef BOOST_MYSQL_HEADER_ONLY
- #include <boost/mysql/impl/date.ipp>
- #endif
- #endif
|