comparison src/core/date.cpp @ 51:75c804f713b2

window: add about window, *: use tr() when applicable (useful for i18n)
author Paper <mrpapersonic@gmail.com>
date Mon, 25 Sep 2023 20:29:26 -0400
parents d8eb763e6661
children 3d2decf093bb
comparison
equal deleted inserted replaced
50:10868c3fb2be 51:75c804f713b2
1 #include "core/date.h" 1 #include "core/date.h"
2 #include "core/json.h" 2 #include "core/json.h"
3 #include <QDate> 3 #include <QDate>
4 #include <QDebug>
4 #include <cstdint> 5 #include <cstdint>
5 #include <tuple> 6 #include <tuple>
6 7
7 /* An implementation of AniList's "fuzzy date" */ 8 /* An implementation of AniList's "fuzzy date" */
8 9
28 }) 29 })
29 30
30 Date::Date() { 31 Date::Date() {
31 } 32 }
32 33
33 Date::Date(int32_t y) { 34 Date::Date(unsigned int y) {
34 SetYear(y); 35 SetYear(y);
35 } 36 }
36 37
37 Date::Date(int32_t y, int8_t m, int8_t d) { 38 Date::Date(unsigned int y, unsigned int m, unsigned int d) {
38 SetYear(y); 39 SetYear(y);
39 SetMonth(m); 40 SetMonth(m);
40 SetDay(d); 41 SetDay(d);
42 }
43
44 Date::Date(const QDate& date) {
45 SetYear(date.year());
46 SetMonth(date.month());
47 SetDay(date.day());
41 } 48 }
42 49
43 void Date::VoidYear() { 50 void Date::VoidYear() {
44 year.reset(); 51 year.reset();
45 } 52 }
50 57
51 void Date::VoidDay() { 58 void Date::VoidDay() {
52 day.reset(); 59 day.reset();
53 } 60 }
54 61
55 void Date::SetYear(int32_t y) { 62 void Date::SetYear(unsigned int y) {
56 year.reset(new int32_t(MAX(0, y))); 63 year.reset(new unsigned int(MAX(0U, y)));
57 } 64 }
58 65
59 void Date::SetMonth(int8_t m) { 66 void Date::SetMonth(unsigned int m) {
60 month.reset(new int8_t(CLAMP(m, 1, 12))); 67 month.reset(new unsigned int(CLAMP(m, 1U, 12U)));
61 } 68 }
62 69
63 void Date::SetDay(int8_t d) { 70 void Date::SetDay(unsigned int d) {
64 day.reset(new int8_t(CLAMP(d, 1, 31))); 71 day.reset(new unsigned int(CLAMP(d, 1U, 31U)));
65 } 72 }
66 73
67 int32_t Date::GetYear() const { 74 unsigned int Date::GetYear() const {
68 int32_t* ptr = year.get(); 75 unsigned int* ptr = year.get();
69 if (ptr != nullptr) 76 if (ptr != nullptr)
70 return *year; 77 return *year;
71 return -1; 78 return -1;
72 } 79 }
73 80
74 int8_t Date::GetMonth() const { 81 unsigned int Date::GetMonth() const {
75 int8_t* ptr = month.get(); 82 unsigned int* ptr = month.get();
76 if (ptr != nullptr) 83 if (ptr != nullptr)
77 return *month; 84 return *month;
78 return -1; 85 return -1;
79 } 86 }
80 87
81 int8_t Date::GetDay() const { 88 unsigned int Date::GetDay() const {
82 int8_t* ptr = day.get(); 89 unsigned int* ptr = day.get();
83 if (ptr != nullptr) 90 if (ptr != nullptr)
84 return *day; 91 return *day;
85 return -1; 92 return -1;
86 } 93 }
87 94
88 bool Date::IsValid() const { 95 bool Date::IsValid() const {
89 return year.get() && month.get() && day.get(); 96 return year.get() && month.get() && day.get();
90 } 97 }
91 98
92 bool Date::operator<(const Date& other) const { 99 bool Date::operator<(const Date& other) const {
93 int y = GetYear(), m = GetMonth(), d = GetDay(); 100 unsigned int y = GetYear(), m = GetMonth(), d = GetDay();
94 int o_y = other.GetYear(), o_m = other.GetMonth(), o_d = other.GetDay(); 101 unsigned int o_y = other.GetYear(), o_m = other.GetMonth(), o_d = other.GetDay();
95 return std::tie(y, m, d) < std::tie(o_y, o_m, o_d); 102 return std::tie(y, m, d) < std::tie(o_y, o_m, o_d);
96 } 103 }
97 104
98 bool Date::operator>(const Date& other) const { 105 bool Date::operator>(const Date& other) const {
99 return other < (*this); 106 return other < (*this);