diff src/core/date.cc @ 258:862d0d8619f6

*: HUUUGE changes animia has been renamed to animone, so instead of thinking of a health condition, you think of a beautiful flower :) I've also edited some of the code for animone, but I have no idea if it even works or not because I don't have a mac or windows machine lying around. whoops! ... anyway, all of the changes divergent from Anisthesia are now licensed under BSD. it's possible that I could even rewrite most of the code to where I don't even have to keep the MIT license, but that's thinking too far into the future I've been slacking off on implementing the anime seasons page, mostly out of laziness. I think I'd have to create another db file specifically for the seasons anyway, this code is being pushed *primarily* because the hard drive it's on is failing! yay :)
author Paper <paper@paper.us.eu.org>
date Mon, 01 Apr 2024 02:43:44 -0400
parents bc1ae1810855
children b1f4d1867ab1
line wrap: on
line diff
--- a/src/core/date.cc	Sun Feb 18 16:02:14 2024 -0500
+++ b/src/core/date.cc	Mon Apr 01 02:43:44 2024 -0400
@@ -11,11 +11,11 @@
 Date::Date() {
 }
 
-Date::Date(unsigned int y) {
+Date::Date(Date::Year y) {
 	SetYear(y);
 }
 
-Date::Date(unsigned int y, unsigned int m, unsigned int d) {
+Date::Date(Date::Year y, Date::Month m, Date::Day d) {
 	SetYear(y);
 	SetMonth(m);
 	SetDay(d);
@@ -23,21 +23,28 @@
 
 Date::Date(const QDate& date) {
 	SetYear(date.year());
-	SetMonth(date.month());
+	auto m = date.month();
+	m = std::clamp(m, static_cast<decltype(m)>(Date::Month::Jan), static_cast<decltype(m)>(Date::Month::Dec));
+	SetMonth(static_cast<Date::Month>(m));
 	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. */
+	 * 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("/month"_json_pointer) && json.at("/month"_json_pointer).is_number()) {
+		auto m = json.at("/month"_json_pointer).get<unsigned int>();
+		m = std::clamp(m, static_cast<decltype(m)>(Date::Month::Jan), static_cast<decltype(m)>(Date::Month::Dec));
+		SetMonth(static_cast<Date::Month>(m));
+	}
+
 	if (json.contains("/day"_json_pointer) && json.at("/day"_json_pointer).is_number())
-		SetDay(json.at("/day"_json_pointer).get<unsigned int>());
+		SetDay(json.at("/day"_json_pointer).get<unsigned char>());
 }
 
 void Date::VoidYear() {
@@ -52,27 +59,27 @@
 	day.reset();
 }
 
-void Date::SetYear(unsigned int y) {
+void Date::SetYear(Date::Year y) {
 	year.emplace(y);
 }
 
-void Date::SetMonth(unsigned int m) {
-	month.emplace(std::clamp(m, 1U, 12U));
+void Date::SetMonth(Date::Month m) {
+	month.emplace(m);
 }
 
-void Date::SetDay(unsigned int d) {
-	day.emplace(std::clamp(d, 1U, 31U));
+void Date::SetDay(Date::Day d) {
+	day.emplace(std::clamp(d, static_cast<Date::Day>(1U), static_cast<Date::Day>(31U)));
 }
 
-std::optional<unsigned int> Date::GetYear() const {
+std::optional<Date::Year> Date::GetYear() const {
 	return year;
 }
 
-std::optional<unsigned int> Date::GetMonth() const {
+std::optional<Date::Month> Date::GetMonth() const {
 	return month;
 }
 
-std::optional<unsigned int> Date::GetDay() const {
+std::optional<Date::Day> Date::GetDay() const {
 	return day;
 }
 
@@ -83,15 +90,26 @@
 QDate Date::GetAsQDate() const {
 	/* 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));
+	return QDate(year.value_or(2000), static_cast<unsigned int>(month.value_or(Date::Month::Jan)), day.value_or(1));
 }
 
 nlohmann::json Date::GetAsAniListJson() const {
-	return {
-		{"year", year},
-		{"month", month},
-		{"day", day}
-	};
+	nlohmann::json json = {
+	    {"year",  nullptr},
+        {"month", nullptr},
+        {"day",   nullptr}
+    };
+
+	if (year)
+		json["year"] = static_cast<unsigned int>(year.value());
+
+	if (month)
+		json["month"] = static_cast<unsigned char>(month.value());
+
+	if (day)
+		json["day"] = static_cast<unsigned char>(day.value());
+
+	return json;
 }