Mercurial > minori
annotate src/core/date.cc @ 337:a7d4e5107531
dep/animone: REFACTOR ALL THE THINGS
1: animone now has its own syntax divergent from anisthesia,
making different platforms actually have their own sections
2: process names in animone are now called `comm' (this will
probably break things). this is what its called in bsd/linux
so I'm just going to use it everywhere
3: the X11 code now checks for the existence of a UTF-8 window title
and passes it if available
4: ANYTHING THATS NOT LINUX IS 100% UNTESTED AND CAN AND WILL BREAK!
I still actually need to test the bsd code. to be honest I'm probably
going to move all of the bsds into separate files because they're
all essentially different operating systems at this point
author | Paper <paper@paper.us.eu.org> |
---|---|
date | Wed, 19 Jun 2024 12:51:15 -0400 |
parents | d928ec7b6a0d |
children |
rev | line source |
---|---|
9 | 1 #include "core/date.h" |
319
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
2 #include "core/time.h" |
9 | 3 #include "core/json.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 | |
13 Date::Date() { | |
14 } | |
15 | |
258 | 16 Date::Date(Date::Year y) { |
11 | 17 SetYear(y); |
9 | 18 } |
19 | |
258 | 20 Date::Date(Date::Year y, Date::Month m, Date::Day d) { |
11 | 21 SetYear(y); |
22 SetMonth(m); | |
23 SetDay(d); | |
9 | 24 } |
25 | |
317
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
26 Date::Date(const std::string& str) { |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
27 unsigned int y, m, d; |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
28 |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
29 /* 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
|
30 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
|
31 |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
32 if (amt > 0) |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
33 SetYear(y); |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
34 |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
35 if (amt > 1) |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
36 SetMonth(static_cast<Date::Month>(m - 1)); |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
37 |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
38 if (amt > 2) |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
39 SetDay(d); |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
40 } |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
41 |
51 | 42 Date::Date(const QDate& date) { |
43 SetYear(date.year()); | |
258 | 44 auto m = date.month(); |
45 m = std::clamp(m, static_cast<decltype(m)>(Date::Month::Jan), static_cast<decltype(m)>(Date::Month::Dec)); | |
46 SetMonth(static_cast<Date::Month>(m)); | |
51 | 47 SetDay(date.day()); |
48 } | |
49 | |
174
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
81
diff
changeset
|
50 Date::Date(const nlohmann::json& json) { |
175 | 51 /* NOTE: this constructor is made for use with |
258 | 52 * AniList FuzzyDate-style JSON. In the future, some other |
53 * 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
|
54 |
bc1ae1810855
dep/animia: switch from using classes to global functions
Paper <mrpapersonic@gmail.com>
parents:
197
diff
changeset
|
55 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
|
56 SetYear(json.at("/year"_json_pointer).get<unsigned int>()); |
258 | 57 |
58 if (json.contains("/month"_json_pointer) && json.at("/month"_json_pointer).is_number()) { | |
59 auto m = json.at("/month"_json_pointer).get<unsigned int>(); | |
60 m = std::clamp(m, static_cast<decltype(m)>(Date::Month::Jan), static_cast<decltype(m)>(Date::Month::Dec)); | |
61 SetMonth(static_cast<Date::Month>(m)); | |
62 } | |
63 | |
198
bc1ae1810855
dep/animia: switch from using classes to global functions
Paper <mrpapersonic@gmail.com>
parents:
197
diff
changeset
|
64 if (json.contains("/day"_json_pointer) && json.at("/day"_json_pointer).is_number()) |
258 | 65 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
|
66 } |
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
81
diff
changeset
|
67 |
319
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
68 Date::Date(Time::Timestamp timestamp) { |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
69 Date(QDateTime::fromSecsSinceEpoch(timestamp).date()); |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
70 } |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
71 |
9 | 72 void Date::VoidYear() { |
196
f0ff06a45c42
date: use std::optional for values
Paper <mrpapersonic@gmail.com>
parents:
187
diff
changeset
|
73 year.reset(); |
9 | 74 } |
75 | |
76 void Date::VoidMonth() { | |
196
f0ff06a45c42
date: use std::optional for values
Paper <mrpapersonic@gmail.com>
parents:
187
diff
changeset
|
77 month.reset(); |
9 | 78 } |
79 | |
80 void Date::VoidDay() { | |
196
f0ff06a45c42
date: use std::optional for values
Paper <mrpapersonic@gmail.com>
parents:
187
diff
changeset
|
81 day.reset(); |
9 | 82 } |
83 | |
258 | 84 void Date::SetYear(Date::Year y) { |
197 | 85 year.emplace(y); |
9 | 86 } |
87 | |
258 | 88 void Date::SetMonth(Date::Month m) { |
89 month.emplace(m); | |
9 | 90 } |
91 | |
258 | 92 void Date::SetDay(Date::Day d) { |
93 day.emplace(std::clamp(d, static_cast<Date::Day>(1U), static_cast<Date::Day>(31U))); | |
9 | 94 } |
95 | |
258 | 96 std::optional<Date::Year> Date::GetYear() const { |
197 | 97 return year; |
9 | 98 } |
99 | |
258 | 100 std::optional<Date::Month> Date::GetMonth() const { |
197 | 101 return month; |
9 | 102 } |
103 | |
258 | 104 std::optional<Date::Day> Date::GetDay() const { |
197 | 105 return day; |
9 | 106 } |
107 | |
47
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
108 bool Date::IsValid() const { |
196
f0ff06a45c42
date: use std::optional for values
Paper <mrpapersonic@gmail.com>
parents:
187
diff
changeset
|
109 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
|
110 } |
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
111 |
9 | 112 QDate Date::GetAsQDate() const { |
187
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
113 /* QDate doesn't support "missing" values (for good reason), |
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
114 * so we do our best and return what we can. |
258 | 115 */ |
187
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
116 |
258 | 117 return QDate(year.value_or(2000), static_cast<unsigned int>(month.value_or(Date::Month::Jan)), day.value_or(1)); |
9 | 118 } |
119 | |
120 nlohmann::json Date::GetAsAniListJson() const { | |
258 | 121 nlohmann::json json = { |
122 {"year", nullptr}, | |
123 {"month", nullptr}, | |
124 {"day", nullptr} | |
125 }; | |
126 | |
127 if (year) | |
128 json["year"] = static_cast<unsigned int>(year.value()); | |
129 | |
130 if (month) | |
131 json["month"] = static_cast<unsigned char>(month.value()); | |
132 | |
133 if (day) | |
134 json["day"] = static_cast<unsigned char>(day.value()); | |
135 | |
136 return json; | |
9 | 137 } |