Mercurial > minori
comparison src/core/date.cc @ 197:c4ca035c565d
*: misc. patches
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Fri, 08 Dec 2023 11:19:54 -0500 |
parents | f0ff06a45c42 |
children | bc1ae1810855 |
comparison
equal
deleted
inserted
replaced
196:f0ff06a45c42 | 197:c4ca035c565d |
---|---|
5 #include <QDebug> | 5 #include <QDebug> |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 /* An implementation of AniList's "fuzzy date" */ | 9 /* An implementation of AniList's "fuzzy date" */ |
10 | |
11 template<typename T> | |
12 bool CLAMP(T x, T low, T high) { | |
13 return std::max(low, std::min(high, x)); | |
14 } | |
15 | 10 |
16 Date::Date() { | 11 Date::Date() { |
17 } | 12 } |
18 | 13 |
19 Date::Date(unsigned int y) { | 14 Date::Date(unsigned int y) { |
55 void Date::VoidDay() { | 50 void Date::VoidDay() { |
56 day.reset(); | 51 day.reset(); |
57 } | 52 } |
58 | 53 |
59 void Date::SetYear(unsigned int y) { | 54 void Date::SetYear(unsigned int y) { |
60 year = y; | 55 year.emplace(y); |
61 } | 56 } |
62 | 57 |
63 void Date::SetMonth(unsigned int m) { | 58 void Date::SetMonth(unsigned int m) { |
64 month = CLAMP(m, 1U, 12U); | 59 month.emplace(std::clamp(m, 1U, 12U)); |
65 } | 60 } |
66 | 61 |
67 void Date::SetDay(unsigned int d) { | 62 void Date::SetDay(unsigned int d) { |
68 day = CLAMP(d, 1U, 31U); | 63 day.emplace(std::clamp(d, 1U, 31U)); |
69 } | 64 } |
70 | 65 |
71 unsigned int Date::GetYear() const { | 66 std::optional<unsigned int> Date::GetYear() const { |
72 return year.value_or(-1); | 67 return year; |
73 } | 68 } |
74 | 69 |
75 unsigned int Date::GetMonth() const { | 70 std::optional<unsigned int> Date::GetMonth() const { |
76 return month.value_or(-1); | 71 return month; |
77 } | 72 } |
78 | 73 |
79 unsigned int Date::GetDay() const { | 74 std::optional<unsigned int> Date::GetDay() const { |
80 return day.value_or(-1); | 75 return day; |
81 } | 76 } |
82 | 77 |
83 bool Date::IsValid() const { | 78 bool Date::IsValid() const { |
84 return year.has_value() && month.has_value() && day.has_value(); | 79 return year.has_value() && month.has_value() && day.has_value(); |
85 } | |
86 | |
87 bool Date::operator<(const Date& other) const { | |
88 const unsigned int y = GetYear(), m = GetMonth(), d = GetDay(); | |
89 const unsigned int o_y = other.GetYear(), o_m = other.GetMonth(), o_d = other.GetDay(); | |
90 | |
91 return (y < o_y && m < o_m && d < o_d); | |
92 } | |
93 | |
94 bool Date::operator>(const Date& other) const { | |
95 return other < (*this); | |
96 } | |
97 | |
98 bool Date::operator<=(const Date& other) const { | |
99 return !((*this) > other); | |
100 } | |
101 | |
102 bool Date::operator>=(const Date& other) const { | |
103 return !((*this) < other); | |
104 } | 80 } |
105 | 81 |
106 QDate Date::GetAsQDate() const { | 82 QDate Date::GetAsQDate() const { |
107 /* QDate doesn't support "missing" values (for good reason), | 83 /* QDate doesn't support "missing" values (for good reason), |
108 * so we do our best and return what we can. | 84 * so we do our best and return what we can. |