Mercurial > minori
comparison src/core/date.cpp @ 9:5c0397762b53
INCOMPLETE: megacommit :)
| author | Paper <mrpapersonic@gmail.com> |
|---|---|
| date | Sun, 10 Sep 2023 03:59:16 -0400 |
| parents | |
| children | 4b198a111713 |
comparison
equal
deleted
inserted
replaced
| 8:b1f73678ef61 | 9:5c0397762b53 |
|---|---|
| 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 | |
| 9 #define MIN(A, B) \ | |
| 10 ({ \ | |
| 11 __typeof__(A) __a = (A); \ | |
| 12 __typeof__(B) __b = (B); \ | |
| 13 __a < __b ? __a : __b; \ | |
| 14 }) | |
| 15 #define MAX(A, B) \ | |
| 16 ({ \ | |
| 17 __typeof__(A) __a = (A); \ | |
| 18 __typeof__(B) __b = (B); \ | |
| 19 __a < __b ? __b : __a; \ | |
| 20 }) | |
| 21 | |
| 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); \ | |
| 28 }) | |
| 29 | |
| 30 Date::Date() { | |
| 31 } | |
| 32 | |
| 33 Date::Date(int32_t y) { | |
| 34 year = std::make_unique<int32_t>(MAX(0, y)); | |
| 35 } | |
| 36 | |
| 37 Date::Date(int32_t y, int8_t m, int8_t d) { | |
| 38 year = std::make_unique<int32_t>(MAX(0, y)); | |
| 39 month = std::make_unique<int8_t>(CLAMP(m, 1, 12)); | |
| 40 day = std::make_unique<int8_t>(CLAMP(d, 1, 31)); | |
| 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) { | |
| 56 year = std::make_unique<int32_t>(MAX(0, y)); | |
| 57 } | |
| 58 | |
| 59 void Date::SetMonth(int8_t m) { | |
| 60 month = std::make_unique<int8_t>(CLAMP(m, 1, 12)); | |
| 61 } | |
| 62 | |
| 63 void Date::SetDay(int8_t d) { | |
| 64 day = std::make_unique<int8_t>(CLAMP(d, 1, 31)); | |
| 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 | |
| 88 bool Date::operator<(const Date& other) const { | |
| 89 int o_y = other.GetYear(), o_m = other.GetMonth(), o_d = other.GetDay(); | |
| 90 return std::tie(*year, *month, *day) < std::tie(o_y, o_m, o_d); | |
| 91 } | |
| 92 | |
| 93 bool Date::operator>(const Date& other) const { | |
| 94 return other < (*this); | |
| 95 } | |
| 96 | |
| 97 bool Date::operator<=(const Date& other) const { | |
| 98 return !((*this) > other); | |
| 99 } | |
| 100 | |
| 101 bool Date::operator>=(const Date& other) const { | |
| 102 return !((*this) < other); | |
| 103 } | |
| 104 | |
| 105 QDate Date::GetAsQDate() const { | |
| 106 return QDate(*year, *month, *day); | |
| 107 } | |
| 108 | |
| 109 nlohmann::json Date::GetAsAniListJson() const { | |
| 110 nlohmann::json result; | |
| 111 if (year.get()) | |
| 112 result.insert(result.end(), {"year", *year}); | |
| 113 else | |
| 114 result.insert(result.end(), {"year", nullptr}); | |
| 115 if (month.get()) | |
| 116 result.insert(result.end(), {"month", *month}); | |
| 117 else | |
| 118 result.insert(result.end(), {"month", nullptr}); | |
| 119 if (day.get()) | |
| 120 result.insert(result.end(), {"day", *day}); | |
| 121 else | |
| 122 result.insert(result.end(), {"day", nullptr}); | |
| 123 return result; | |
| 124 } |
