Mercurial > minori
annotate src/core/date.cc @ 195:975a3f0965e2
locale: only attempt loading locales after QApplication is init'd
also the general application stuff and anime list is separated in settings
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Thu, 07 Dec 2023 11:14:01 -0500 |
parents | 9613d72b097e |
children | f0ff06a45c42 |
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 | |
187
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
11 template<typename T> |
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
12 bool CLAMP(T x, T low, T high) { |
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
13 return std::max(low, std::min(high, x)); |
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
14 } |
9 | 15 |
16 Date::Date() { | |
17 } | |
18 | |
51 | 19 Date::Date(unsigned int y) { |
11 | 20 SetYear(y); |
9 | 21 } |
22 | |
51 | 23 Date::Date(unsigned int y, unsigned int m, unsigned int d) { |
11 | 24 SetYear(y); |
25 SetMonth(m); | |
26 SetDay(d); | |
9 | 27 } |
28 | |
51 | 29 Date::Date(const QDate& date) { |
30 SetYear(date.year()); | |
31 SetMonth(date.month()); | |
32 SetDay(date.day()); | |
33 } | |
34 | |
174
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
81
diff
changeset
|
35 Date::Date(const nlohmann::json& json) { |
175 | 36 /* NOTE: this constructor is made for use with |
187
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
37 AniList FuzzyDate-style JSON. In the future, some other |
175 | 38 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
|
39 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
|
40 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
|
41 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
|
42 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
|
43 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
|
44 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
|
45 } |
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
81
diff
changeset
|
46 |
9 | 47 void Date::VoidYear() { |
187
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
48 year.Void(); |
9 | 49 } |
50 | |
51 void Date::VoidMonth() { | |
187
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
52 month.Void(); |
9 | 53 } |
54 | |
55 void Date::VoidDay() { | |
187
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
56 day.Void(); |
9 | 57 } |
58 | |
51 | 59 void Date::SetYear(unsigned int y) { |
187
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
60 year.Set(y); |
9 | 61 } |
62 | |
51 | 63 void Date::SetMonth(unsigned int m) { |
187
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
64 month.Set(CLAMP(m, 1U, 12U)); |
9 | 65 } |
66 | |
51 | 67 void Date::SetDay(unsigned int d) { |
187
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
68 day.Set(CLAMP(d, 1U, 31U)); |
9 | 69 } |
70 | |
51 | 71 unsigned int Date::GetYear() const { |
187
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
72 return year.Get(); |
9 | 73 } |
74 | |
51 | 75 unsigned int Date::GetMonth() const { |
187
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
76 return month.Get(); |
9 | 77 } |
78 | |
51 | 79 unsigned int Date::GetDay() const { |
187
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
80 return day.Get(); |
9 | 81 } |
82 | |
47
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
83 bool Date::IsValid() const { |
187
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
84 return year.Enabled() && month.Enabled() && day.Enabled(); |
47
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
85 } |
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
86 |
9 | 87 bool Date::operator<(const Date& other) const { |
187
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
88 const unsigned int y = GetYear(), m = GetMonth(), d = GetDay(); |
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
89 const unsigned int o_y = other.GetYear(), o_m = other.GetMonth(), o_d = other.GetDay(); |
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
90 |
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
91 return (y < o_y && m < o_m && d < o_d); |
9 | 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 } | |
105 | |
106 QDate Date::GetAsQDate() const { | |
187
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
107 /* QDate doesn't support "missing" values (for good reason), |
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
108 * so we do our best and return what we can. |
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
109 */ |
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
110 |
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
111 return QDate(year.Enabled() ? year.Get() : 2000, month.Enabled() ? month.Get() : 1, day.Enabled() ? day.Get() : 1); |
9 | 112 } |
113 | |
114 nlohmann::json Date::GetAsAniListJson() const { | |
77 | 115 nlohmann::json result = {}; |
187
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
116 |
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
117 if (year.Enabled()) |
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
118 result["year"] = year.Get(); |
9 | 119 else |
77 | 120 result["year"] = nullptr; |
187
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
121 |
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
122 if (month.Enabled()) |
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
123 result["month"] = month.Get(); |
9 | 124 else |
77 | 125 result["month"] = nullptr; |
187
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
126 |
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
127 if (day.Enabled()) |
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
128 result["day"] = day.Get(); |
9 | 129 else |
77 | 130 result["day"] = nullptr; |
187
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
131 |
9 | 132 return result; |
133 } |