11#include <userver/compiler/impl/three_way_comparison.hpp>
12#include <userver/formats/common/meta.hpp>
14USERVER_NAMESPACE_BEGIN
31 using Days = std::chrono::duration<
long long, std::ratio<86400>>;
32 using SysDays = std::chrono::time_point<std::chrono::system_clock, Days>;
35 Date(
const Date&)
noexcept =
default;
36 Date& operator=(
const Date&)
noexcept =
default;
39 Date(
int year,
int month,
int day);
42 constexpr Date(SysDays tp)
noexcept : sys_days_(tp) {}
48 constexpr SysDays
GetSysDays()
const {
return sys_days_; }
51 constexpr explicit operator SysDays()
const {
return sys_days_; }
53#ifdef USERVER_IMPL_HAS_THREE_WAY_COMPARISON
54 constexpr auto operator<=>(
const Date&)
const =
default;
56 constexpr bool operator==(Date other)
const {
return sys_days_ == other.sys_days_; }
57 constexpr bool operator!=(Date other)
const {
return !(*
this == other); }
58 constexpr bool operator<(Date other)
const {
return sys_days_ < other.sys_days_; }
59 constexpr bool operator<=(Date other)
const {
return sys_days_ <= other.sys_days_; }
60 constexpr bool operator>(Date other)
const {
return sys_days_ > other.sys_days_; }
61 constexpr bool operator>=(Date other)
const {
return sys_days_ >= other.sys_days_; }
74template <
typename Value>
78 str = value.
template As<std::string>();
79 }
catch (
const std::exception& e) {
80 throw typename Value::ParseException(
"Only strings can be parsed as `utils::datetime::Date`");
85 }
catch (
const std::exception& e) {
86 throw typename Value::ParseException(
"'" + str +
"' cannot be parsed to `utils::datetime::Date`");
90template <
typename Value>
92 return typename Value::Builder(
ToString(date
)).ExtractValue();
95template <
typename StringBuilder>
96void WriteToStream(Date value, StringBuilder& sw) {
100template <
typename LogHelper = USERVER_NAMESPACE::
logging::LogHelper>
101USERVER_NAMESPACE::
logging::LogHelper& operator<<(USERVER_NAMESPACE::
logging::LogHelper& lh,
const Date& date) {
103 std::is_same_v<LogHelper, USERVER_NAMESPACE::
logging::LogHelper>,
104 "This was made template to work well with forward declared "
107 return static_cast<LogHelper&>(lh) <<
ToString(date
);
110std::ostream& operator<<(std::ostream& os, Date date);