2020-02-11 18:42:02 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-02-11 18:42:02 +00:00
|
|
|
*/
|
|
|
|
|
2021-06-01 12:52:38 +00:00
|
|
|
#include <AK/CharacterTypes.h>
|
2022-03-17 01:08:53 +00:00
|
|
|
#include <AK/DateConstants.h>
|
2020-03-08 10:35:26 +00:00
|
|
|
#include <AK/StringBuilder.h>
|
2020-08-25 20:38:24 +00:00
|
|
|
#include <AK/Time.h>
|
2020-02-11 18:42:02 +00:00
|
|
|
#include <LibCore/DateTime.h>
|
2021-06-01 12:52:38 +00:00
|
|
|
#include <errno.h>
|
2020-02-11 18:42:02 +00:00
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
namespace Core {
|
|
|
|
|
|
|
|
DateTime DateTime::now()
|
|
|
|
{
|
2020-02-11 18:48:46 +00:00
|
|
|
return from_timestamp(time(nullptr));
|
|
|
|
}
|
|
|
|
|
2021-08-19 16:16:03 +00:00
|
|
|
DateTime DateTime::create(int year, int month, int day, int hour, int minute, int second)
|
2020-03-10 21:41:01 +00:00
|
|
|
{
|
|
|
|
DateTime dt;
|
2020-08-24 13:23:46 +00:00
|
|
|
dt.set_time(year, month, day, hour, minute, second);
|
2020-03-10 21:41:01 +00:00
|
|
|
return dt;
|
|
|
|
}
|
|
|
|
|
2020-02-11 18:48:46 +00:00
|
|
|
DateTime DateTime::from_timestamp(time_t timestamp)
|
|
|
|
{
|
2020-02-11 18:42:02 +00: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 21:41:01 +00:00
|
|
|
unsigned DateTime::weekday() const
|
|
|
|
{
|
2020-08-26 00:32:32 +00:00
|
|
|
return ::day_of_week(m_year, m_month, m_day);
|
2020-03-10 21:41:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned DateTime::days_in_month() const
|
|
|
|
{
|
2020-08-26 00:11:12 +00:00
|
|
|
return ::days_in_month(m_year, m_month);
|
2020-03-10 21:41:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned DateTime::day_of_year() const
|
|
|
|
{
|
2020-08-25 23:19:16 +00:00
|
|
|
return ::day_of_year(m_year, m_month, m_day);
|
2020-03-10 21:41:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool DateTime::is_leap_year() const
|
|
|
|
{
|
2020-08-25 20:38:24 +00:00
|
|
|
return ::is_leap_year(m_year);
|
2020-03-10 21:41:01 +00:00
|
|
|
}
|
|
|
|
|
2021-08-19 16:16:03 +00:00
|
|
|
void DateTime::set_time(int year, int month, int day, int hour, int minute, int second)
|
2020-03-10 21:41:01 +00:00
|
|
|
{
|
2020-03-23 12:13:36 +00:00
|
|
|
struct tm tm = {};
|
2021-08-19 16:16:03 +00:00
|
|
|
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;
|
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 13:39:16 +00:00
|
|
|
tm.tm_isdst = -1;
|
2020-08-24 13:24:44 +00:00
|
|
|
// mktime() doesn't read tm.tm_wday and tm.tm_yday, no need to fill them in.
|
|
|
|
|
2020-03-10 21:41:01 +00:00
|
|
|
m_timestamp = mktime(&tm);
|
2020-08-24 13:24:44 +00: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 21:41:01 +00:00
|
|
|
}
|
|
|
|
|
2022-01-28 19:26:43 +00:00
|
|
|
String DateTime::to_string(StringView format) const
|
2020-02-11 18:42:02 +00:00
|
|
|
{
|
2020-03-08 10:35:26 +00:00
|
|
|
struct tm tm;
|
|
|
|
localtime_r(&m_timestamp, &tm);
|
|
|
|
StringBuilder builder;
|
2022-04-01 17:58:27 +00:00
|
|
|
int const format_len = format.length();
|
2020-03-08 10:35:26 +00:00
|
|
|
|
2022-01-25 21:04:54 +00:00
|
|
|
auto format_time_zone_offset = [&](bool with_separator) {
|
2022-03-13 12:52:07 +00:00
|
|
|
#if defined(__serenity__)
|
|
|
|
auto offset_seconds = daylight ? -altzone : -timezone;
|
|
|
|
#elif !defined(__FreeBSD__)
|
2022-01-25 20:46:49 +00:00
|
|
|
auto offset_seconds = -timezone;
|
2022-02-14 17:52:30 +00:00
|
|
|
#else
|
|
|
|
auto offset_seconds = 0;
|
|
|
|
#endif
|
2022-01-25 20:46:49 +00:00
|
|
|
StringView offset_sign;
|
|
|
|
|
|
|
|
if (offset_seconds >= 0) {
|
|
|
|
offset_sign = "+"sv;
|
|
|
|
} else {
|
|
|
|
offset_sign = "-"sv;
|
|
|
|
offset_seconds *= -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto offset_hours = offset_seconds / 3600;
|
|
|
|
auto offset_minutes = (offset_seconds % 3600) / 60;
|
2022-01-25 21:04:54 +00:00
|
|
|
auto separator = with_separator ? ":"sv : ""sv;
|
2022-01-25 20:46:49 +00:00
|
|
|
|
2022-01-25 21:04:54 +00:00
|
|
|
builder.appendff("{}{:02}{}{:02}", offset_sign, offset_hours, separator, offset_minutes);
|
2022-01-25 20:46:49 +00:00
|
|
|
};
|
|
|
|
|
2020-03-08 10:35:26 +00:00
|
|
|
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':
|
2022-03-17 01:08:53 +00:00
|
|
|
builder.append(short_day_names[tm.tm_wday]);
|
2020-03-08 10:35:26 +00:00
|
|
|
break;
|
|
|
|
case 'A':
|
2022-03-17 01:08:53 +00:00
|
|
|
builder.append(long_day_names[tm.tm_wday]);
|
2020-03-08 10:35:26 +00:00
|
|
|
break;
|
|
|
|
case 'b':
|
2022-03-17 01:08:53 +00:00
|
|
|
builder.append(short_month_names[tm.tm_mon]);
|
2020-03-08 10:35:26 +00:00
|
|
|
break;
|
|
|
|
case 'B':
|
2022-03-17 01:08:53 +00:00
|
|
|
builder.append(long_month_names[tm.tm_mon]);
|
2020-03-08 10:35:26 +00:00
|
|
|
break;
|
|
|
|
case 'C':
|
2021-05-07 08:47:22 +00:00
|
|
|
builder.appendff("{:02}", (tm.tm_year + 1900) / 100);
|
2020-03-08 10:35:26 +00:00
|
|
|
break;
|
|
|
|
case 'd':
|
2021-05-07 08:47:22 +00:00
|
|
|
builder.appendff("{:02}", tm.tm_mday);
|
2020-03-08 10:35:26 +00:00
|
|
|
break;
|
|
|
|
case 'D':
|
2021-05-07 08:47:22 +00:00
|
|
|
builder.appendff("{:02}/{:02}/{:02}", tm.tm_mon + 1, tm.tm_mday, (tm.tm_year + 1900) % 100);
|
2020-03-08 10:35:26 +00:00
|
|
|
break;
|
|
|
|
case 'e':
|
2021-05-07 08:47:22 +00:00
|
|
|
builder.appendff("{:2}", tm.tm_mday);
|
2020-03-08 10:35:26 +00:00
|
|
|
break;
|
|
|
|
case 'h':
|
2022-03-17 01:08:53 +00:00
|
|
|
builder.append(short_month_names[tm.tm_mon]);
|
2020-03-08 10:35:26 +00:00
|
|
|
break;
|
|
|
|
case 'H':
|
2021-05-07 08:47:22 +00:00
|
|
|
builder.appendff("{:02}", tm.tm_hour);
|
2020-03-08 10:35:26 +00:00
|
|
|
break;
|
|
|
|
case 'I':
|
2021-05-07 08:47:22 +00:00
|
|
|
builder.appendff("{:02}", tm.tm_hour % 12);
|
2020-03-08 10:35:26 +00:00
|
|
|
break;
|
|
|
|
case 'j':
|
2021-05-07 08:47:22 +00:00
|
|
|
builder.appendff("{:03}", tm.tm_yday + 1);
|
2020-03-08 10:35:26 +00:00
|
|
|
break;
|
|
|
|
case 'm':
|
2021-05-07 08:47:22 +00:00
|
|
|
builder.appendff("{:02}", tm.tm_mon + 1);
|
2020-03-08 10:35:26 +00:00
|
|
|
break;
|
|
|
|
case 'M':
|
2021-05-07 08:47:22 +00:00
|
|
|
builder.appendff("{:02}", tm.tm_min);
|
2020-03-08 10:35:26 +00: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 08:47:22 +00: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 10:35:26 +00:00
|
|
|
break;
|
|
|
|
case 'R':
|
2021-05-07 08:47:22 +00:00
|
|
|
builder.appendff("{:02}:{:02}", tm.tm_hour, tm.tm_min);
|
2020-03-08 10:35:26 +00:00
|
|
|
break;
|
|
|
|
case 'S':
|
2021-05-07 08:47:22 +00:00
|
|
|
builder.appendff("{:02}", tm.tm_sec);
|
2020-03-08 10:35:26 +00:00
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
builder.append('\t');
|
|
|
|
break;
|
|
|
|
case 'T':
|
2021-05-07 08:47:22 +00:00
|
|
|
builder.appendff("{:02}:{:02}:{:02}", tm.tm_hour, tm.tm_min, tm.tm_sec);
|
2020-03-08 10:35:26 +00:00
|
|
|
break;
|
|
|
|
case 'u':
|
2021-05-07 08:47:22 +00:00
|
|
|
builder.appendff("{}", tm.tm_wday ? tm.tm_wday : 7);
|
2020-03-08 10:35:26 +00:00
|
|
|
break;
|
|
|
|
case 'U': {
|
2022-04-01 17:58:27 +00:00
|
|
|
int const wday_of_year_beginning = (tm.tm_wday + 6 * tm.tm_yday) % 7;
|
|
|
|
int const week_number = (tm.tm_yday + wday_of_year_beginning) / 7;
|
2021-05-07 08:47:22 +00:00
|
|
|
builder.appendff("{:02}", week_number);
|
2020-03-08 10:35:26 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'V': {
|
2022-04-01 17:58:27 +00:00
|
|
|
int const wday_of_year_beginning = (tm.tm_wday + 6 + 6 * tm.tm_yday) % 7;
|
2020-03-08 10:35:26 +00:00
|
|
|
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 {
|
2022-04-01 17:58:27 +00:00
|
|
|
int const days_of_last_year = days_in_year(tm.tm_year + 1900 - 1);
|
|
|
|
int const wday_of_last_year_beginning = (wday_of_year_beginning + 6 * days_of_last_year) % 7;
|
2020-03-08 10:35:26 +00:00
|
|
|
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 08:47:22 +00:00
|
|
|
builder.appendff("{:02}", week_number);
|
2020-03-08 10:35:26 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'w':
|
2021-05-07 08:47:22 +00:00
|
|
|
builder.appendff("{}", tm.tm_wday);
|
2020-03-08 10:35:26 +00:00
|
|
|
break;
|
|
|
|
case 'W': {
|
2022-04-01 17:58:27 +00:00
|
|
|
int const wday_of_year_beginning = (tm.tm_wday + 6 + 6 * tm.tm_yday) % 7;
|
|
|
|
int const week_number = (tm.tm_yday + wday_of_year_beginning) / 7;
|
2021-05-07 08:47:22 +00:00
|
|
|
builder.appendff("{:02}", week_number);
|
2020-03-08 10:35:26 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'y':
|
2021-05-07 08:47:22 +00:00
|
|
|
builder.appendff("{:02}", (tm.tm_year + 1900) % 100);
|
2020-03-08 10:35:26 +00:00
|
|
|
break;
|
|
|
|
case 'Y':
|
2021-05-07 08:47:22 +00:00
|
|
|
builder.appendff("{}", tm.tm_year + 1900);
|
2020-03-08 10:35:26 +00:00
|
|
|
break;
|
2022-01-25 20:46:49 +00:00
|
|
|
case 'z':
|
2022-01-25 21:04:54 +00:00
|
|
|
format_time_zone_offset(false);
|
|
|
|
break;
|
|
|
|
case ':':
|
|
|
|
if (++i == format_len)
|
|
|
|
return String::empty();
|
|
|
|
if (format[i] != 'z')
|
|
|
|
return String::empty();
|
|
|
|
|
|
|
|
format_time_zone_offset(true);
|
2022-01-25 20:46:49 +00:00
|
|
|
break;
|
2022-01-25 21:07:22 +00:00
|
|
|
case 'Z':
|
2022-03-13 12:52:07 +00:00
|
|
|
builder.append(tzname[daylight]);
|
2022-01-25 21:07:22 +00:00
|
|
|
break;
|
2020-03-08 10:35:26 +00:00
|
|
|
case '%':
|
|
|
|
builder.append('%');
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return String();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return builder.build();
|
2020-02-11 18:42:02 +00:00
|
|
|
}
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
Optional<DateTime> DateTime::parse(StringView format, String const& string)
|
2021-06-01 12:52:38 +00:00
|
|
|
{
|
|
|
|
unsigned format_pos = 0;
|
|
|
|
unsigned string_pos = 0;
|
|
|
|
struct tm tm = {};
|
|
|
|
|
|
|
|
auto parsing_failed = false;
|
|
|
|
|
|
|
|
auto parse_number = [&] {
|
|
|
|
if (string_pos >= string.length()) {
|
|
|
|
parsing_failed = true;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
char* end_ptr = nullptr;
|
|
|
|
errno = 0;
|
|
|
|
int number = strtol(string.characters() + string_pos, &end_ptr, 10);
|
|
|
|
|
|
|
|
auto chars_parsed = end_ptr - (string.characters() + string_pos);
|
|
|
|
if (chars_parsed == 0 || errno != 0)
|
|
|
|
parsing_failed = true;
|
|
|
|
else
|
|
|
|
string_pos += chars_parsed;
|
|
|
|
return number;
|
|
|
|
};
|
|
|
|
|
|
|
|
auto consume = [&](char x) {
|
|
|
|
if (string_pos >= string.length()) {
|
|
|
|
parsing_failed = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (string[string_pos] != x)
|
|
|
|
parsing_failed = true;
|
|
|
|
else
|
|
|
|
string_pos++;
|
|
|
|
};
|
|
|
|
|
|
|
|
while (format_pos < format.length() && string_pos < string.length()) {
|
|
|
|
if (format[format_pos] != '%') {
|
|
|
|
if (format[format_pos] != string[string_pos]) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
format_pos++;
|
|
|
|
string_pos++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
format_pos++;
|
|
|
|
if (format_pos == format.length()) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
switch (format[format_pos]) {
|
|
|
|
case 'a': {
|
|
|
|
auto wday = 0;
|
2022-03-17 01:08:53 +00:00
|
|
|
for (auto name : short_day_names) {
|
2021-06-01 12:52:38 +00:00
|
|
|
if (string.substring_view(string_pos).starts_with(name, AK::CaseSensitivity::CaseInsensitive)) {
|
|
|
|
string_pos += name.length();
|
|
|
|
tm.tm_wday = wday;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
++wday;
|
|
|
|
}
|
|
|
|
if (wday == 7)
|
|
|
|
return {};
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'A': {
|
|
|
|
auto wday = 0;
|
2022-03-17 01:08:53 +00:00
|
|
|
for (auto name : long_day_names) {
|
2021-06-01 12:52:38 +00:00
|
|
|
if (string.substring_view(string_pos).starts_with(name, AK::CaseSensitivity::CaseInsensitive)) {
|
|
|
|
string_pos += name.length();
|
|
|
|
tm.tm_wday = wday;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
++wday;
|
|
|
|
}
|
|
|
|
if (wday == 7)
|
|
|
|
return {};
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'h':
|
|
|
|
case 'b': {
|
|
|
|
auto mon = 0;
|
2022-03-17 01:08:53 +00:00
|
|
|
for (auto name : short_month_names) {
|
2021-06-01 12:52:38 +00:00
|
|
|
if (string.substring_view(string_pos).starts_with(name, AK::CaseSensitivity::CaseInsensitive)) {
|
|
|
|
string_pos += name.length();
|
|
|
|
tm.tm_mon = mon;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
++mon;
|
|
|
|
}
|
|
|
|
if (mon == 12)
|
|
|
|
return {};
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'B': {
|
|
|
|
auto mon = 0;
|
2022-03-17 01:08:53 +00:00
|
|
|
for (auto name : long_month_names) {
|
2021-06-01 12:52:38 +00:00
|
|
|
if (string.substring_view(string_pos).starts_with(name, AK::CaseSensitivity::CaseInsensitive)) {
|
|
|
|
string_pos += name.length();
|
|
|
|
tm.tm_mon = mon;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
++mon;
|
|
|
|
}
|
|
|
|
if (mon == 12)
|
|
|
|
return {};
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'C': {
|
|
|
|
int num = parse_number();
|
|
|
|
tm.tm_year = (num - 19) * 100;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'd': {
|
|
|
|
tm.tm_mday = parse_number();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'D': {
|
|
|
|
int mon = parse_number();
|
|
|
|
consume('/');
|
|
|
|
int day = parse_number();
|
|
|
|
consume('/');
|
|
|
|
int year = parse_number();
|
|
|
|
tm.tm_mon = mon + 1;
|
|
|
|
tm.tm_mday = day;
|
|
|
|
tm.tm_year = (year + 1900) % 100;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'e': {
|
|
|
|
tm.tm_mday = parse_number();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'H': {
|
|
|
|
tm.tm_hour = parse_number();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'I': {
|
|
|
|
int num = parse_number();
|
|
|
|
tm.tm_hour = num % 12;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'j': {
|
|
|
|
// a little trickery here... we can get mktime() to figure out mon and mday using out of range values.
|
|
|
|
// yday is not used so setting it is pointless.
|
|
|
|
tm.tm_mday = parse_number();
|
|
|
|
tm.tm_mon = 0;
|
|
|
|
mktime(&tm);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'm': {
|
|
|
|
int num = parse_number();
|
|
|
|
tm.tm_mon = num - 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'M': {
|
|
|
|
tm.tm_min = parse_number();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'n':
|
|
|
|
case 't':
|
|
|
|
while (is_ascii_blank(string[string_pos])) {
|
|
|
|
string_pos++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'p': {
|
|
|
|
auto ampm = string.substring_view(string_pos, 4);
|
|
|
|
if (ampm == "p.m." && tm.tm_hour < 12) {
|
|
|
|
tm.tm_hour += 12;
|
|
|
|
}
|
|
|
|
string_pos += 4;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'r': {
|
|
|
|
auto ampm = string.substring_view(string_pos, 4);
|
|
|
|
if (ampm == "p.m." && tm.tm_hour < 12) {
|
|
|
|
tm.tm_hour += 12;
|
|
|
|
}
|
|
|
|
string_pos += 4;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'R': {
|
|
|
|
tm.tm_hour = parse_number();
|
|
|
|
consume(':');
|
|
|
|
tm.tm_min = parse_number();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'S':
|
|
|
|
tm.tm_sec = parse_number();
|
|
|
|
break;
|
|
|
|
case 'T':
|
|
|
|
tm.tm_hour = parse_number();
|
|
|
|
consume(':');
|
|
|
|
tm.tm_min = parse_number();
|
|
|
|
consume(':');
|
|
|
|
tm.tm_sec = parse_number();
|
|
|
|
break;
|
|
|
|
case 'w':
|
|
|
|
tm.tm_wday = parse_number();
|
|
|
|
break;
|
|
|
|
case 'y': {
|
|
|
|
int year = parse_number();
|
|
|
|
tm.tm_year = year <= 99 && year > 69 ? 1900 + year : 2000 + year;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'Y': {
|
|
|
|
int year = parse_number();
|
|
|
|
tm.tm_year = year - 1900;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'z': {
|
|
|
|
if (string[string_pos] == 'Z') {
|
|
|
|
// UTC time
|
|
|
|
string_pos++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
int sign;
|
|
|
|
|
|
|
|
if (string[string_pos] == '+')
|
|
|
|
sign = -1;
|
|
|
|
else if (string[string_pos] == '-')
|
|
|
|
sign = +1;
|
|
|
|
else
|
|
|
|
return {};
|
|
|
|
|
|
|
|
string_pos++;
|
|
|
|
|
|
|
|
auto hours = parse_number();
|
|
|
|
int minutes;
|
|
|
|
if (string_pos < string.length() && string[string_pos] == ':') {
|
|
|
|
string_pos++;
|
|
|
|
minutes = parse_number();
|
|
|
|
} else {
|
|
|
|
minutes = hours % 100;
|
|
|
|
hours = hours / 100;
|
|
|
|
}
|
|
|
|
|
|
|
|
tm.tm_hour += sign * hours;
|
|
|
|
tm.tm_min += sign * minutes;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case '%':
|
|
|
|
if (string[string_pos] != '%') {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
string_pos += 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
parsing_failed = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parsing_failed) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
format_pos++;
|
|
|
|
}
|
|
|
|
if (string_pos != string.length() || format_pos != format.length()) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
return DateTime::from_timestamp(mktime(&tm));
|
|
|
|
}
|
2020-02-11 18:42:02 +00:00
|
|
|
}
|