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