Mercurial > minori
annotate src/core/date.cc @ 174:f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Wed, 29 Nov 2023 13:53:56 -0500 |
parents | 9b2b41f83a5e |
children | 9b10175be389 |
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) { |
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
81
diff
changeset
|
33 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
|
34 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
|
35 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
|
36 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
|
37 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
|
38 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
|
39 } |
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
81
diff
changeset
|
40 |
9 | 41 void Date::VoidYear() { |
42 year.reset(); | |
43 } | |
44 | |
45 void Date::VoidMonth() { | |
46 month.reset(); | |
47 } | |
48 | |
49 void Date::VoidDay() { | |
50 day.reset(); | |
51 } | |
52 | |
51 | 53 void Date::SetYear(unsigned int y) { |
63 | 54 year.reset(new unsigned int(y)); |
9 | 55 } |
56 | |
51 | 57 void Date::SetMonth(unsigned int m) { |
58 month.reset(new unsigned int(CLAMP(m, 1U, 12U))); | |
9 | 59 } |
60 | |
51 | 61 void Date::SetDay(unsigned int d) { |
62 day.reset(new unsigned int(CLAMP(d, 1U, 31U))); | |
9 | 63 } |
64 | |
51 | 65 unsigned int Date::GetYear() const { |
66 unsigned int* ptr = year.get(); | |
9 | 67 if (ptr != nullptr) |
68 return *year; | |
69 return -1; | |
70 } | |
71 | |
51 | 72 unsigned int Date::GetMonth() const { |
73 unsigned int* ptr = month.get(); | |
9 | 74 if (ptr != nullptr) |
75 return *month; | |
76 return -1; | |
77 } | |
78 | |
51 | 79 unsigned int Date::GetDay() const { |
80 unsigned int* ptr = day.get(); | |
9 | 81 if (ptr != nullptr) |
82 return *day; | |
83 return -1; | |
84 } | |
85 | |
47
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
86 bool Date::IsValid() const { |
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
87 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
|
88 } |
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
89 |
9 | 90 bool Date::operator<(const Date& other) const { |
51 | 91 unsigned int y = GetYear(), m = GetMonth(), d = GetDay(); |
92 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
|
93 return std::tie(y, m, d) < std::tie(o_y, o_m, o_d); |
9 | 94 } |
95 | |
96 bool Date::operator>(const Date& other) const { | |
97 return other < (*this); | |
98 } | |
99 | |
100 bool Date::operator<=(const Date& other) const { | |
101 return !((*this) > other); | |
102 } | |
103 | |
104 bool Date::operator>=(const Date& other) const { | |
105 return !((*this) < other); | |
106 } | |
107 | |
108 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
|
109 /* 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
|
110 if (IsValid()) |
10 | 111 return QDate(*year, *month, *day); |
15 | 112 else |
113 return QDate(); | |
9 | 114 } |
115 | |
116 nlohmann::json Date::GetAsAniListJson() const { | |
77 | 117 nlohmann::json result = {}; |
9 | 118 if (year.get()) |
77 | 119 result["year"] = *year; |
9 | 120 else |
77 | 121 result["year"] = nullptr; |
9 | 122 if (month.get()) |
77 | 123 result["month"] = *month; |
9 | 124 else |
77 | 125 result["month"] = nullptr; |
9 | 126 if (day.get()) |
77 | 127 result["day"] = *day; |
9 | 128 else |
77 | 129 result["day"] = nullptr; |
9 | 130 return result; |
131 } |