Mercurial > minori
annotate src/core/date.cc @ 317:b1f4d1867ab1
services: VERY initial Kitsu support
it only supports user authentication for now, but it's definitely
a start.
author | Paper <paper@paper.us.eu.org> |
---|---|
date | Wed, 12 Jun 2024 04:07:10 -0400 |
parents | 862d0d8619f6 |
children | d928ec7b6a0d |
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> |
317
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
8 #include <cstdio> |
9 | 9 |
10 /* An implementation of AniList's "fuzzy date" */ | |
11 | |
12 Date::Date() { | |
13 } | |
14 | |
258 | 15 Date::Date(Date::Year y) { |
11 | 16 SetYear(y); |
9 | 17 } |
18 | |
258 | 19 Date::Date(Date::Year y, Date::Month m, Date::Day d) { |
11 | 20 SetYear(y); |
21 SetMonth(m); | |
22 SetDay(d); | |
9 | 23 } |
24 | |
317
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
25 Date::Date(const std::string& str) { |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
26 unsigned int y, m, d; |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
27 |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
28 /* I don't like this that much, but it works... */ |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
29 int amt = std::sscanf(str.c_str(), "%4u-%2u-%2u", &y, &m, &d); |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
30 |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
31 if (amt > 0) |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
32 SetYear(y); |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
33 |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
34 if (amt > 1) |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
35 SetMonth(static_cast<Date::Month>(m - 1)); |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
36 |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
37 if (amt > 2) |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
38 SetDay(d); |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
39 } |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
40 |
51 | 41 Date::Date(const QDate& date) { |
42 SetYear(date.year()); | |
258 | 43 auto m = date.month(); |
44 m = std::clamp(m, static_cast<decltype(m)>(Date::Month::Jan), static_cast<decltype(m)>(Date::Month::Dec)); | |
45 SetMonth(static_cast<Date::Month>(m)); | |
51 | 46 SetDay(date.day()); |
47 } | |
48 | |
174
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
81
diff
changeset
|
49 Date::Date(const nlohmann::json& json) { |
175 | 50 /* NOTE: this constructor is made for use with |
258 | 51 * AniList FuzzyDate-style JSON. In the future, some other |
52 * 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
|
53 |
bc1ae1810855
dep/animia: switch from using classes to global functions
Paper <mrpapersonic@gmail.com>
parents:
197
diff
changeset
|
54 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
|
55 SetYear(json.at("/year"_json_pointer).get<unsigned int>()); |
258 | 56 |
57 if (json.contains("/month"_json_pointer) && json.at("/month"_json_pointer).is_number()) { | |
58 auto m = json.at("/month"_json_pointer).get<unsigned int>(); | |
59 m = std::clamp(m, static_cast<decltype(m)>(Date::Month::Jan), static_cast<decltype(m)>(Date::Month::Dec)); | |
60 SetMonth(static_cast<Date::Month>(m)); | |
61 } | |
62 | |
198
bc1ae1810855
dep/animia: switch from using classes to global functions
Paper <mrpapersonic@gmail.com>
parents:
197
diff
changeset
|
63 if (json.contains("/day"_json_pointer) && json.at("/day"_json_pointer).is_number()) |
258 | 64 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
|
65 } |
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
81
diff
changeset
|
66 |
9 | 67 void Date::VoidYear() { |
196
f0ff06a45c42
date: use std::optional for values
Paper <mrpapersonic@gmail.com>
parents:
187
diff
changeset
|
68 year.reset(); |
9 | 69 } |
70 | |
71 void Date::VoidMonth() { | |
196
f0ff06a45c42
date: use std::optional for values
Paper <mrpapersonic@gmail.com>
parents:
187
diff
changeset
|
72 month.reset(); |
9 | 73 } |
74 | |
75 void Date::VoidDay() { | |
196
f0ff06a45c42
date: use std::optional for values
Paper <mrpapersonic@gmail.com>
parents:
187
diff
changeset
|
76 day.reset(); |
9 | 77 } |
78 | |
258 | 79 void Date::SetYear(Date::Year y) { |
197 | 80 year.emplace(y); |
9 | 81 } |
82 | |
258 | 83 void Date::SetMonth(Date::Month m) { |
84 month.emplace(m); | |
9 | 85 } |
86 | |
258 | 87 void Date::SetDay(Date::Day d) { |
88 day.emplace(std::clamp(d, static_cast<Date::Day>(1U), static_cast<Date::Day>(31U))); | |
9 | 89 } |
90 | |
258 | 91 std::optional<Date::Year> Date::GetYear() const { |
197 | 92 return year; |
9 | 93 } |
94 | |
258 | 95 std::optional<Date::Month> Date::GetMonth() const { |
197 | 96 return month; |
9 | 97 } |
98 | |
258 | 99 std::optional<Date::Day> Date::GetDay() const { |
197 | 100 return day; |
9 | 101 } |
102 | |
47
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
103 bool Date::IsValid() const { |
196
f0ff06a45c42
date: use std::optional for values
Paper <mrpapersonic@gmail.com>
parents:
187
diff
changeset
|
104 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
|
105 } |
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
106 |
9 | 107 QDate Date::GetAsQDate() const { |
187
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
108 /* QDate doesn't support "missing" values (for good reason), |
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
109 * so we do our best and return what we can. |
258 | 110 */ |
187
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
111 |
258 | 112 return QDate(year.value_or(2000), static_cast<unsigned int>(month.value_or(Date::Month::Jan)), day.value_or(1)); |
9 | 113 } |
114 | |
115 nlohmann::json Date::GetAsAniListJson() const { | |
258 | 116 nlohmann::json json = { |
117 {"year", nullptr}, | |
118 {"month", nullptr}, | |
119 {"day", nullptr} | |
120 }; | |
121 | |
122 if (year) | |
123 json["year"] = static_cast<unsigned int>(year.value()); | |
124 | |
125 if (month) | |
126 json["month"] = static_cast<unsigned char>(month.value()); | |
127 | |
128 if (day) | |
129 json["day"] = static_cast<unsigned char>(day.value()); | |
130 | |
131 return json; | |
9 | 132 } |