Mercurial > minori
diff src/core/date.cc @ 202:71832ffe425a
animia: re-add kvm fd source
this is all being merged from my wildly out-of-date laptop. SORRY!
in other news, I edited the CI file to install the wayland client
as well, so the linux CI build might finally get wayland stuff.
author | Paper <paper@paper.us.eu.org> |
---|---|
date | Tue, 02 Jan 2024 06:05:06 -0500 |
parents | bc1ae1810855 |
children | 862d0d8619f6 |
line wrap: on
line diff
--- a/src/core/date.cc Sun Nov 19 19:13:28 2023 -0500 +++ b/src/core/date.cc Tue Jan 02 06:05:06 2024 -0500 @@ -1,15 +1,13 @@ #include "core/date.h" #include "core/json.h" + #include <QDate> #include <QDebug> + #include <algorithm> -#include <cstdint> -#include <tuple> /* An implementation of AniList's "fuzzy date" */ -#define CLAMP(x, low, high) (std::max(low, std::min(high, x))) - Date::Date() { } @@ -29,6 +27,19 @@ SetDay(date.day()); } +Date::Date(const nlohmann::json& json) { + /* NOTE: this constructor is made for use with + AniList FuzzyDate-style JSON. In the future, some other + methods may be parsed and whatnot if necessary. */ + + if (json.contains("/year"_json_pointer) && json.at("/year"_json_pointer).is_number()) + SetYear(json.at("/year"_json_pointer).get<unsigned int>()); + if (json.contains("/month"_json_pointer) && json.at("/month"_json_pointer).is_number()) + SetMonth(json.at("/month"_json_pointer).get<unsigned int>()); + if (json.contains("/day"_json_pointer) && json.at("/day"_json_pointer).is_number()) + SetDay(json.at("/day"_json_pointer).get<unsigned int>()); +} + void Date::VoidYear() { year.reset(); } @@ -42,81 +53,45 @@ } void Date::SetYear(unsigned int y) { - year.reset(new unsigned int(y)); + year.emplace(y); } void Date::SetMonth(unsigned int m) { - month.reset(new unsigned int(CLAMP(m, 1U, 12U))); + month.emplace(std::clamp(m, 1U, 12U)); } void Date::SetDay(unsigned int d) { - day.reset(new unsigned int(CLAMP(d, 1U, 31U))); + day.emplace(std::clamp(d, 1U, 31U)); } -unsigned int Date::GetYear() const { - unsigned int* ptr = year.get(); - if (ptr != nullptr) - return *year; - return -1; +std::optional<unsigned int> Date::GetYear() const { + return year; } -unsigned int Date::GetMonth() const { - unsigned int* ptr = month.get(); - if (ptr != nullptr) - return *month; - return -1; +std::optional<unsigned int> Date::GetMonth() const { + return month; } -unsigned int Date::GetDay() const { - unsigned int* ptr = day.get(); - if (ptr != nullptr) - return *day; - return -1; +std::optional<unsigned int> Date::GetDay() const { + return day; } bool Date::IsValid() const { - return year.get() && month.get() && day.get(); -} - -bool Date::operator<(const Date& other) const { - unsigned int y = GetYear(), m = GetMonth(), d = GetDay(); - unsigned int o_y = other.GetYear(), o_m = other.GetMonth(), o_d = other.GetDay(); - return std::tie(y, m, d) < std::tie(o_y, o_m, o_d); -} - -bool Date::operator>(const Date& other) const { - return other < (*this); -} - -bool Date::operator<=(const Date& other) const { - return !((*this) > other); -} - -bool Date::operator>=(const Date& other) const { - return !((*this) < other); + return year.has_value() && month.has_value() && day.has_value(); } QDate Date::GetAsQDate() const { - /* QDates don't support "missing" values, for good reason. */ - if (IsValid()) - return QDate(*year, *month, *day); - else - return QDate(); + /* QDate doesn't support "missing" values (for good reason), + * so we do our best and return what we can. + */ + + return QDate(year.value_or(2000), month.value_or(1), day.value_or(1)); } nlohmann::json Date::GetAsAniListJson() const { - nlohmann::json result = {}; - if (year.get()) - result["year"] = *year; - else - result["year"] = nullptr; - if (month.get()) - result["month"] = *month; - else - result["month"] = nullptr; - if (day.get()) - result["day"] = *day; - else - result["day"] = nullptr; - return result; + return { + {"year", year}, + {"month", month}, + {"day", day} + }; }