annotate src/core/date.cc @ 196:f0ff06a45c42

date: use std::optional for values
author Paper <mrpapersonic@gmail.com>
date Thu, 07 Dec 2023 16:28:11 -0500
parents 9613d72b097e
children c4ca035c565d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
1 #include "core/date.h"
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
2 #include "core/json.h"
187
9613d72b097e *: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents: 175
diff changeset
3
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
4 #include <QDate>
51
75c804f713b2 window: add about window,
Paper <mrpapersonic@gmail.com>
parents: 47
diff changeset
5 #include <QDebug>
187
9613d72b097e *: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents: 175
diff changeset
6
63
3d2decf093bb *: fix many clang warnings
Paper <mrpapersonic@gmail.com>
parents: 51
diff changeset
7 #include <algorithm>
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
8
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
9 /* An implementation of AniList's "fuzzy date" */
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
10
187
9613d72b097e *: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents: 175
diff changeset
11 template<typename T>
9613d72b097e *: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents: 175
diff changeset
12 bool CLAMP(T x, T low, T high) {
9613d72b097e *: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents: 175
diff changeset
13 return std::max(low, std::min(high, x));
9613d72b097e *: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents: 175
diff changeset
14 }
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
15
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
16 Date::Date() {
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
17 }
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
18
51
75c804f713b2 window: add about window,
Paper <mrpapersonic@gmail.com>
parents: 47
diff changeset
19 Date::Date(unsigned int y) {
11
fc1bf97c528b *: use C++11 standard
Paper <mrpapersonic@gmail.com>
parents: 10
diff changeset
20 SetYear(y);
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
21 }
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
22
51
75c804f713b2 window: add about window,
Paper <mrpapersonic@gmail.com>
parents: 47
diff changeset
23 Date::Date(unsigned int y, unsigned int m, unsigned int d) {
11
fc1bf97c528b *: use C++11 standard
Paper <mrpapersonic@gmail.com>
parents: 10
diff changeset
24 SetYear(y);
fc1bf97c528b *: use C++11 standard
Paper <mrpapersonic@gmail.com>
parents: 10
diff changeset
25 SetMonth(m);
fc1bf97c528b *: use C++11 standard
Paper <mrpapersonic@gmail.com>
parents: 10
diff changeset
26 SetDay(d);
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
27 }
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
28
51
75c804f713b2 window: add about window,
Paper <mrpapersonic@gmail.com>
parents: 47
diff changeset
29 Date::Date(const QDate& date) {
75c804f713b2 window: add about window,
Paper <mrpapersonic@gmail.com>
parents: 47
diff changeset
30 SetYear(date.year());
75c804f713b2 window: add about window,
Paper <mrpapersonic@gmail.com>
parents: 47
diff changeset
31 SetMonth(date.month());
75c804f713b2 window: add about window,
Paper <mrpapersonic@gmail.com>
parents: 47
diff changeset
32 SetDay(date.day());
75c804f713b2 window: add about window,
Paper <mrpapersonic@gmail.com>
parents: 47
diff changeset
33 }
75c804f713b2 window: add about window,
Paper <mrpapersonic@gmail.com>
parents: 47
diff changeset
34
174
f88eda79c60a anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
35 Date::Date(const nlohmann::json& json) {
175
9b10175be389 dep/json: update to v3.11.3
Paper <mrpapersonic@gmail.com>
parents: 174
diff changeset
36 /* NOTE: this constructor is made for use with
187
9613d72b097e *: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents: 175
diff changeset
37 AniList FuzzyDate-style JSON. In the future, some other
175
9b10175be389 dep/json: update to v3.11.3
Paper <mrpapersonic@gmail.com>
parents: 174
diff changeset
38 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
39 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
40 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
41 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
42 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
43 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
44 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
45 }
f88eda79c60a anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
46
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
47 void Date::VoidYear() {
196
f0ff06a45c42 date: use std::optional for values
Paper <mrpapersonic@gmail.com>
parents: 187
diff changeset
48 year.reset();
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
49 }
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
50
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
51 void Date::VoidMonth() {
196
f0ff06a45c42 date: use std::optional for values
Paper <mrpapersonic@gmail.com>
parents: 187
diff changeset
52 month.reset();
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
53 }
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
54
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
55 void Date::VoidDay() {
196
f0ff06a45c42 date: use std::optional for values
Paper <mrpapersonic@gmail.com>
parents: 187
diff changeset
56 day.reset();
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
57 }
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
58
51
75c804f713b2 window: add about window,
Paper <mrpapersonic@gmail.com>
parents: 47
diff changeset
59 void Date::SetYear(unsigned int y) {
196
f0ff06a45c42 date: use std::optional for values
Paper <mrpapersonic@gmail.com>
parents: 187
diff changeset
60 year = y;
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
61 }
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
62
51
75c804f713b2 window: add about window,
Paper <mrpapersonic@gmail.com>
parents: 47
diff changeset
63 void Date::SetMonth(unsigned int m) {
196
f0ff06a45c42 date: use std::optional for values
Paper <mrpapersonic@gmail.com>
parents: 187
diff changeset
64 month = CLAMP(m, 1U, 12U);
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
65 }
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
66
51
75c804f713b2 window: add about window,
Paper <mrpapersonic@gmail.com>
parents: 47
diff changeset
67 void Date::SetDay(unsigned int d) {
196
f0ff06a45c42 date: use std::optional for values
Paper <mrpapersonic@gmail.com>
parents: 187
diff changeset
68 day = CLAMP(d, 1U, 31U);
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
69 }
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
70
51
75c804f713b2 window: add about window,
Paper <mrpapersonic@gmail.com>
parents: 47
diff changeset
71 unsigned int Date::GetYear() const {
196
f0ff06a45c42 date: use std::optional for values
Paper <mrpapersonic@gmail.com>
parents: 187
diff changeset
72 return year.value_or(-1);
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
73 }
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
74
51
75c804f713b2 window: add about window,
Paper <mrpapersonic@gmail.com>
parents: 47
diff changeset
75 unsigned int Date::GetMonth() const {
196
f0ff06a45c42 date: use std::optional for values
Paper <mrpapersonic@gmail.com>
parents: 187
diff changeset
76 return month.value_or(-1);
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
77 }
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
78
51
75c804f713b2 window: add about window,
Paper <mrpapersonic@gmail.com>
parents: 47
diff changeset
79 unsigned int Date::GetDay() const {
196
f0ff06a45c42 date: use std::optional for values
Paper <mrpapersonic@gmail.com>
parents: 187
diff changeset
80 return day.value_or(-1);
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
81 }
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
82
47
d8eb763e6661 information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents: 36
diff changeset
83 bool Date::IsValid() const {
196
f0ff06a45c42 date: use std::optional for values
Paper <mrpapersonic@gmail.com>
parents: 187
diff changeset
84 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
85 }
d8eb763e6661 information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents: 36
diff changeset
86
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
87 bool Date::operator<(const Date& other) const {
187
9613d72b097e *: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents: 175
diff changeset
88 const unsigned int y = GetYear(), m = GetMonth(), d = GetDay();
9613d72b097e *: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents: 175
diff changeset
89 const unsigned int o_y = other.GetYear(), o_m = other.GetMonth(), o_d = other.GetDay();
9613d72b097e *: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents: 175
diff changeset
90
9613d72b097e *: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents: 175
diff changeset
91 return (y < o_y && m < o_m && d < o_d);
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
92 }
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
93
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
94 bool Date::operator>(const Date& other) const {
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
95 return other < (*this);
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
96 }
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
97
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
98 bool Date::operator<=(const Date& other) const {
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
99 return !((*this) > other);
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
100 }
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
101
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
102 bool Date::operator>=(const Date& other) const {
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
103 return !((*this) < other);
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
104 }
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
105
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
106 QDate Date::GetAsQDate() const {
187
9613d72b097e *: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents: 175
diff changeset
107 /* QDate doesn't support "missing" values (for good reason),
9613d72b097e *: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents: 175
diff changeset
108 * so we do our best and return what we can.
9613d72b097e *: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents: 175
diff changeset
109 */
9613d72b097e *: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents: 175
diff changeset
110
196
f0ff06a45c42 date: use std::optional for values
Paper <mrpapersonic@gmail.com>
parents: 187
diff changeset
111 return QDate(year.value_or(2000), month.value_or(1), day.value_or(1));
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
112 }
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
113
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
114 nlohmann::json Date::GetAsAniListJson() const {
77
6f7385bd334c *: update
Paper <mrpapersonic@gmail.com>
parents: 63
diff changeset
115 nlohmann::json result = {};
187
9613d72b097e *: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents: 175
diff changeset
116
196
f0ff06a45c42 date: use std::optional for values
Paper <mrpapersonic@gmail.com>
parents: 187
diff changeset
117 if (year.has_value())
f0ff06a45c42 date: use std::optional for values
Paper <mrpapersonic@gmail.com>
parents: 187
diff changeset
118 result["year"] = year.value();
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
119 else
77
6f7385bd334c *: update
Paper <mrpapersonic@gmail.com>
parents: 63
diff changeset
120 result["year"] = nullptr;
187
9613d72b097e *: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents: 175
diff changeset
121
196
f0ff06a45c42 date: use std::optional for values
Paper <mrpapersonic@gmail.com>
parents: 187
diff changeset
122 if (month.has_value())
f0ff06a45c42 date: use std::optional for values
Paper <mrpapersonic@gmail.com>
parents: 187
diff changeset
123 result["month"] = month.value();
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
124 else
77
6f7385bd334c *: update
Paper <mrpapersonic@gmail.com>
parents: 63
diff changeset
125 result["month"] = nullptr;
187
9613d72b097e *: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents: 175
diff changeset
126
196
f0ff06a45c42 date: use std::optional for values
Paper <mrpapersonic@gmail.com>
parents: 187
diff changeset
127 if (day.has_value())
f0ff06a45c42 date: use std::optional for values
Paper <mrpapersonic@gmail.com>
parents: 187
diff changeset
128 result["day"] = day.value();
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
129 else
77
6f7385bd334c *: update
Paper <mrpapersonic@gmail.com>
parents: 63
diff changeset
130 result["day"] = nullptr;
187
9613d72b097e *: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents: 175
diff changeset
131
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
132 return result;
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
133 }