mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-06-11 18:20:43 +09:00
LibCore: Moved cal.cpp functions to DateTime
This commit is contained in:
parent
46a897b59b
commit
08a30a4961
Notes:
sideshowbarker
2024-07-19 08:16:13 +09:00
Author: https://github.com/RyanGrieb
Commit: 08a30a4961
Pull-request: https://github.com/SerenityOS/serenity/pull/1413
Reviewed-by: https://github.com/awesomekling
Reviewed-by: https://github.com/shannonbooth
Reviewed-by: https://github.com/xTibor
3 changed files with 75 additions and 23 deletions
|
@ -36,6 +36,22 @@ DateTime DateTime::now()
|
|||
return from_timestamp(time(nullptr));
|
||||
}
|
||||
|
||||
DateTime DateTime::create(unsigned year, unsigned month, unsigned day, unsigned hour, unsigned minute, unsigned second)
|
||||
{
|
||||
DateTime dt;
|
||||
dt.m_year = year;
|
||||
dt.m_month = month;
|
||||
dt.m_day = day;
|
||||
dt.m_hour = hour;
|
||||
dt.m_minute = minute;
|
||||
dt.m_second = second;
|
||||
|
||||
struct tm tm = { (int)second, (int)minute, (int)hour, (int)day, (int)month, (int)year, (int)dt.weekday(), (int)dt.day_of_year(), -1 };
|
||||
dt.m_timestamp = mktime(&tm);
|
||||
|
||||
return dt;
|
||||
}
|
||||
|
||||
DateTime DateTime::from_timestamp(time_t timestamp)
|
||||
{
|
||||
struct tm tm;
|
||||
|
@ -51,6 +67,55 @@ DateTime DateTime::from_timestamp(time_t timestamp)
|
|||
return dt;
|
||||
}
|
||||
|
||||
unsigned DateTime::weekday() const
|
||||
{
|
||||
int target_year = m_year;
|
||||
static const int seek_table[] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };
|
||||
if (m_month < 3)
|
||||
--target_year;
|
||||
|
||||
return (target_year + target_year / 4 - target_year / 100 + target_year / 400 + seek_table[m_month - 1] + m_day) % 7;
|
||||
}
|
||||
|
||||
unsigned DateTime::days_in_month() const
|
||||
{
|
||||
bool is_long_month = (m_month == 1 || m_month == 3 || m_month == 5 || m_month == 7 || m_month == 8 || m_month == 10 || m_month == 12);
|
||||
|
||||
if (m_month == 2)
|
||||
return is_leap_year() ? 29 : 28;
|
||||
|
||||
return is_long_month ? 31 : 30;
|
||||
}
|
||||
|
||||
unsigned DateTime::day_of_year() const
|
||||
{
|
||||
static const int seek_table[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
|
||||
int day_of_year = seek_table[m_month - 1] + m_day;
|
||||
|
||||
if (is_leap_year() && m_month > 3)
|
||||
day_of_year++;
|
||||
|
||||
return day_of_year - 1;
|
||||
}
|
||||
|
||||
bool DateTime::is_leap_year() const
|
||||
{
|
||||
return ((m_year % 400 == 0) || (m_year % 4 == 0 && m_year % 100 != 0));
|
||||
}
|
||||
|
||||
void DateTime::set_time(unsigned year, unsigned month, unsigned day, unsigned hour, unsigned minute, unsigned second)
|
||||
{
|
||||
m_year = year;
|
||||
m_month = month;
|
||||
m_day = day;
|
||||
m_hour = hour;
|
||||
m_minute = minute;
|
||||
m_second = second;
|
||||
|
||||
struct tm tm = { (int)second, (int)minute, (int)hour, (int)day, (int)month, (int)year, (int)weekday(), (int)day_of_year(), -1 };
|
||||
m_timestamp = mktime(&tm);
|
||||
}
|
||||
|
||||
String DateTime::to_string(const String& format) const
|
||||
{
|
||||
|
||||
|
|
|
@ -42,9 +42,15 @@ public:
|
|||
unsigned hour() const { return m_hour; }
|
||||
unsigned minute() const { return m_minute; }
|
||||
unsigned second() const { return m_second; }
|
||||
unsigned weekday() const;
|
||||
unsigned days_in_month() const;
|
||||
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);
|
||||
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 now();
|
||||
static DateTime from_timestamp(time_t);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue