Mercurial > minori
annotate 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 |
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 | |
51 | 14 Date::Date(unsigned int y) { |
11 | 15 SetYear(y); |
9 | 16 } |
17 | |
51 | 18 Date::Date(unsigned int y, unsigned int m, unsigned int 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()); | |
26 SetMonth(date.month()); | |
27 SetDay(date.day()); | |
28 } | |
29 | |
174
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
81
diff
changeset
|
30 Date::Date(const nlohmann::json& json) { |
175 | 31 /* NOTE: this constructor is made for use with |
187
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
32 AniList FuzzyDate-style JSON. In the future, some other |
175 | 33 methods may be parsed and whatnot if necessary. */ |
174
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
81
diff
changeset
|
34 if (json.contains("year") && json.at("year").is_number()) |
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
81
diff
changeset
|
35 SetYear(json.at("year").get<unsigned int>()); |
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
81
diff
changeset
|
36 if (json.contains("month") && json.at("month").is_number()) |
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
81
diff
changeset
|
37 SetMonth(json.at("month").get<unsigned int>()); |
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
81
diff
changeset
|
38 if (json.contains("day") && json.at("day").is_number()) |
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
81
diff
changeset
|
39 SetDay(json.at("day").get<unsigned int>()); |
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
81
diff
changeset
|
40 } |
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
81
diff
changeset
|
41 |
9 | 42 void Date::VoidYear() { |
196
f0ff06a45c42
date: use std::optional for values
Paper <mrpapersonic@gmail.com>
parents:
187
diff
changeset
|
43 year.reset(); |
9 | 44 } |
45 | |
46 void Date::VoidMonth() { | |
196
f0ff06a45c42
date: use std::optional for values
Paper <mrpapersonic@gmail.com>
parents:
187
diff
changeset
|
47 month.reset(); |
9 | 48 } |
49 | |
50 void Date::VoidDay() { | |
196
f0ff06a45c42
date: use std::optional for values
Paper <mrpapersonic@gmail.com>
parents:
187
diff
changeset
|
51 day.reset(); |
9 | 52 } |
53 | |
51 | 54 void Date::SetYear(unsigned int y) { |
197 | 55 year.emplace(y); |
9 | 56 } |
57 | |
51 | 58 void Date::SetMonth(unsigned int m) { |
197 | 59 month.emplace(std::clamp(m, 1U, 12U)); |
9 | 60 } |
61 | |
51 | 62 void Date::SetDay(unsigned int d) { |
197 | 63 day.emplace(std::clamp(d, 1U, 31U)); |
9 | 64 } |
65 | |
197 | 66 std::optional<unsigned int> Date::GetYear() const { |
67 return year; | |
9 | 68 } |
69 | |
197 | 70 std::optional<unsigned int> Date::GetMonth() const { |
71 return month; | |
9 | 72 } |
73 | |
197 | 74 std::optional<unsigned int> Date::GetDay() const { |
75 return day; | |
9 | 76 } |
77 | |
47
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
78 bool Date::IsValid() const { |
196
f0ff06a45c42
date: use std::optional for values
Paper <mrpapersonic@gmail.com>
parents:
187
diff
changeset
|
79 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
|
80 } |
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
81 |
9 | 82 QDate Date::GetAsQDate() const { |
187
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
83 /* QDate doesn't support "missing" values (for good reason), |
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
84 * so we do our best and return what we can. |
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
85 */ |
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
86 |
196
f0ff06a45c42
date: use std::optional for values
Paper <mrpapersonic@gmail.com>
parents:
187
diff
changeset
|
87 return QDate(year.value_or(2000), month.value_or(1), day.value_or(1)); |
9 | 88 } |
89 | |
90 nlohmann::json Date::GetAsAniListJson() const { | |
77 | 91 nlohmann::json result = {}; |
187
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
92 |
196
f0ff06a45c42
date: use std::optional for values
Paper <mrpapersonic@gmail.com>
parents:
187
diff
changeset
|
93 if (year.has_value()) |
f0ff06a45c42
date: use std::optional for values
Paper <mrpapersonic@gmail.com>
parents:
187
diff
changeset
|
94 result["year"] = year.value(); |
9 | 95 else |
77 | 96 result["year"] = nullptr; |
187
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
97 |
196
f0ff06a45c42
date: use std::optional for values
Paper <mrpapersonic@gmail.com>
parents:
187
diff
changeset
|
98 if (month.has_value()) |
f0ff06a45c42
date: use std::optional for values
Paper <mrpapersonic@gmail.com>
parents:
187
diff
changeset
|
99 result["month"] = month.value(); |
9 | 100 else |
77 | 101 result["month"] = nullptr; |
187
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
102 |
196
f0ff06a45c42
date: use std::optional for values
Paper <mrpapersonic@gmail.com>
parents:
187
diff
changeset
|
103 if (day.has_value()) |
f0ff06a45c42
date: use std::optional for values
Paper <mrpapersonic@gmail.com>
parents:
187
diff
changeset
|
104 result["day"] = day.value(); |
9 | 105 else |
77 | 106 result["day"] = nullptr; |
187
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
107 |
9 | 108 return result; |
109 } |