Mercurial > minori
annotate src/core/date.cc @ 175:9b10175be389
dep/json: update to v3.11.3
anime/db: save anime list to database, very much untested and likely won't work as intended
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Thu, 30 Nov 2023 13:52:26 -0500 |
parents | f88eda79c60a |
children | 9613d72b097e |
rev | line source |
---|---|
9 | 1 #include "core/date.h" |
2 #include "core/json.h" | |
3 #include <QDate> | |
51 | 4 #include <QDebug> |
63 | 5 #include <algorithm> |
9 | 6 #include <cstdint> |
7 #include <tuple> | |
8 | |
9 /* An implementation of AniList's "fuzzy date" */ | |
10 | |
63 | 11 #define CLAMP(x, low, high) (std::max(low, std::min(high, x))) |
9 | 12 |
13 Date::Date() { | |
14 } | |
15 | |
51 | 16 Date::Date(unsigned int y) { |
11 | 17 SetYear(y); |
9 | 18 } |
19 | |
51 | 20 Date::Date(unsigned int y, unsigned int m, unsigned int d) { |
11 | 21 SetYear(y); |
22 SetMonth(m); | |
23 SetDay(d); | |
9 | 24 } |
25 | |
51 | 26 Date::Date(const QDate& date) { |
27 SetYear(date.year()); | |
28 SetMonth(date.month()); | |
29 SetDay(date.day()); | |
30 } | |
31 | |
174
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
81
diff
changeset
|
32 Date::Date(const nlohmann::json& json) { |
175 | 33 /* NOTE: this constructor is made for use with |
34 AniList FussyDate-style JSON. In the future, some other | |
35 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
|
36 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
|
37 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
|
38 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
|
39 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
|
40 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
|
41 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
|
42 } |
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
81
diff
changeset
|
43 |
9 | 44 void Date::VoidYear() { |
45 year.reset(); | |
46 } | |
47 | |
48 void Date::VoidMonth() { | |
49 month.reset(); | |
50 } | |
51 | |
52 void Date::VoidDay() { | |
53 day.reset(); | |
54 } | |
55 | |
51 | 56 void Date::SetYear(unsigned int y) { |
63 | 57 year.reset(new unsigned int(y)); |
9 | 58 } |
59 | |
51 | 60 void Date::SetMonth(unsigned int m) { |
61 month.reset(new unsigned int(CLAMP(m, 1U, 12U))); | |
9 | 62 } |
63 | |
51 | 64 void Date::SetDay(unsigned int d) { |
65 day.reset(new unsigned int(CLAMP(d, 1U, 31U))); | |
9 | 66 } |
67 | |
51 | 68 unsigned int Date::GetYear() const { |
69 unsigned int* ptr = year.get(); | |
9 | 70 if (ptr != nullptr) |
71 return *year; | |
72 return -1; | |
73 } | |
74 | |
51 | 75 unsigned int Date::GetMonth() const { |
76 unsigned int* ptr = month.get(); | |
9 | 77 if (ptr != nullptr) |
78 return *month; | |
79 return -1; | |
80 } | |
81 | |
51 | 82 unsigned int Date::GetDay() const { |
83 unsigned int* ptr = day.get(); | |
9 | 84 if (ptr != nullptr) |
85 return *day; | |
86 return -1; | |
87 } | |
88 | |
47
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
89 bool Date::IsValid() const { |
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
90 return year.get() && month.get() && day.get(); |
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
91 } |
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
92 |
9 | 93 bool Date::operator<(const Date& other) const { |
51 | 94 unsigned int y = GetYear(), m = GetMonth(), d = GetDay(); |
95 unsigned int o_y = other.GetYear(), o_m = other.GetMonth(), o_d = other.GetDay(); | |
47
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
96 return std::tie(y, m, d) < std::tie(o_y, o_m, o_d); |
9 | 97 } |
98 | |
99 bool Date::operator>(const Date& other) const { | |
100 return other < (*this); | |
101 } | |
102 | |
103 bool Date::operator<=(const Date& other) const { | |
104 return !((*this) > other); | |
105 } | |
106 | |
107 bool Date::operator>=(const Date& other) const { | |
108 return !((*this) < other); | |
109 } | |
110 | |
111 QDate Date::GetAsQDate() const { | |
47
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
112 /* QDates don't support "missing" values, for good reason. */ |
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
113 if (IsValid()) |
10 | 114 return QDate(*year, *month, *day); |
15 | 115 else |
116 return QDate(); | |
9 | 117 } |
118 | |
119 nlohmann::json Date::GetAsAniListJson() const { | |
77 | 120 nlohmann::json result = {}; |
9 | 121 if (year.get()) |
77 | 122 result["year"] = *year; |
9 | 123 else |
77 | 124 result["year"] = nullptr; |
9 | 125 if (month.get()) |
77 | 126 result["month"] = *month; |
9 | 127 else |
77 | 128 result["month"] = nullptr; |
9 | 129 if (day.get()) |
77 | 130 result["day"] = *day; |
9 | 131 else |
77 | 132 result["day"] = nullptr; |
9 | 133 return result; |
134 } |