11#include <userver/formats/common/meta.hpp>
13USERVER_NAMESPACE_BEGIN
30 using Days = std::chrono::duration<
long long, std::ratio<86400>>;
31 using SysDays = std::chrono::time_point<std::chrono::system_clock, Days>;
34 Date(
const Date&)
noexcept =
default;
35 Date& operator=(
const Date&)
noexcept =
default;
38 Date(
int year,
int month,
int day);
50 constexpr explicit operator SysDays()
const {
return sys_days_; }
52 constexpr bool operator==(Date other)
const {
return sys_days_ == other.sys_days_; }
53 constexpr bool operator!=(Date other)
const {
return !(*
this == other); }
65template <
typename Value>
66std::enable_if_t<formats::common::kIsFormatValue<Value>, Date> Parse(
const Value& value, formats::parse::To<Date>) {
69 str = value.
template As<std::string>();
70 }
catch (
const std::exception& e) {
71 throw typename Value::ParseException(
"Only strings can be parsed as `utils::datetime::Date`");
75 return utils::datetime::DateFromRFC3339String(str);
76 }
catch (
const std::exception& e) {
77 throw typename Value::ParseException(
"'" + str +
"' cannot be parsed to `utils::datetime::Date`");
81template <
typename Value>
82std::enable_if_t<formats::common::kIsFormatValue<Value>, Value> Serialize(Date date, formats::serialize::To<Value>) {
83 return typename Value::Builder(
ToString(date
)).ExtractValue();
86template <
typename StringBuilder>
87void WriteToStream(Date value, StringBuilder& sw) {
91template <
typename LogHelper = USERVER_NAMESPACE::
logging::LogHelper>
92USERVER_NAMESPACE::
logging::LogHelper& operator<<(USERVER_NAMESPACE::
logging::LogHelper& lh,
const Date& date) {
94 std::is_same_v<LogHelper, USERVER_NAMESPACE::
logging::LogHelper>,
95 "This was made template to work well with forward declared "
98 return static_cast<LogHelper&>(lh) <<
ToString(date
);
101std::ostream& operator<<(std::ostream& os, Date date);