mirror of
https://github.com/SerenityOS/serenity.git
synced 2025-01-23 09:51:57 -05:00
LibCore: Add a way to parse a DateTime from a string
DateTime can now be parsed from a string. Implements the same formatters as strptime: https://linux.die.net/man/3/strptime (Well, some of them at least).
This commit is contained in:
parent
cbd7437d40
commit
904322e754
2 changed files with 282 additions and 0 deletions
|
@ -4,9 +4,11 @@
|
|||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/CharacterTypes.h>
|
||||
#include <AK/StringBuilder.h>
|
||||
#include <AK/Time.h>
|
||||
#include <LibCore/DateTime.h>
|
||||
#include <errno.h>
|
||||
#include <sys/time.h>
|
||||
#include <time.h>
|
||||
|
||||
|
@ -228,4 +230,283 @@ String DateTime::to_string(const String& format) const
|
|||
return builder.build();
|
||||
}
|
||||
|
||||
Optional<DateTime> DateTime::parse(const String& format, const String& string)
|
||||
{
|
||||
unsigned format_pos = 0;
|
||||
unsigned string_pos = 0;
|
||||
struct tm tm = {};
|
||||
|
||||
const StringView wday_short_names[7] = {
|
||||
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
|
||||
};
|
||||
const StringView wday_long_names[7] = {
|
||||
"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
|
||||
};
|
||||
const StringView mon_short_names[12] = {
|
||||
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
|
||||
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
|
||||
};
|
||||
const StringView mon_long_names[12] = {
|
||||
"January", "February", "March", "April", "May", "June",
|
||||
"July", "August", "September", "October", "November", "December"
|
||||
};
|
||||
|
||||
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;
|
||||
for (auto name : wday_short_names) {
|
||||
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;
|
||||
for (auto name : wday_long_names) {
|
||||
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;
|
||||
for (auto name : mon_short_names) {
|
||||
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;
|
||||
for (auto name : mon_long_names) {
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -35,6 +35,7 @@ public:
|
|||
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);
|
||||
static Optional<DateTime> parse(const String& format, const String& string);
|
||||
|
||||
bool operator<(const DateTime& other) const { return m_timestamp < other.m_timestamp; }
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue