From 95bc8e46411b10edb62739178b51d57b1ed2064d Mon Sep 17 00:00:00 2001 From: Idan Horowitz Date: Thu, 19 Aug 2021 19:16:03 +0300 Subject: [PATCH] LibCore: Make DateTime's members signed Core::DateTime is essentially a C++ wrapper of the tm struct, so we should support the same signed range as the underlying tm. --- AK/Time.h | 2 +- Userland/Libraries/LibCore/DateTime.cpp | 16 ++++++++-------- Userland/Libraries/LibCore/DateTime.h | 16 ++++++++-------- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/AK/Time.h b/AK/Time.h index 93d7dc1eeba..7995dfb2a41 100644 --- a/AK/Time.h +++ b/AK/Time.h @@ -48,7 +48,7 @@ inline bool is_leap_year(int year) return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0); } -inline unsigned days_in_year(int year) +inline int days_in_year(int year) { return 365 + is_leap_year(year); } diff --git a/Userland/Libraries/LibCore/DateTime.cpp b/Userland/Libraries/LibCore/DateTime.cpp index cf606e431d5..cc433e0aba9 100644 --- a/Userland/Libraries/LibCore/DateTime.cpp +++ b/Userland/Libraries/LibCore/DateTime.cpp @@ -18,7 +18,7 @@ DateTime DateTime::now() return from_timestamp(time(nullptr)); } -DateTime DateTime::create(unsigned year, unsigned month, unsigned day, unsigned hour, unsigned minute, unsigned second) +DateTime DateTime::create(int year, int month, int day, int hour, int minute, int second) { DateTime dt; dt.set_time(year, month, day, hour, minute, second); @@ -60,15 +60,15 @@ bool DateTime::is_leap_year() const return ::is_leap_year(m_year); } -void DateTime::set_time(unsigned year, unsigned month, unsigned day, unsigned hour, unsigned minute, unsigned second) +void DateTime::set_time(int year, int month, int day, int hour, int minute, int second) { struct tm tm = {}; - tm.tm_sec = (int)second; - tm.tm_min = (int)minute; - tm.tm_hour = (int)hour; - tm.tm_mday = (int)day; - tm.tm_mon = (int)month - 1; - tm.tm_year = (int)year - 1900; + tm.tm_sec = second; + tm.tm_min = minute; + tm.tm_hour = hour; + tm.tm_mday = day; + tm.tm_mon = month - 1; + tm.tm_year = year - 1900; tm.tm_isdst = -1; // mktime() doesn't read tm.tm_wday and tm.tm_yday, no need to fill them in. diff --git a/Userland/Libraries/LibCore/DateTime.h b/Userland/Libraries/LibCore/DateTime.h index 2fc43cb1645..46b6014ade1 100644 --- a/Userland/Libraries/LibCore/DateTime.h +++ b/Userland/Libraries/LibCore/DateTime.h @@ -29,10 +29,10 @@ public: unsigned day_of_year() const; bool is_leap_year() const; - void set_time(unsigned year, unsigned month = 1, unsigned day = 0, unsigned hour = 0, unsigned minute = 0, unsigned second = 0); + void set_time(int year, int month = 1, int day = 0, int hour = 0, int minute = 0, int second = 0); String to_string(const String& format = "%Y-%m-%d %H:%M:%S") const; - static DateTime create(unsigned year, unsigned month = 1, unsigned day = 0, unsigned hour = 0, unsigned minute = 0, unsigned second = 0); + static DateTime create(int year, int month = 1, int day = 0, int hour = 0, int minute = 0, int second = 0); static DateTime now(); static DateTime from_timestamp(time_t); static Optional parse(const String& format, const String& string); @@ -41,12 +41,12 @@ public: private: time_t m_timestamp { 0 }; - unsigned m_year { 0 }; - unsigned m_month { 0 }; - unsigned m_day { 0 }; - unsigned m_hour { 0 }; - unsigned m_minute { 0 }; - unsigned m_second { 0 }; + int m_year { 0 }; + int m_month { 0 }; + int m_day { 0 }; + int m_hour { 0 }; + int m_minute { 0 }; + int m_second { 0 }; }; }