1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 15:07:45 +00:00

Userland: Remove remaining users of Duration::now_realtime()

This is a clear sign that they want to use a UnixDateTime instead.

This also adds support for placing durations and date times into SQL
databases via their millisecond offset to UTC.
This commit is contained in:
kleines Filmröllchen 2023-03-13 22:35:22 +01:00 committed by Jelle Raaijmakers
parent 82c681e44b
commit effcd080ca
16 changed files with 62 additions and 45 deletions

View file

@ -12,9 +12,10 @@
namespace Web::Cookie {
static DeprecatedString time_to_string(Duration const& time)
static DeprecatedString time_to_string(UnixDateTime const& time)
{
auto local_time = Core::DateTime::from_timestamp(time.to_seconds());
// FIXME: This roundabout formatting should not be necessary; it also loses precision.
auto local_time = Core::DateTime::from_timestamp(time.seconds_since_epoch());
return local_time.to_deprecated_string("%Y-%m-%d %H:%M:%S %Z"sv);
}
@ -87,11 +88,11 @@ ErrorOr<Web::Cookie::Cookie> IPC::decode(Decoder& decoder)
auto value = TRY(decoder.decode<DeprecatedString>());
auto domain = TRY(decoder.decode<DeprecatedString>());
auto path = TRY(decoder.decode<DeprecatedString>());
auto creation_time = TRY(decoder.decode<Duration>());
auto expiry_time = TRY(decoder.decode<Duration>());
auto creation_time = TRY(decoder.decode<UnixDateTime>());
auto expiry_time = TRY(decoder.decode<UnixDateTime>());
auto host_only = TRY(decoder.decode<bool>());
auto http_only = TRY(decoder.decode<bool>());
auto last_access_time = TRY(decoder.decode<Duration>());
auto last_access_time = TRY(decoder.decode<UnixDateTime>());
auto persistent = TRY(decoder.decode<bool>());
auto secure = TRY(decoder.decode<bool>());
auto same_site = TRY(decoder.decode<Web::Cookie::SameSite>());

View file

@ -32,9 +32,9 @@ struct Cookie {
DeprecatedString name;
DeprecatedString value;
SameSite same_site;
Duration creation_time {};
Duration last_access_time {};
Duration expiry_time {};
UnixDateTime creation_time {};
UnixDateTime last_access_time {};
UnixDateTime expiry_time {};
DeprecatedString domain {};
DeprecatedString path {};
bool secure { false };

View file

@ -27,7 +27,7 @@ static void on_path_attribute(ParsedCookie& parsed_cookie, StringView attribute_
static void on_secure_attribute(ParsedCookie& parsed_cookie);
static void on_http_only_attribute(ParsedCookie& parsed_cookie);
static void on_same_site_attribute(ParsedCookie& parsed_cookie, StringView attribute_value);
static Optional<Duration> parse_date_time(StringView date_string);
static Optional<UnixDateTime> parse_date_time(StringView date_string);
Optional<ParsedCookie> parse_cookie(DeprecatedString const& cookie_string)
{
@ -169,10 +169,10 @@ void on_max_age_attribute(ParsedCookie& parsed_cookie, StringView attribute_valu
if (auto delta_seconds = attribute_value.to_int(); delta_seconds.has_value()) {
if (*delta_seconds <= 0) {
// If delta-seconds is less than or equal to zero (0), let expiry-time be the earliest representable date and time.
parsed_cookie.expiry_time_from_max_age_attribute = Duration::min();
parsed_cookie.expiry_time_from_max_age_attribute = UnixDateTime::earliest();
} else {
// Otherwise, let the expiry-time be the current date and time plus delta-seconds seconds.
parsed_cookie.expiry_time_from_max_age_attribute = Duration::now_realtime() + Duration::from_seconds(*delta_seconds);
parsed_cookie.expiry_time_from_max_age_attribute = UnixDateTime::now() + Duration::from_seconds(*delta_seconds);
}
}
}
@ -236,7 +236,7 @@ void on_same_site_attribute(ParsedCookie& parsed_cookie, StringView attribute_va
parsed_cookie.same_site_attribute = same_site_from_string(attribute_value);
}
Optional<Duration> parse_date_time(StringView date_string)
Optional<UnixDateTime> parse_date_time(StringView date_string)
{
// https://tools.ietf.org/html/rfc6265#section-5.1.1
unsigned hour = 0;
@ -345,7 +345,8 @@ Optional<Duration> parse_date_time(StringView date_string)
// day-of-month-value, the month-value, the year-value, the hour-value, the minute-value, and the second-value, respectively.
// If no such date exists, abort these steps and fail to parse the cookie-date.
// FIXME: Fail on dates that do not exist.
auto parsed_cookie_date = Duration::from_timestamp(year, month, day_of_month, hour, minute, second, 0);
// FIXME: This currently uses UNIX time, which is not equivalent to UTC due to leap seconds.
auto parsed_cookie_date = UnixDateTime::from_unix_time_parts(year, month, day_of_month, hour, minute, second, 0);
// 7. Return the parsed-cookie-date as the result of this algorithm.
return parsed_cookie_date;
@ -374,8 +375,8 @@ ErrorOr<Web::Cookie::ParsedCookie> IPC::decode(Decoder& decoder)
{
auto name = TRY(decoder.decode<DeprecatedString>());
auto value = TRY(decoder.decode<DeprecatedString>());
auto expiry_time_from_expires_attribute = TRY(decoder.decode<Optional<Duration>>());
auto expiry_time_from_max_age_attribute = TRY(decoder.decode<Optional<Duration>>());
auto expiry_time_from_expires_attribute = TRY(decoder.decode<Optional<UnixDateTime>>());
auto expiry_time_from_max_age_attribute = TRY(decoder.decode<Optional<UnixDateTime>>());
auto domain = TRY(decoder.decode<Optional<DeprecatedString>>());
auto path = TRY(decoder.decode<Optional<DeprecatedString>>());
auto secure_attribute_present = TRY(decoder.decode<bool>());

View file

@ -18,8 +18,8 @@ struct ParsedCookie {
DeprecatedString name;
DeprecatedString value;
SameSite same_site_attribute { SameSite::Default };
Optional<Duration> expiry_time_from_expires_attribute {};
Optional<Duration> expiry_time_from_max_age_attribute {};
Optional<UnixDateTime> expiry_time_from_expires_attribute {};
Optional<UnixDateTime> expiry_time_from_max_age_attribute {};
Optional<DeprecatedString> domain {};
Optional<DeprecatedString> path {};
bool secure_attribute_present { false };

View file

@ -1651,7 +1651,7 @@ void Document::completely_finish_loading()
VERIFY(browsing_context());
// 2. Set document's completely loaded time to the current time.
m_completely_loaded_time = AK::Duration::now_realtime();
m_completely_loaded_time = AK::UnixDateTime::now();
// 3. Let container be document's browsing context's container.
auto container = JS::make_handle(browsing_context()->container());

View file

@ -612,7 +612,7 @@ private:
JS::GCPtr<HTMLCollection> m_all;
// https://html.spec.whatwg.org/#completely-loaded-time
Optional<AK::Duration> m_completely_loaded_time;
Optional<AK::UnixDateTime> m_completely_loaded_time;
// https://html.spec.whatwg.org/multipage/dom.html#concept-document-navigation-id
Optional<String> m_navigation_id;

View file

@ -59,7 +59,7 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<File>> File::create(JS::Realm& realm, Vecto
// 3. If the lastModified member is provided, let d be set to the lastModified dictionary member. If it is not provided, set d to the current date and time represented as the number of milliseconds since the Unix Epoch (which is the equivalent of Date.now() [ECMA-262]).
// Note: Since ECMA-262 Date objects convert to long long values representing the number of milliseconds since the Unix Epoch, the lastModified member could be a Date object [ECMA-262].
last_modified = options->last_modified.has_value() ? options->last_modified.value() : Duration::now_realtime().to_milliseconds();
last_modified = options->last_modified.has_value() ? options->last_modified.value() : UnixDateTime::now().milliseconds_since_epoch();
}
// 4. Return a new File object F such that: