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