Mercurial > minori
comparison src/core/date.cc @ 81:9b2b41f83a5e
boring: mass rename to cc
because this is a very unix-y project, it makes more sense to use the
'cc' extension
| author | Paper <mrpapersonic@gmail.com> |
|---|---|
| date | Mon, 23 Oct 2023 12:07:27 -0400 |
| parents | src/core/date.cpp@6f7385bd334c |
| children | f88eda79c60a |
comparison
equal
deleted
inserted
replaced
| 80:825506f0e221 | 81:9b2b41f83a5e |
|---|---|
| 1 #include "core/date.h" | |
| 2 #include "core/json.h" | |
| 3 #include <QDate> | |
| 4 #include <QDebug> | |
| 5 #include <algorithm> | |
| 6 #include <cstdint> | |
| 7 #include <tuple> | |
| 8 | |
| 9 /* An implementation of AniList's "fuzzy date" */ | |
| 10 | |
| 11 #define CLAMP(x, low, high) (std::max(low, std::min(high, x))) | |
| 12 | |
| 13 Date::Date() { | |
| 14 } | |
| 15 | |
| 16 Date::Date(unsigned int y) { | |
| 17 SetYear(y); | |
| 18 } | |
| 19 | |
| 20 Date::Date(unsigned int y, unsigned int m, unsigned int d) { | |
| 21 SetYear(y); | |
| 22 SetMonth(m); | |
| 23 SetDay(d); | |
| 24 } | |
| 25 | |
| 26 Date::Date(const QDate& date) { | |
| 27 SetYear(date.year()); | |
| 28 SetMonth(date.month()); | |
| 29 SetDay(date.day()); | |
| 30 } | |
| 31 | |
| 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 | |
| 44 void Date::SetYear(unsigned int y) { | |
| 45 year.reset(new unsigned int(y)); | |
| 46 } | |
| 47 | |
| 48 void Date::SetMonth(unsigned int m) { | |
| 49 month.reset(new unsigned int(CLAMP(m, 1U, 12U))); | |
| 50 } | |
| 51 | |
| 52 void Date::SetDay(unsigned int d) { | |
| 53 day.reset(new unsigned int(CLAMP(d, 1U, 31U))); | |
| 54 } | |
| 55 | |
| 56 unsigned int Date::GetYear() const { | |
| 57 unsigned int* ptr = year.get(); | |
| 58 if (ptr != nullptr) | |
| 59 return *year; | |
| 60 return -1; | |
| 61 } | |
| 62 | |
| 63 unsigned int Date::GetMonth() const { | |
| 64 unsigned int* ptr = month.get(); | |
| 65 if (ptr != nullptr) | |
| 66 return *month; | |
| 67 return -1; | |
| 68 } | |
| 69 | |
| 70 unsigned int Date::GetDay() const { | |
| 71 unsigned int* ptr = day.get(); | |
| 72 if (ptr != nullptr) | |
| 73 return *day; | |
| 74 return -1; | |
| 75 } | |
| 76 | |
| 77 bool Date::IsValid() const { | |
| 78 return year.get() && month.get() && day.get(); | |
| 79 } | |
| 80 | |
| 81 bool Date::operator<(const Date& other) const { | |
| 82 unsigned int y = GetYear(), m = GetMonth(), d = GetDay(); | |
| 83 unsigned int o_y = other.GetYear(), o_m = other.GetMonth(), o_d = other.GetDay(); | |
| 84 return std::tie(y, m, d) < std::tie(o_y, o_m, o_d); | |
| 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 { | |
| 100 /* QDates don't support "missing" values, for good reason. */ | |
| 101 if (IsValid()) | |
| 102 return QDate(*year, *month, *day); | |
| 103 else | |
| 104 return QDate(); | |
| 105 } | |
| 106 | |
| 107 nlohmann::json Date::GetAsAniListJson() const { | |
| 108 nlohmann::json result = {}; | |
| 109 if (year.get()) | |
| 110 result["year"] = *year; | |
| 111 else | |
| 112 result["year"] = nullptr; | |
| 113 if (month.get()) | |
| 114 result["month"] = *month; | |
| 115 else | |
| 116 result["month"] = nullptr; | |
| 117 if (day.get()) | |
| 118 result["day"] = *day; | |
| 119 else | |
| 120 result["day"] = nullptr; | |
| 121 return result; | |
| 122 } |
