mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-08-26 12:17:52 +00:00
LibCore: Remove DateTime::to_string() and to_byte_string()
Some checks are pending
CI / macOS, arm64, Sanitizer_CI, Clang (push) Waiting to run
CI / Linux, x86_64, Fuzzers_CI, Clang (push) Waiting to run
CI / Linux, x86_64, Sanitizer_CI, GNU (push) Waiting to run
CI / Linux, x86_64, Sanitizer_CI, Clang (push) Waiting to run
Package the js repl as a binary artifact / macOS, arm64 (push) Waiting to run
Package the js repl as a binary artifact / Linux, x86_64 (push) Waiting to run
Run test262 and test-wasm / run_and_update_results (push) Waiting to run
Lint Code / lint (push) Waiting to run
Label PRs with merge conflicts / auto-labeler (push) Waiting to run
Push notes / build (push) Waiting to run
Some checks are pending
CI / macOS, arm64, Sanitizer_CI, Clang (push) Waiting to run
CI / Linux, x86_64, Fuzzers_CI, Clang (push) Waiting to run
CI / Linux, x86_64, Sanitizer_CI, GNU (push) Waiting to run
CI / Linux, x86_64, Sanitizer_CI, Clang (push) Waiting to run
Package the js repl as a binary artifact / macOS, arm64 (push) Waiting to run
Package the js repl as a binary artifact / Linux, x86_64 (push) Waiting to run
Run test262 and test-wasm / run_and_update_results (push) Waiting to run
Lint Code / lint (push) Waiting to run
Label PRs with merge conflicts / auto-labeler (push) Waiting to run
Push notes / build (push) Waiting to run
This commit is contained in:
parent
6fb2be96bf
commit
7278a17e87
Notes:
github-actions[bot]
2025-06-20 00:44:06 +00:00
Author: https://github.com/tomaszstrejczek
Commit: 7278a17e87
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/5096
Reviewed-by: https://github.com/ADKaster ✅
Reviewed-by: https://github.com/R-Goc
Reviewed-by: https://github.com/gmta
2 changed files with 0 additions and 239 deletions
|
@ -134,209 +134,6 @@ void DateTime::set_date(Core::DateTime const& other)
|
||||||
set_time(other.year(), other.month(), other.day(), hour(), minute(), second());
|
set_time(other.year(), other.month(), other.day(), hour(), minute(), second());
|
||||||
}
|
}
|
||||||
|
|
||||||
ErrorOr<String> DateTime::to_string(StringView format, LocalTime local_time) const
|
|
||||||
{
|
|
||||||
struct tm tm;
|
|
||||||
|
|
||||||
if (local_time == LocalTime::Yes)
|
|
||||||
localtime_r(&m_timestamp, &tm);
|
|
||||||
else
|
|
||||||
gmtime_r(&m_timestamp, &tm);
|
|
||||||
|
|
||||||
StringBuilder builder;
|
|
||||||
size_t const format_len = format.length();
|
|
||||||
|
|
||||||
auto format_time_zone_offset = [&](bool with_separator) -> ErrorOr<void> {
|
|
||||||
struct tm gmt_tm;
|
|
||||||
gmtime_r(&m_timestamp, &gmt_tm);
|
|
||||||
|
|
||||||
gmt_tm.tm_isdst = -1;
|
|
||||||
auto gmt_timestamp = mktime(&gmt_tm);
|
|
||||||
|
|
||||||
auto offset_seconds = static_cast<time_t>(difftime(m_timestamp, gmt_timestamp));
|
|
||||||
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;
|
|
||||||
auto separator = with_separator ? ":"sv : ""sv;
|
|
||||||
|
|
||||||
TRY(builder.try_appendff("{}{:02}{}{:02}", offset_sign, offset_hours, separator, offset_minutes));
|
|
||||||
return {};
|
|
||||||
};
|
|
||||||
|
|
||||||
for (size_t i = 0; i < format_len; ++i) {
|
|
||||||
if (format[i] != '%') {
|
|
||||||
TRY(builder.try_append(format[i]));
|
|
||||||
} else {
|
|
||||||
if (++i == format_len)
|
|
||||||
return String {};
|
|
||||||
|
|
||||||
switch (format[i]) {
|
|
||||||
case 'a':
|
|
||||||
TRY(builder.try_append(short_day_names[tm.tm_wday]));
|
|
||||||
break;
|
|
||||||
case 'A':
|
|
||||||
TRY(builder.try_append(long_day_names[tm.tm_wday]));
|
|
||||||
break;
|
|
||||||
case 'b':
|
|
||||||
TRY(builder.try_append(short_month_names[tm.tm_mon]));
|
|
||||||
break;
|
|
||||||
case 'B':
|
|
||||||
TRY(builder.try_append(long_month_names[tm.tm_mon]));
|
|
||||||
break;
|
|
||||||
case 'C':
|
|
||||||
TRY(builder.try_appendff("{:02}", (tm.tm_year + 1900) / 100));
|
|
||||||
break;
|
|
||||||
case 'd':
|
|
||||||
TRY(builder.try_appendff("{:02}", tm.tm_mday));
|
|
||||||
break;
|
|
||||||
case 'D':
|
|
||||||
TRY(builder.try_appendff("{:02}/{:02}/{:02}", tm.tm_mon + 1, tm.tm_mday, (tm.tm_year + 1900) % 100));
|
|
||||||
break;
|
|
||||||
case 'e':
|
|
||||||
TRY(builder.try_appendff("{:2}", tm.tm_mday));
|
|
||||||
break;
|
|
||||||
case 'h':
|
|
||||||
TRY(builder.try_append(short_month_names[tm.tm_mon]));
|
|
||||||
break;
|
|
||||||
case 'H':
|
|
||||||
TRY(builder.try_appendff("{:02}", tm.tm_hour));
|
|
||||||
break;
|
|
||||||
case 'I': {
|
|
||||||
int display_hour = tm.tm_hour % 12;
|
|
||||||
if (display_hour == 0)
|
|
||||||
display_hour = 12;
|
|
||||||
TRY(builder.try_appendff("{:02}", display_hour));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case 'j':
|
|
||||||
TRY(builder.try_appendff("{:03}", tm.tm_yday + 1));
|
|
||||||
break;
|
|
||||||
case 'l': {
|
|
||||||
int display_hour = tm.tm_hour % 12;
|
|
||||||
if (display_hour == 0)
|
|
||||||
display_hour = 12;
|
|
||||||
TRY(builder.try_appendff("{:2}", display_hour));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case 'm':
|
|
||||||
TRY(builder.try_appendff("{:02}", tm.tm_mon + 1));
|
|
||||||
break;
|
|
||||||
case 'M':
|
|
||||||
TRY(builder.try_appendff("{:02}", tm.tm_min));
|
|
||||||
break;
|
|
||||||
case 'n':
|
|
||||||
TRY(builder.try_append('\n'));
|
|
||||||
break;
|
|
||||||
case 'p':
|
|
||||||
TRY(builder.try_append(tm.tm_hour < 12 ? "AM"sv : "PM"sv));
|
|
||||||
break;
|
|
||||||
case 'r': {
|
|
||||||
int display_hour = tm.tm_hour % 12;
|
|
||||||
if (display_hour == 0)
|
|
||||||
display_hour = 12;
|
|
||||||
TRY(builder.try_appendff("{:02}:{:02}:{:02} {}", display_hour, tm.tm_min, tm.tm_sec, tm.tm_hour < 12 ? "AM" : "PM"));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case 'R':
|
|
||||||
TRY(builder.try_appendff("{:02}:{:02}", tm.tm_hour, tm.tm_min));
|
|
||||||
break;
|
|
||||||
case 'S':
|
|
||||||
TRY(builder.try_appendff("{:02}", tm.tm_sec));
|
|
||||||
break;
|
|
||||||
case 't':
|
|
||||||
TRY(builder.try_append('\t'));
|
|
||||||
break;
|
|
||||||
case 'T':
|
|
||||||
TRY(builder.try_appendff("{:02}:{:02}:{:02}", tm.tm_hour, tm.tm_min, tm.tm_sec));
|
|
||||||
break;
|
|
||||||
case 'u':
|
|
||||||
TRY(builder.try_appendff("{}", tm.tm_wday ? tm.tm_wday : 7));
|
|
||||||
break;
|
|
||||||
case 'U': {
|
|
||||||
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;
|
|
||||||
TRY(builder.try_appendff("{:02}", week_number));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case 'V': {
|
|
||||||
int const 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 {
|
|
||||||
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;
|
|
||||||
week_number = (days_of_last_year + wday_of_last_year_beginning) / 7 + 1;
|
|
||||||
if (wday_of_last_year_beginning > 3)
|
|
||||||
--week_number;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
TRY(builder.try_appendff("{:02}", week_number));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case 'w':
|
|
||||||
TRY(builder.try_appendff("{}", tm.tm_wday));
|
|
||||||
break;
|
|
||||||
case 'W': {
|
|
||||||
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;
|
|
||||||
TRY(builder.try_appendff("{:02}", week_number));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case 'y':
|
|
||||||
TRY(builder.try_appendff("{:02}", (tm.tm_year + 1900) % 100));
|
|
||||||
break;
|
|
||||||
case 'Y':
|
|
||||||
TRY(builder.try_appendff("{}", tm.tm_year + 1900));
|
|
||||||
break;
|
|
||||||
case 'z':
|
|
||||||
TRY(format_time_zone_offset(false));
|
|
||||||
break;
|
|
||||||
case ':':
|
|
||||||
if (++i == format_len) {
|
|
||||||
TRY(builder.try_append("%:"sv));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (format[i] != 'z') {
|
|
||||||
TRY(builder.try_append("%:"sv));
|
|
||||||
TRY(builder.try_append(format[i]));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
TRY(format_time_zone_offset(true));
|
|
||||||
break;
|
|
||||||
case 'Z': {
|
|
||||||
auto const* timezone_name = tzname[tm.tm_isdst == 0 ? 0 : 1];
|
|
||||||
TRY(builder.try_append({ timezone_name, strlen(timezone_name) }));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case '%':
|
|
||||||
TRY(builder.try_append('%'));
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
TRY(builder.try_append('%'));
|
|
||||||
TRY(builder.try_append(format[i]));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return builder.to_string();
|
|
||||||
}
|
|
||||||
|
|
||||||
ByteString DateTime::to_byte_string(StringView format, LocalTime local_time) const
|
|
||||||
{
|
|
||||||
return MUST(to_string(format, local_time)).to_byte_string();
|
|
||||||
}
|
|
||||||
|
|
||||||
Optional<DateTime> DateTime::parse(StringView format, StringView string)
|
Optional<DateTime> DateTime::parse(StringView format, StringView string)
|
||||||
{
|
{
|
||||||
unsigned format_pos = 0;
|
unsigned format_pos = 0;
|
||||||
|
|
|
@ -33,42 +33,6 @@ public:
|
||||||
void set_time_only(int hour, int minute, Optional<int> second = {});
|
void set_time_only(int hour, int minute, Optional<int> second = {});
|
||||||
void set_date(Core::DateTime const& other);
|
void set_date(Core::DateTime const& other);
|
||||||
|
|
||||||
enum class LocalTime {
|
|
||||||
Yes,
|
|
||||||
No,
|
|
||||||
};
|
|
||||||
|
|
||||||
// %a: require short day name
|
|
||||||
// %A: require long day name
|
|
||||||
// %h/b: require short month name
|
|
||||||
// %B: require long month name
|
|
||||||
// %C: require short year number (hundreds) (ex: 19 -> 1900)
|
|
||||||
// %d: require day number
|
|
||||||
// %D: require month number/day number/short year number (ex: 12/31/24)
|
|
||||||
// %e: require day number
|
|
||||||
// %H: require hour (24h format)
|
|
||||||
// %I: require hour (12h format)
|
|
||||||
// %j: require defining date with day number ? (not sure)
|
|
||||||
// %m: require set to month entred - 1
|
|
||||||
// %M: require minutes
|
|
||||||
// %n: require newline
|
|
||||||
// %t: require tab
|
|
||||||
// %r/p: require AM | PM
|
|
||||||
// %R: require hours:minutes (ex: 13:57)
|
|
||||||
// %S: require seconds
|
|
||||||
// %T: require hours:minutes:seconds (ex: 13:57:34)
|
|
||||||
// %w: require week day number
|
|
||||||
// %y: require 2 digits year (69 < year < 99: in the 1900 else in 2000)
|
|
||||||
// %Y: require full year
|
|
||||||
// %z: require timezone hours:minutes or single number to represent hour and minutes
|
|
||||||
// %x: require single number to represent hour and minutes
|
|
||||||
// %X: require sub second precision
|
|
||||||
// %Z: require timezone name
|
|
||||||
// %+: ignore until next '%'
|
|
||||||
// %%: require character '%'
|
|
||||||
ErrorOr<String> to_string(StringView format = "%Y-%m-%d %H:%M:%S"sv, LocalTime = LocalTime::Yes) const;
|
|
||||||
ByteString to_byte_string(StringView format = "%Y-%m-%d %H:%M:%S"sv, LocalTime = LocalTime::Yes) const;
|
|
||||||
|
|
||||||
static DateTime create(int year, int month = 1, int day = 1, int hour = 0, int minute = 0, int second = 0);
|
static DateTime create(int year, int month = 1, int day = 1, int hour = 0, int minute = 0, int second = 0);
|
||||||
static DateTime now();
|
static DateTime now();
|
||||||
static DateTime from_timestamp(time_t);
|
static DateTime from_timestamp(time_t);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue