Mercurial > minori
annotate src/core/date.cc @ 307:8769c5d50b06
pages/anime_list: don't call GUI functions in a non-GUI thread
author | Paper <paper@paper.us.eu.org> |
---|---|
date | Sun, 19 May 2024 18:25:14 -0400 |
parents | 862d0d8619f6 |
children | b1f4d1867ab1 |
rev | line source |
---|---|
9 | 1 #include "core/date.h" |
2 #include "core/json.h" | |
187
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
3 |
9 | 4 #include <QDate> |
51 | 5 #include <QDebug> |
187
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
6 |
63 | 7 #include <algorithm> |
9 | 8 |
9 /* An implementation of AniList's "fuzzy date" */ | |
10 | |
11 Date::Date() { | |
12 } | |
13 | |
258 | 14 Date::Date(Date::Year y) { |
11 | 15 SetYear(y); |
9 | 16 } |
17 | |
258 | 18 Date::Date(Date::Year y, Date::Month m, Date::Day d) { |
11 | 19 SetYear(y); |
20 SetMonth(m); | |
21 SetDay(d); | |
9 | 22 } |
23 | |
51 | 24 Date::Date(const QDate& date) { |
25 SetYear(date.year()); | |
258 | 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)); | |
51 | 29 SetDay(date.day()); |
30 } | |
31 | |
174
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
81
diff
changeset
|
32 Date::Date(const nlohmann::json& json) { |
175 | 33 /* NOTE: this constructor is made for use with |
258 | 34 * AniList FuzzyDate-style JSON. In the future, some other |
35 * methods may be parsed and whatnot if necessary. */ | |
198
bc1ae1810855
dep/animia: switch from using classes to global functions
Paper <mrpapersonic@gmail.com>
parents:
197
diff
changeset
|
36 |
bc1ae1810855
dep/animia: switch from using classes to global functions
Paper <mrpapersonic@gmail.com>
parents:
197
diff
changeset
|
37 if (json.contains("/year"_json_pointer) && json.at("/year"_json_pointer).is_number()) |
bc1ae1810855
dep/animia: switch from using classes to global functions
Paper <mrpapersonic@gmail.com>
parents:
197
diff
changeset
|
38 SetYear(json.at("/year"_json_pointer).get<unsigned int>()); |
258 | 39 |
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 | |
198
bc1ae1810855
dep/animia: switch from using classes to global functions
Paper <mrpapersonic@gmail.com>
parents:
197
diff
changeset
|
46 if (json.contains("/day"_json_pointer) && json.at("/day"_json_pointer).is_number()) |
258 | 47 SetDay(json.at("/day"_json_pointer).get<unsigned char>()); |
174
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
81
diff
changeset
|
48 } |
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
81
diff
changeset
|
49 |
9 | 50 void Date::VoidYear() { |
196
f0ff06a45c42
date: use std::optional for values
Paper <mrpapersonic@gmail.com>
parents:
187
diff
changeset
|
51 year.reset(); |
9 | 52 } |
53 | |
54 void Date::VoidMonth() { | |
196
f0ff06a45c42
date: use std::optional for values
Paper <mrpapersonic@gmail.com>
parents:
187
diff
changeset
|
55 month.reset(); |
9 | 56 } |
57 | |
58 void Date::VoidDay() { | |
196
f0ff06a45c42
date: use std::optional for values
Paper <mrpapersonic@gmail.com>
parents:
187
diff
changeset
|
59 day.reset(); |
9 | 60 } |
61 | |
258 | 62 void Date::SetYear(Date::Year y) { |
197 | 63 year.emplace(y); |
9 | 64 } |
65 | |
258 | 66 void Date::SetMonth(Date::Month m) { |
67 month.emplace(m); | |
9 | 68 } |
69 | |
258 | 70 void Date::SetDay(Date::Day d) { |
71 day.emplace(std::clamp(d, static_cast<Date::Day>(1U), static_cast<Date::Day>(31U))); | |
9 | 72 } |
73 | |
258 | 74 std::optional<Date::Year> Date::GetYear() const { |
197 | 75 return year; |
9 | 76 } |
77 | |
258 | 78 std::optional<Date::Month> Date::GetMonth() const { |
197 | 79 return month; |
9 | 80 } |
81 | |
258 | 82 std::optional<Date::Day> Date::GetDay() const { |
197 | 83 return day; |
9 | 84 } |
85 | |
47
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
86 bool Date::IsValid() const { |
196
f0ff06a45c42
date: use std::optional for values
Paper <mrpapersonic@gmail.com>
parents:
187
diff
changeset
|
87 return year.has_value() && month.has_value() && day.has_value(); |
47
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
88 } |
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
89 |
9 | 90 QDate Date::GetAsQDate() const { |
187
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
91 /* QDate doesn't support "missing" values (for good reason), |
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
92 * so we do our best and return what we can. |
258 | 93 */ |
187
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
94 |
258 | 95 return QDate(year.value_or(2000), static_cast<unsigned int>(month.value_or(Date::Month::Jan)), day.value_or(1)); |
9 | 96 } |
97 | |
98 nlohmann::json Date::GetAsAniListJson() const { | |
258 | 99 nlohmann::json json = { |
100 {"year", nullptr}, | |
101 {"month", nullptr}, | |
102 {"day", nullptr} | |
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; | |
9 | 115 } |