Mercurial > minori
annotate src/core/date.cc @ 369:47c9f8502269
*: clang-format all the things
I've edited the formatting a bit. Now pointer asterisks (and reference
ampersands) are on the variable instead of the type, as well as having
newlines for function braces (but nothing else)
author | Paper <paper@tflc.us> |
---|---|
date | Fri, 25 Jul 2025 10:16:02 -0400 |
parents | d928ec7b6a0d |
children |
rev | line source |
---|---|
9 | 1 #include "core/date.h" |
369 | 2 #include "core/json.h" |
319
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
3 #include "core/time.h" |
187
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
4 |
9 | 5 #include <QDate> |
51 | 6 #include <QDebug> |
187
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
7 |
63 | 8 #include <algorithm> |
317
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
9 #include <cstdio> |
9 | 10 |
11 /* An implementation of AniList's "fuzzy date" */ | |
12 | |
369 | 13 Date::Date() |
14 { | |
9 | 15 } |
16 | |
369 | 17 Date::Date(Date::Year y) |
18 { | |
11 | 19 SetYear(y); |
9 | 20 } |
21 | |
369 | 22 Date::Date(Date::Year y, Date::Month m, Date::Day d) |
23 { | |
11 | 24 SetYear(y); |
25 SetMonth(m); | |
26 SetDay(d); | |
9 | 27 } |
28 | |
369 | 29 Date::Date(const std::string &str) |
30 { | |
317
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
31 unsigned int y, m, d; |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
32 |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
33 /* 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
|
34 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
|
35 |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
36 if (amt > 0) |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
37 SetYear(y); |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
38 |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
39 if (amt > 1) |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
40 SetMonth(static_cast<Date::Month>(m - 1)); |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
41 |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
42 if (amt > 2) |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
43 SetDay(d); |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
44 } |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
45 |
369 | 46 Date::Date(const QDate &date) |
47 { | |
51 | 48 SetYear(date.year()); |
258 | 49 auto m = date.month(); |
50 m = std::clamp(m, static_cast<decltype(m)>(Date::Month::Jan), static_cast<decltype(m)>(Date::Month::Dec)); | |
51 SetMonth(static_cast<Date::Month>(m)); | |
51 | 52 SetDay(date.day()); |
53 } | |
54 | |
369 | 55 Date::Date(const nlohmann::json &json) |
56 { | |
175 | 57 /* NOTE: this constructor is made for use with |
258 | 58 * AniList FuzzyDate-style JSON. In the future, some other |
59 * 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
|
60 |
bc1ae1810855
dep/animia: switch from using classes to global functions
Paper <mrpapersonic@gmail.com>
parents:
197
diff
changeset
|
61 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
|
62 SetYear(json.at("/year"_json_pointer).get<unsigned int>()); |
258 | 63 |
64 if (json.contains("/month"_json_pointer) && json.at("/month"_json_pointer).is_number()) { | |
65 auto m = json.at("/month"_json_pointer).get<unsigned int>(); | |
66 m = std::clamp(m, static_cast<decltype(m)>(Date::Month::Jan), static_cast<decltype(m)>(Date::Month::Dec)); | |
67 SetMonth(static_cast<Date::Month>(m)); | |
68 } | |
69 | |
198
bc1ae1810855
dep/animia: switch from using classes to global functions
Paper <mrpapersonic@gmail.com>
parents:
197
diff
changeset
|
70 if (json.contains("/day"_json_pointer) && json.at("/day"_json_pointer).is_number()) |
258 | 71 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
|
72 } |
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
81
diff
changeset
|
73 |
369 | 74 Date::Date(Time::Timestamp timestamp) |
75 { | |
319
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
76 Date(QDateTime::fromSecsSinceEpoch(timestamp).date()); |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
77 } |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
78 |
369 | 79 void Date::VoidYear() |
80 { | |
196
f0ff06a45c42
date: use std::optional for values
Paper <mrpapersonic@gmail.com>
parents:
187
diff
changeset
|
81 year.reset(); |
9 | 82 } |
83 | |
369 | 84 void Date::VoidMonth() |
85 { | |
196
f0ff06a45c42
date: use std::optional for values
Paper <mrpapersonic@gmail.com>
parents:
187
diff
changeset
|
86 month.reset(); |
9 | 87 } |
88 | |
369 | 89 void Date::VoidDay() |
90 { | |
196
f0ff06a45c42
date: use std::optional for values
Paper <mrpapersonic@gmail.com>
parents:
187
diff
changeset
|
91 day.reset(); |
9 | 92 } |
93 | |
369 | 94 void Date::SetYear(Date::Year y) |
95 { | |
197 | 96 year.emplace(y); |
9 | 97 } |
98 | |
369 | 99 void Date::SetMonth(Date::Month m) |
100 { | |
258 | 101 month.emplace(m); |
9 | 102 } |
103 | |
369 | 104 void Date::SetDay(Date::Day d) |
105 { | |
258 | 106 day.emplace(std::clamp(d, static_cast<Date::Day>(1U), static_cast<Date::Day>(31U))); |
9 | 107 } |
108 | |
369 | 109 std::optional<Date::Year> Date::GetYear() const |
110 { | |
197 | 111 return year; |
9 | 112 } |
113 | |
369 | 114 std::optional<Date::Month> Date::GetMonth() const |
115 { | |
197 | 116 return month; |
9 | 117 } |
118 | |
369 | 119 std::optional<Date::Day> Date::GetDay() const |
120 { | |
197 | 121 return day; |
9 | 122 } |
123 | |
369 | 124 bool Date::IsValid() const |
125 { | |
196
f0ff06a45c42
date: use std::optional for values
Paper <mrpapersonic@gmail.com>
parents:
187
diff
changeset
|
126 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
|
127 } |
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
128 |
369 | 129 QDate Date::GetAsQDate() const |
130 { | |
187
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
131 /* QDate doesn't support "missing" values (for good reason), |
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
132 * so we do our best and return what we can. |
258 | 133 */ |
187
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
134 |
258 | 135 return QDate(year.value_or(2000), static_cast<unsigned int>(month.value_or(Date::Month::Jan)), day.value_or(1)); |
9 | 136 } |
137 | |
369 | 138 nlohmann::json Date::GetAsAniListJson() const |
139 { | |
258 | 140 nlohmann::json json = { |
141 {"year", nullptr}, | |
142 {"month", nullptr}, | |
143 {"day", nullptr} | |
144 }; | |
145 | |
146 if (year) | |
147 json["year"] = static_cast<unsigned int>(year.value()); | |
148 | |
149 if (month) | |
150 json["month"] = static_cast<unsigned char>(month.value()); | |
151 | |
152 if (day) | |
153 json["day"] = static_cast<unsigned char>(day.value()); | |
154 | |
155 return json; | |
9 | 156 } |