Mercurial > minori
comparison src/core/date.cc @ 196:f0ff06a45c42
date: use std::optional for values
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Thu, 07 Dec 2023 16:28:11 -0500 |
parents | 9613d72b097e |
children | c4ca035c565d |
comparison
equal
deleted
inserted
replaced
195:975a3f0965e2 | 196:f0ff06a45c42 |
---|---|
43 if (json.contains("day") && json.at("day").is_number()) | 43 if (json.contains("day") && json.at("day").is_number()) |
44 SetDay(json.at("day").get<unsigned int>()); | 44 SetDay(json.at("day").get<unsigned int>()); |
45 } | 45 } |
46 | 46 |
47 void Date::VoidYear() { | 47 void Date::VoidYear() { |
48 year.Void(); | 48 year.reset(); |
49 } | 49 } |
50 | 50 |
51 void Date::VoidMonth() { | 51 void Date::VoidMonth() { |
52 month.Void(); | 52 month.reset(); |
53 } | 53 } |
54 | 54 |
55 void Date::VoidDay() { | 55 void Date::VoidDay() { |
56 day.Void(); | 56 day.reset(); |
57 } | 57 } |
58 | 58 |
59 void Date::SetYear(unsigned int y) { | 59 void Date::SetYear(unsigned int y) { |
60 year.Set(y); | 60 year = y; |
61 } | 61 } |
62 | 62 |
63 void Date::SetMonth(unsigned int m) { | 63 void Date::SetMonth(unsigned int m) { |
64 month.Set(CLAMP(m, 1U, 12U)); | 64 month = CLAMP(m, 1U, 12U); |
65 } | 65 } |
66 | 66 |
67 void Date::SetDay(unsigned int d) { | 67 void Date::SetDay(unsigned int d) { |
68 day.Set(CLAMP(d, 1U, 31U)); | 68 day = CLAMP(d, 1U, 31U); |
69 } | 69 } |
70 | 70 |
71 unsigned int Date::GetYear() const { | 71 unsigned int Date::GetYear() const { |
72 return year.Get(); | 72 return year.value_or(-1); |
73 } | 73 } |
74 | 74 |
75 unsigned int Date::GetMonth() const { | 75 unsigned int Date::GetMonth() const { |
76 return month.Get(); | 76 return month.value_or(-1); |
77 } | 77 } |
78 | 78 |
79 unsigned int Date::GetDay() const { | 79 unsigned int Date::GetDay() const { |
80 return day.Get(); | 80 return day.value_or(-1); |
81 } | 81 } |
82 | 82 |
83 bool Date::IsValid() const { | 83 bool Date::IsValid() const { |
84 return year.Enabled() && month.Enabled() && day.Enabled(); | 84 return year.has_value() && month.has_value() && day.has_value(); |
85 } | 85 } |
86 | 86 |
87 bool Date::operator<(const Date& other) const { | 87 bool Date::operator<(const Date& other) const { |
88 const unsigned int y = GetYear(), m = GetMonth(), d = GetDay(); | 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(); | 89 const unsigned int o_y = other.GetYear(), o_m = other.GetMonth(), o_d = other.GetDay(); |
106 QDate Date::GetAsQDate() const { | 106 QDate Date::GetAsQDate() const { |
107 /* QDate doesn't support "missing" values (for good reason), | 107 /* QDate doesn't support "missing" values (for good reason), |
108 * so we do our best and return what we can. | 108 * so we do our best and return what we can. |
109 */ | 109 */ |
110 | 110 |
111 return QDate(year.Enabled() ? year.Get() : 2000, month.Enabled() ? month.Get() : 1, day.Enabled() ? day.Get() : 1); | 111 return QDate(year.value_or(2000), month.value_or(1), day.value_or(1)); |
112 } | 112 } |
113 | 113 |
114 nlohmann::json Date::GetAsAniListJson() const { | 114 nlohmann::json Date::GetAsAniListJson() const { |
115 nlohmann::json result = {}; | 115 nlohmann::json result = {}; |
116 | 116 |
117 if (year.Enabled()) | 117 if (year.has_value()) |
118 result["year"] = year.Get(); | 118 result["year"] = year.value(); |
119 else | 119 else |
120 result["year"] = nullptr; | 120 result["year"] = nullptr; |
121 | 121 |
122 if (month.Enabled()) | 122 if (month.has_value()) |
123 result["month"] = month.Get(); | 123 result["month"] = month.value(); |
124 else | 124 else |
125 result["month"] = nullptr; | 125 result["month"] = nullptr; |
126 | 126 |
127 if (day.Enabled()) | 127 if (day.has_value()) |
128 result["day"] = day.Get(); | 128 result["day"] = day.value(); |
129 else | 129 else |
130 result["day"] = nullptr; | 130 result["day"] = nullptr; |
131 | 131 |
132 return result; | 132 return result; |
133 } | 133 } |