2020-02-11 19:42:02 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-02-11 19:42:02 +01:00
|
|
|
*/
|
|
|
|
|
2020-03-08 18:35:26 +08:00
|
|
|
#include <AK/StringBuilder.h>
|
2020-08-25 16:38:24 -04:00
|
|
|
#include <AK/Time.h>
|
2020-02-11 19:42:02 +01:00
|
|
|
#include <LibCore/DateTime.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
namespace Core {
|
|
|
|
|
|
|
|
DateTime DateTime::now()
|
|
|
|
{
|
2020-02-11 19:48:46 +01:00
|
|
|
return from_timestamp(time(nullptr));
|
|
|
|
}
|
|
|
|
|
2020-03-10 16:41:01 -05:00
|
|
|
DateTime DateTime::create(unsigned year, unsigned month, unsigned day, unsigned hour, unsigned minute, unsigned second)
|
|
|
|
{
|
|
|
|
DateTime dt;
|
2020-08-24 09:23:46 -04:00
|
|
|
dt.set_time(year, month, day, hour, minute, second);
|
2020-03-10 16:41:01 -05:00
|
|
|
return dt;
|
|
|
|
}
|
|
|
|
|
2020-02-11 19:48:46 +01:00
|
|
|
DateTime DateTime::from_timestamp(time_t timestamp)
|
|
|
|
{
|
2020-02-11 19:42:02 +01:00
|
|
|
struct tm tm;
|
|
|
|
localtime_r(×tamp, &tm);
|
|
|
|
DateTime dt;
|
|
|
|
dt.m_year = tm.tm_year + 1900;
|
|
|
|
dt.m_month = tm.tm_mon + 1;
|
|
|
|
dt.m_day = tm.tm_mday;
|
|
|
|
dt.m_hour = tm.tm_hour;
|
|
|
|
dt.m_minute = tm.tm_min;
|
|
|
|
dt.m_second = tm.tm_sec;
|
|
|
|
dt.m_timestamp = timestamp;
|
|
|
|
return dt;
|
|
|
|
}
|
|
|
|
|
2020-03-10 16:41:01 -05:00
|
|
|
unsigned DateTime::weekday() const
|
|
|
|
{
|
2020-08-25 20:32:32 -04:00
|
|
|
return ::day_of_week(m_year, m_month, m_day);
|
2020-03-10 16:41:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned DateTime::days_in_month() const
|
|
|
|
{
|
2020-08-25 20:11:12 -04:00
|
|
|
return ::days_in_month(m_year, m_month);
|
2020-03-10 16:41:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned DateTime::day_of_year() const
|
|
|
|
{
|
2020-08-25 19:19:16 -04:00
|
|
|
return ::day_of_year(m_year, m_month, m_day);
|
2020-03-10 16:41:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
bool DateTime::is_leap_year() const
|
|
|
|
{
|
2020-08-25 16:38:24 -04:00
|
|
|
return ::is_leap_year(m_year);
|
2020-03-10 16:41:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void DateTime::set_time(unsigned year, unsigned month, unsigned day, unsigned hour, unsigned minute, unsigned second)
|
|
|
|
{
|
2020-03-23 13:13:36 +01:00
|
|
|
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;
|
LibCore: Let DateTime::create()/set_time() take summer time into account
DateTime::create() takes a date/time in local time, but it set
tm_isdst to 0, which meant it was in local winter time always.
Set tm_isdst to -1 so that times during summer time are treated
in summer time, and times in winter time are treated as winter
time (when appropriate). When the time is adjusted backward by
one hour, the same time can be in winter time or summer time,
so this isn't 100% reliable, but for most of the year it should
work fine.
Since LibJS uses DateTime, this means that the Date tuple
ctor (which creates a timestamp from year/month/day/hours/etc
in local time) and getTime() should now have consistent (and
correct) output, which should fix #3327.
In Serenity itself, dst handling (and timezones) are unimplemented
and this doens't have any effect yet, but in Lagom this has an effect.
2020-08-30 09:39:16 -04:00
|
|
|
tm.tm_isdst = -1;
|
2020-08-24 09:24:44 -04:00
|
|
|
// mktime() doesn't read tm.tm_wday and tm.tm_yday, no need to fill them in.
|
|
|
|
|
2020-03-10 16:41:01 -05:00
|
|
|
m_timestamp = mktime(&tm);
|
2020-08-24 09:24:44 -04:00
|
|
|
|
|
|
|
// mktime() normalizes the components to the right ranges (Jan 32 -> Feb 1 etc), so read fields back out from tm.
|
|
|
|
m_year = tm.tm_year + 1900;
|
|
|
|
m_month = tm.tm_mon + 1;
|
|
|
|
m_day = tm.tm_mday;
|
|
|
|
m_hour = tm.tm_hour;
|
|
|
|
m_minute = tm.tm_min;
|
|
|
|
m_second = tm.tm_sec;
|
2020-03-10 16:41:01 -05:00
|
|
|
}
|
|
|
|
|
2020-03-08 18:35:26 +08:00
|
|
|
String DateTime::to_string(const String& format) const
|
2020-02-11 19:42:02 +01:00
|
|
|
{
|
2020-03-08 18:35:26 +08:00
|
|
|
const char wday_short_names[7][4] = {
|
|
|
|
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
|
|
|
|
};
|
|
|
|
const char wday_long_names[7][10] = {
|
|
|
|
"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
|
|
|
|
};
|
|
|
|
const char mon_short_names[12][4] = {
|
|
|
|
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
|
|
|
|
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
|
|
|
|
};
|
|
|
|
const char mon_long_names[12][10] = {
|
|
|
|
"January", "February", "March", "April", "May", "June",
|
2020-08-19 13:53:14 -04:00
|
|
|
"July", "August", "September", "October", "November", "December"
|
2020-03-08 18:35:26 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct tm tm;
|
|
|
|
localtime_r(&m_timestamp, &tm);
|
|
|
|
StringBuilder builder;
|
|
|
|
const int format_len = format.length();
|
|
|
|
|
|
|
|
for (int i = 0; i < format_len; ++i) {
|
|
|
|
if (format[i] != '%') {
|
|
|
|
builder.append(format[i]);
|
|
|
|
} else {
|
|
|
|
if (++i == format_len)
|
|
|
|
return String();
|
|
|
|
|
|
|
|
switch (format[i]) {
|
|
|
|
case 'a':
|
|
|
|
builder.append(wday_short_names[tm.tm_wday]);
|
|
|
|
break;
|
|
|
|
case 'A':
|
|
|
|
builder.append(wday_long_names[tm.tm_wday]);
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
builder.append(mon_short_names[tm.tm_mon]);
|
|
|
|
break;
|
|
|
|
case 'B':
|
|
|
|
builder.append(mon_long_names[tm.tm_mon]);
|
|
|
|
break;
|
|
|
|
case 'C':
|
2021-05-07 10:47:22 +02:00
|
|
|
builder.appendff("{:02}", (tm.tm_year + 1900) / 100);
|
2020-03-08 18:35:26 +08:00
|
|
|
break;
|
|
|
|
case 'd':
|
2021-05-07 10:47:22 +02:00
|
|
|
builder.appendff("{:02}", tm.tm_mday);
|
2020-03-08 18:35:26 +08:00
|
|
|
break;
|
|
|
|
case 'D':
|
2021-05-07 10:47:22 +02:00
|
|
|
builder.appendff("{:02}/{:02}/{:02}", tm.tm_mon + 1, tm.tm_mday, (tm.tm_year + 1900) % 100);
|
2020-03-08 18:35:26 +08:00
|
|
|
break;
|
|
|
|
case 'e':
|
2021-05-07 10:47:22 +02:00
|
|
|
builder.appendff("{:2}", tm.tm_mday);
|
2020-03-08 18:35:26 +08:00
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
builder.append(mon_short_names[tm.tm_mon]);
|
|
|
|
break;
|
|
|
|
case 'H':
|
2021-05-07 10:47:22 +02:00
|
|
|
builder.appendff("{:02}", tm.tm_hour);
|
2020-03-08 18:35:26 +08:00
|
|
|
break;
|
|
|
|
case 'I':
|
2021-05-07 10:47:22 +02:00
|
|
|
builder.appendff("{:02}", tm.tm_hour % 12);
|
2020-03-08 18:35:26 +08:00
|
|
|
break;
|
|
|
|
case 'j':
|
2021-05-07 10:47:22 +02:00
|
|
|
builder.appendff("{:03}", tm.tm_yday + 1);
|
2020-03-08 18:35:26 +08:00
|
|
|
break;
|
|
|
|
case 'm':
|
2021-05-07 10:47:22 +02:00
|
|
|
builder.appendff("{:02}", tm.tm_mon + 1);
|
2020-03-08 18:35:26 +08:00
|
|
|
break;
|
|
|
|
case 'M':
|
2021-05-07 10:47:22 +02:00
|
|
|
builder.appendff("{:02}", tm.tm_min);
|
2020-03-08 18:35:26 +08:00
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
builder.append('\n');
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
builder.append(tm.tm_hour < 12 ? "a.m." : "p.m.");
|
|
|
|
break;
|
|
|
|
case 'r':
|
2021-05-07 10:47:22 +02:00
|
|
|
builder.appendff("{:02}:{:02}:{:02} {}", tm.tm_hour % 12, tm.tm_min, tm.tm_sec, tm.tm_hour < 12 ? "a.m." : "p.m.");
|
2020-03-08 18:35:26 +08:00
|
|
|
break;
|
|
|
|
case 'R':
|
2021-05-07 10:47:22 +02:00
|
|
|
builder.appendff("{:02}:{:02}", tm.tm_hour, tm.tm_min);
|
2020-03-08 18:35:26 +08:00
|
|
|
break;
|
|
|
|
case 'S':
|
2021-05-07 10:47:22 +02:00
|
|
|
builder.appendff("{:02}", tm.tm_sec);
|
2020-03-08 18:35:26 +08:00
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
builder.append('\t');
|
|
|
|
break;
|
|
|
|
case 'T':
|
2021-05-07 10:47:22 +02:00
|
|
|
builder.appendff("{:02}:{:02}:{:02}", tm.tm_hour, tm.tm_min, tm.tm_sec);
|
2020-03-08 18:35:26 +08:00
|
|
|
break;
|
|
|
|
case 'u':
|
2021-05-07 10:47:22 +02:00
|
|
|
builder.appendff("{}", tm.tm_wday ? tm.tm_wday : 7);
|
2020-03-08 18:35:26 +08:00
|
|
|
break;
|
|
|
|
case 'U': {
|
|
|
|
const int wday_of_year_beginning = (tm.tm_wday + 6 * tm.tm_yday) % 7;
|
|
|
|
const int week_number = (tm.tm_yday + wday_of_year_beginning) / 7;
|
2021-05-07 10:47:22 +02:00
|
|
|
builder.appendff("{:02}", week_number);
|
2020-03-08 18:35:26 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'V': {
|
|
|
|
const int wday_of_year_beginning = (tm.tm_wday + 6 + 6 * tm.tm_yday) % 7;
|
|
|
|
int week_number = (tm.tm_yday + wday_of_year_beginning) / 7 + 1;
|
|
|
|
if (wday_of_year_beginning > 3) {
|
|
|
|
if (tm.tm_yday >= 7 - wday_of_year_beginning)
|
|
|
|
--week_number;
|
|
|
|
else {
|
2020-08-25 20:17:19 -04:00
|
|
|
const int days_of_last_year = days_in_year(tm.tm_year + 1900 - 1);
|
2020-03-08 18:35:26 +08:00
|
|
|
const int wday_of_last_year_beginning = (wday_of_year_beginning + 6 * days_of_last_year) % 7;
|
|
|
|
week_number = (days_of_last_year + wday_of_last_year_beginning) / 7 + 1;
|
|
|
|
if (wday_of_last_year_beginning > 3)
|
|
|
|
--week_number;
|
|
|
|
}
|
|
|
|
}
|
2021-05-07 10:47:22 +02:00
|
|
|
builder.appendff("{:02}", week_number);
|
2020-03-08 18:35:26 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'w':
|
2021-05-07 10:47:22 +02:00
|
|
|
builder.appendff("{}", tm.tm_wday);
|
2020-03-08 18:35:26 +08:00
|
|
|
break;
|
|
|
|
case 'W': {
|
|
|
|
const int wday_of_year_beginning = (tm.tm_wday + 6 + 6 * tm.tm_yday) % 7;
|
|
|
|
const int week_number = (tm.tm_yday + wday_of_year_beginning) / 7;
|
2021-05-07 10:47:22 +02:00
|
|
|
builder.appendff("{:02}", week_number);
|
2020-03-08 18:35:26 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'y':
|
2021-05-07 10:47:22 +02:00
|
|
|
builder.appendff("{:02}", (tm.tm_year + 1900) % 100);
|
2020-03-08 18:35:26 +08:00
|
|
|
break;
|
|
|
|
case 'Y':
|
2021-05-07 10:47:22 +02:00
|
|
|
builder.appendff("{}", tm.tm_year + 1900);
|
2020-03-08 18:35:26 +08:00
|
|
|
break;
|
|
|
|
case '%':
|
|
|
|
builder.append('%');
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return String();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return builder.build();
|
2020-02-11 19:42:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|