mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-06-10 18:10:56 +09:00
LibCore: Allow formatting a DateTime in GMT
HTTP dates are always expressed in GMT, so add an API to support that behaviour.
This commit is contained in:
parent
86d29bd775
commit
b169ffd1cf
Notes:
sideshowbarker
2024-07-17 00:47:29 +09:00
Author: https://github.com/shannonbooth
Commit: b169ffd1cf
Pull-request: https://github.com/SerenityOS/serenity/pull/23799
2 changed files with 17 additions and 6 deletions
|
@ -122,10 +122,15 @@ void DateTime::set_date(Core::DateTime const& other)
|
|||
set_time(other.year(), other.month(), other.day(), hour(), minute(), second());
|
||||
}
|
||||
|
||||
ErrorOr<String> DateTime::to_string(StringView format) const
|
||||
ErrorOr<String> DateTime::to_string(StringView format, LocalTime local_time) const
|
||||
{
|
||||
struct tm tm;
|
||||
localtime_r(&m_timestamp, &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();
|
||||
|
||||
|
@ -315,9 +320,9 @@ ErrorOr<String> DateTime::to_string(StringView format) const
|
|||
return builder.to_string();
|
||||
}
|
||||
|
||||
ByteString DateTime::to_byte_string(StringView format) const
|
||||
ByteString DateTime::to_byte_string(StringView format, LocalTime local_time) const
|
||||
{
|
||||
return MUST(to_string(format)).to_byte_string();
|
||||
return MUST(to_string(format, local_time)).to_byte_string();
|
||||
}
|
||||
|
||||
Optional<DateTime> DateTime::parse(StringView format, StringView string)
|
||||
|
|
|
@ -33,8 +33,14 @@ public:
|
|||
void set_time(int year, int month = 1, int day = 1, int hour = 0, int minute = 0, int second = 0);
|
||||
void set_time_only(int hour, int minute, Optional<int> second = {});
|
||||
void set_date(Core::DateTime const& other);
|
||||
ErrorOr<String> to_string(StringView format = "%Y-%m-%d %H:%M:%S"sv) const;
|
||||
ByteString to_byte_string(StringView format = "%Y-%m-%d %H:%M:%S"sv) const;
|
||||
|
||||
enum class LocalTime {
|
||||
Yes,
|
||||
No,
|
||||
};
|
||||
|
||||
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 now();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue