comparison 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
comparison
equal deleted inserted replaced
368:6d37a998cf91 369:47c9f8502269
1 #include "core/date.h" 1 #include "core/date.h"
2 #include "core/json.h"
2 #include "core/time.h" 3 #include "core/time.h"
3 #include "core/json.h"
4 4
5 #include <QDate> 5 #include <QDate>
6 #include <QDebug> 6 #include <QDebug>
7 7
8 #include <algorithm> 8 #include <algorithm>
9 #include <cstdio> 9 #include <cstdio>
10 10
11 /* An implementation of AniList's "fuzzy date" */ 11 /* An implementation of AniList's "fuzzy date" */
12 12
13 Date::Date() { 13 Date::Date()
14 {
14 } 15 }
15 16
16 Date::Date(Date::Year y) { 17 Date::Date(Date::Year y)
18 {
17 SetYear(y); 19 SetYear(y);
18 } 20 }
19 21
20 Date::Date(Date::Year y, Date::Month m, Date::Day d) { 22 Date::Date(Date::Year y, Date::Month m, Date::Day d)
23 {
21 SetYear(y); 24 SetYear(y);
22 SetMonth(m); 25 SetMonth(m);
23 SetDay(d); 26 SetDay(d);
24 } 27 }
25 28
26 Date::Date(const std::string& str) { 29 Date::Date(const std::string &str)
30 {
27 unsigned int y, m, d; 31 unsigned int y, m, d;
28 32
29 /* I don't like this that much, but it works... */ 33 /* I don't like this that much, but it works... */
30 int amt = std::sscanf(str.c_str(), "%4u-%2u-%2u", &y, &m, &d); 34 int amt = std::sscanf(str.c_str(), "%4u-%2u-%2u", &y, &m, &d);
31 35
37 41
38 if (amt > 2) 42 if (amt > 2)
39 SetDay(d); 43 SetDay(d);
40 } 44 }
41 45
42 Date::Date(const QDate& date) { 46 Date::Date(const QDate &date)
47 {
43 SetYear(date.year()); 48 SetYear(date.year());
44 auto m = date.month(); 49 auto m = date.month();
45 m = std::clamp(m, static_cast<decltype(m)>(Date::Month::Jan), static_cast<decltype(m)>(Date::Month::Dec)); 50 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 SetMonth(static_cast<Date::Month>(m));
47 SetDay(date.day()); 52 SetDay(date.day());
48 } 53 }
49 54
50 Date::Date(const nlohmann::json& json) { 55 Date::Date(const nlohmann::json &json)
56 {
51 /* NOTE: this constructor is made for use with 57 /* NOTE: this constructor is made for use with
52 * AniList FuzzyDate-style JSON. In the future, some other 58 * AniList FuzzyDate-style JSON. In the future, some other
53 * methods may be parsed and whatnot if necessary. */ 59 * methods may be parsed and whatnot if necessary. */
54 60
55 if (json.contains("/year"_json_pointer) && json.at("/year"_json_pointer).is_number()) 61 if (json.contains("/year"_json_pointer) && json.at("/year"_json_pointer).is_number())
63 69
64 if (json.contains("/day"_json_pointer) && json.at("/day"_json_pointer).is_number()) 70 if (json.contains("/day"_json_pointer) && json.at("/day"_json_pointer).is_number())
65 SetDay(json.at("/day"_json_pointer).get<unsigned char>()); 71 SetDay(json.at("/day"_json_pointer).get<unsigned char>());
66 } 72 }
67 73
68 Date::Date(Time::Timestamp timestamp) { 74 Date::Date(Time::Timestamp timestamp)
75 {
69 Date(QDateTime::fromSecsSinceEpoch(timestamp).date()); 76 Date(QDateTime::fromSecsSinceEpoch(timestamp).date());
70 } 77 }
71 78
72 void Date::VoidYear() { 79 void Date::VoidYear()
80 {
73 year.reset(); 81 year.reset();
74 } 82 }
75 83
76 void Date::VoidMonth() { 84 void Date::VoidMonth()
85 {
77 month.reset(); 86 month.reset();
78 } 87 }
79 88
80 void Date::VoidDay() { 89 void Date::VoidDay()
90 {
81 day.reset(); 91 day.reset();
82 } 92 }
83 93
84 void Date::SetYear(Date::Year y) { 94 void Date::SetYear(Date::Year y)
95 {
85 year.emplace(y); 96 year.emplace(y);
86 } 97 }
87 98
88 void Date::SetMonth(Date::Month m) { 99 void Date::SetMonth(Date::Month m)
100 {
89 month.emplace(m); 101 month.emplace(m);
90 } 102 }
91 103
92 void Date::SetDay(Date::Day d) { 104 void Date::SetDay(Date::Day d)
105 {
93 day.emplace(std::clamp(d, static_cast<Date::Day>(1U), static_cast<Date::Day>(31U))); 106 day.emplace(std::clamp(d, static_cast<Date::Day>(1U), static_cast<Date::Day>(31U)));
94 } 107 }
95 108
96 std::optional<Date::Year> Date::GetYear() const { 109 std::optional<Date::Year> Date::GetYear() const
110 {
97 return year; 111 return year;
98 } 112 }
99 113
100 std::optional<Date::Month> Date::GetMonth() const { 114 std::optional<Date::Month> Date::GetMonth() const
115 {
101 return month; 116 return month;
102 } 117 }
103 118
104 std::optional<Date::Day> Date::GetDay() const { 119 std::optional<Date::Day> Date::GetDay() const
120 {
105 return day; 121 return day;
106 } 122 }
107 123
108 bool Date::IsValid() const { 124 bool Date::IsValid() const
125 {
109 return year.has_value() && month.has_value() && day.has_value(); 126 return year.has_value() && month.has_value() && day.has_value();
110 } 127 }
111 128
112 QDate Date::GetAsQDate() const { 129 QDate Date::GetAsQDate() const
130 {
113 /* QDate doesn't support "missing" values (for good reason), 131 /* QDate doesn't support "missing" values (for good reason),
114 * so we do our best and return what we can. 132 * so we do our best and return what we can.
115 */ 133 */
116 134
117 return QDate(year.value_or(2000), static_cast<unsigned int>(month.value_or(Date::Month::Jan)), day.value_or(1)); 135 return QDate(year.value_or(2000), static_cast<unsigned int>(month.value_or(Date::Month::Jan)), day.value_or(1));
118 } 136 }
119 137
120 nlohmann::json Date::GetAsAniListJson() const { 138 nlohmann::json Date::GetAsAniListJson() const
139 {
121 nlohmann::json json = { 140 nlohmann::json json = {
122 {"year", nullptr}, 141 {"year", nullptr},
123 {"month", nullptr}, 142 {"month", nullptr},
124 {"day", nullptr} 143 {"day", nullptr}
125 }; 144 };