Mercurial > minori
annotate src/core/date.cpp @ 60:d417e9381ca5
filesystem: WIP class-ification of paths
| author | Paper <mrpapersonic@gmail.com> |
|---|---|
| date | Fri, 29 Sep 2023 13:52:50 -0400 |
| parents | 75c804f713b2 |
| children | 3d2decf093bb |
| rev | line source |
|---|---|
| 9 | 1 #include "core/date.h" |
| 2 #include "core/json.h" | |
| 3 #include <QDate> | |
| 51 | 4 #include <QDebug> |
| 9 | 5 #include <cstdint> |
| 6 #include <tuple> | |
| 7 | |
| 8 /* An implementation of AniList's "fuzzy date" */ | |
| 9 | |
| 36 | 10 #define MIN(A, B) \ |
| 11 ({ \ | |
| 12 __typeof__(A) __a = (A); \ | |
| 13 __typeof__(B) __b = (B); \ | |
| 14 __a < __b ? __a : __b; \ | |
| 9 | 15 }) |
| 36 | 16 #define MAX(A, B) \ |
| 17 ({ \ | |
| 18 __typeof__(A) __a = (A); \ | |
| 19 __typeof__(B) __b = (B); \ | |
| 20 __a < __b ? __b : __a; \ | |
| 9 | 21 }) |
| 22 | |
| 36 | 23 #define CLAMP(x, low, high) \ |
| 24 ({ \ | |
| 25 __typeof__(x) __x = (x); \ | |
| 26 __typeof__(low) __low = (low); \ | |
| 27 __typeof__(high) __high = (high); \ | |
| 28 __x > __high ? __high : (__x < __low ? __low : __x); \ | |
| 9 | 29 }) |
| 30 | |
| 31 Date::Date() { | |
| 32 } | |
| 33 | |
| 51 | 34 Date::Date(unsigned int y) { |
| 11 | 35 SetYear(y); |
| 9 | 36 } |
| 37 | |
| 51 | 38 Date::Date(unsigned int y, unsigned int m, unsigned int d) { |
| 11 | 39 SetYear(y); |
| 40 SetMonth(m); | |
| 41 SetDay(d); | |
| 9 | 42 } |
| 43 | |
| 51 | 44 Date::Date(const QDate& date) { |
| 45 SetYear(date.year()); | |
| 46 SetMonth(date.month()); | |
| 47 SetDay(date.day()); | |
| 48 } | |
| 49 | |
| 9 | 50 void Date::VoidYear() { |
| 51 year.reset(); | |
| 52 } | |
| 53 | |
| 54 void Date::VoidMonth() { | |
| 55 month.reset(); | |
| 56 } | |
| 57 | |
| 58 void Date::VoidDay() { | |
| 59 day.reset(); | |
| 60 } | |
| 61 | |
| 51 | 62 void Date::SetYear(unsigned int y) { |
| 63 year.reset(new unsigned int(MAX(0U, y))); | |
| 9 | 64 } |
| 65 | |
| 51 | 66 void Date::SetMonth(unsigned int m) { |
| 67 month.reset(new unsigned int(CLAMP(m, 1U, 12U))); | |
| 9 | 68 } |
| 69 | |
| 51 | 70 void Date::SetDay(unsigned int d) { |
| 71 day.reset(new unsigned int(CLAMP(d, 1U, 31U))); | |
| 9 | 72 } |
| 73 | |
| 51 | 74 unsigned int Date::GetYear() const { |
| 75 unsigned int* ptr = year.get(); | |
| 9 | 76 if (ptr != nullptr) |
| 77 return *year; | |
| 78 return -1; | |
| 79 } | |
| 80 | |
| 51 | 81 unsigned int Date::GetMonth() const { |
| 82 unsigned int* ptr = month.get(); | |
| 9 | 83 if (ptr != nullptr) |
| 84 return *month; | |
| 85 return -1; | |
| 86 } | |
| 87 | |
| 51 | 88 unsigned int Date::GetDay() const { |
| 89 unsigned int* ptr = day.get(); | |
| 9 | 90 if (ptr != nullptr) |
| 91 return *day; | |
| 92 return -1; | |
| 93 } | |
| 94 | |
|
47
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
95 bool Date::IsValid() const { |
|
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
96 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
|
97 } |
|
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
98 |
| 9 | 99 bool Date::operator<(const Date& other) const { |
| 51 | 100 unsigned int y = GetYear(), m = GetMonth(), d = GetDay(); |
| 101 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
|
102 return std::tie(y, m, d) < std::tie(o_y, o_m, o_d); |
| 9 | 103 } |
| 104 | |
| 105 bool Date::operator>(const Date& other) const { | |
| 106 return other < (*this); | |
| 107 } | |
| 108 | |
| 109 bool Date::operator<=(const Date& other) const { | |
| 110 return !((*this) > other); | |
| 111 } | |
| 112 | |
| 113 bool Date::operator>=(const Date& other) const { | |
| 114 return !((*this) < other); | |
| 115 } | |
| 116 | |
| 117 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
|
118 /* 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
|
119 if (IsValid()) |
| 10 | 120 return QDate(*year, *month, *day); |
| 15 | 121 else |
| 122 return QDate(); | |
| 9 | 123 } |
| 124 | |
| 125 nlohmann::json Date::GetAsAniListJson() const { | |
| 126 nlohmann::json result; | |
| 127 if (year.get()) | |
| 128 result.insert(result.end(), {"year", *year}); | |
| 129 else | |
| 130 result.insert(result.end(), {"year", nullptr}); | |
| 131 if (month.get()) | |
| 132 result.insert(result.end(), {"month", *month}); | |
| 133 else | |
| 134 result.insert(result.end(), {"month", nullptr}); | |
| 135 if (day.get()) | |
| 136 result.insert(result.end(), {"day", *day}); | |
| 137 else | |
| 138 result.insert(result.end(), {"day", nullptr}); | |
| 139 return result; | |
| 140 } |
