Mercurial > minori
comparison src/core/date.cc @ 258:862d0d8619f6
*: HUUUGE changes
animia has been renamed to animone, so instead of thinking of a
health condition, you think of a beautiful flower :)
I've also edited some of the code for animone, but I have no idea
if it even works or not because I don't have a mac or windows
machine lying around. whoops!
... anyway, all of the changes divergent from Anisthesia are now
licensed under BSD. it's possible that I could even rewrite most
of the code to where I don't even have to keep the MIT license,
but that's thinking too far into the future
I've been slacking off on implementing the anime seasons page,
mostly out of laziness. I think I'd have to create another db file
specifically for the seasons
anyway, this code is being pushed *primarily* because the hard drive
it's on is failing! yay :)
author | Paper <paper@paper.us.eu.org> |
---|---|
date | Mon, 01 Apr 2024 02:43:44 -0400 |
parents | bc1ae1810855 |
children | b1f4d1867ab1 |
comparison
equal
deleted
inserted
replaced
257:699a20c57dc8 | 258:862d0d8619f6 |
---|---|
9 /* An implementation of AniList's "fuzzy date" */ | 9 /* An implementation of AniList's "fuzzy date" */ |
10 | 10 |
11 Date::Date() { | 11 Date::Date() { |
12 } | 12 } |
13 | 13 |
14 Date::Date(unsigned int y) { | 14 Date::Date(Date::Year y) { |
15 SetYear(y); | 15 SetYear(y); |
16 } | 16 } |
17 | 17 |
18 Date::Date(unsigned int y, unsigned int m, unsigned int d) { | 18 Date::Date(Date::Year y, Date::Month m, Date::Day d) { |
19 SetYear(y); | 19 SetYear(y); |
20 SetMonth(m); | 20 SetMonth(m); |
21 SetDay(d); | 21 SetDay(d); |
22 } | 22 } |
23 | 23 |
24 Date::Date(const QDate& date) { | 24 Date::Date(const QDate& date) { |
25 SetYear(date.year()); | 25 SetYear(date.year()); |
26 SetMonth(date.month()); | 26 auto m = date.month(); |
27 m = std::clamp(m, static_cast<decltype(m)>(Date::Month::Jan), static_cast<decltype(m)>(Date::Month::Dec)); | |
28 SetMonth(static_cast<Date::Month>(m)); | |
27 SetDay(date.day()); | 29 SetDay(date.day()); |
28 } | 30 } |
29 | 31 |
30 Date::Date(const nlohmann::json& json) { | 32 Date::Date(const nlohmann::json& json) { |
31 /* NOTE: this constructor is made for use with | 33 /* NOTE: this constructor is made for use with |
32 AniList FuzzyDate-style JSON. In the future, some other | 34 * AniList FuzzyDate-style JSON. In the future, some other |
33 methods may be parsed and whatnot if necessary. */ | 35 * methods may be parsed and whatnot if necessary. */ |
34 | 36 |
35 if (json.contains("/year"_json_pointer) && json.at("/year"_json_pointer).is_number()) | 37 if (json.contains("/year"_json_pointer) && json.at("/year"_json_pointer).is_number()) |
36 SetYear(json.at("/year"_json_pointer).get<unsigned int>()); | 38 SetYear(json.at("/year"_json_pointer).get<unsigned int>()); |
37 if (json.contains("/month"_json_pointer) && json.at("/month"_json_pointer).is_number()) | 39 |
38 SetMonth(json.at("/month"_json_pointer).get<unsigned int>()); | 40 if (json.contains("/month"_json_pointer) && json.at("/month"_json_pointer).is_number()) { |
41 auto m = json.at("/month"_json_pointer).get<unsigned int>(); | |
42 m = std::clamp(m, static_cast<decltype(m)>(Date::Month::Jan), static_cast<decltype(m)>(Date::Month::Dec)); | |
43 SetMonth(static_cast<Date::Month>(m)); | |
44 } | |
45 | |
39 if (json.contains("/day"_json_pointer) && json.at("/day"_json_pointer).is_number()) | 46 if (json.contains("/day"_json_pointer) && json.at("/day"_json_pointer).is_number()) |
40 SetDay(json.at("/day"_json_pointer).get<unsigned int>()); | 47 SetDay(json.at("/day"_json_pointer).get<unsigned char>()); |
41 } | 48 } |
42 | 49 |
43 void Date::VoidYear() { | 50 void Date::VoidYear() { |
44 year.reset(); | 51 year.reset(); |
45 } | 52 } |
50 | 57 |
51 void Date::VoidDay() { | 58 void Date::VoidDay() { |
52 day.reset(); | 59 day.reset(); |
53 } | 60 } |
54 | 61 |
55 void Date::SetYear(unsigned int y) { | 62 void Date::SetYear(Date::Year y) { |
56 year.emplace(y); | 63 year.emplace(y); |
57 } | 64 } |
58 | 65 |
59 void Date::SetMonth(unsigned int m) { | 66 void Date::SetMonth(Date::Month m) { |
60 month.emplace(std::clamp(m, 1U, 12U)); | 67 month.emplace(m); |
61 } | 68 } |
62 | 69 |
63 void Date::SetDay(unsigned int d) { | 70 void Date::SetDay(Date::Day d) { |
64 day.emplace(std::clamp(d, 1U, 31U)); | 71 day.emplace(std::clamp(d, static_cast<Date::Day>(1U), static_cast<Date::Day>(31U))); |
65 } | 72 } |
66 | 73 |
67 std::optional<unsigned int> Date::GetYear() const { | 74 std::optional<Date::Year> Date::GetYear() const { |
68 return year; | 75 return year; |
69 } | 76 } |
70 | 77 |
71 std::optional<unsigned int> Date::GetMonth() const { | 78 std::optional<Date::Month> Date::GetMonth() const { |
72 return month; | 79 return month; |
73 } | 80 } |
74 | 81 |
75 std::optional<unsigned int> Date::GetDay() const { | 82 std::optional<Date::Day> Date::GetDay() const { |
76 return day; | 83 return day; |
77 } | 84 } |
78 | 85 |
79 bool Date::IsValid() const { | 86 bool Date::IsValid() const { |
80 return year.has_value() && month.has_value() && day.has_value(); | 87 return year.has_value() && month.has_value() && day.has_value(); |
81 } | 88 } |
82 | 89 |
83 QDate Date::GetAsQDate() const { | 90 QDate Date::GetAsQDate() const { |
84 /* QDate doesn't support "missing" values (for good reason), | 91 /* QDate doesn't support "missing" values (for good reason), |
85 * so we do our best and return what we can. | 92 * so we do our best and return what we can. |
86 */ | 93 */ |
87 | 94 |
88 return QDate(year.value_or(2000), month.value_or(1), day.value_or(1)); | 95 return QDate(year.value_or(2000), static_cast<unsigned int>(month.value_or(Date::Month::Jan)), day.value_or(1)); |
89 } | 96 } |
90 | 97 |
91 nlohmann::json Date::GetAsAniListJson() const { | 98 nlohmann::json Date::GetAsAniListJson() const { |
92 return { | 99 nlohmann::json json = { |
93 {"year", year}, | 100 {"year", nullptr}, |
94 {"month", month}, | 101 {"month", nullptr}, |
95 {"day", day} | 102 {"day", nullptr} |
96 }; | 103 }; |
104 | |
105 if (year) | |
106 json["year"] = static_cast<unsigned int>(year.value()); | |
107 | |
108 if (month) | |
109 json["month"] = static_cast<unsigned char>(month.value()); | |
110 | |
111 if (day) | |
112 json["day"] = static_cast<unsigned char>(day.value()); | |
113 | |
114 return json; | |
97 } | 115 } |