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

LibJS: Re-implement the Date constructor / prototype for spec compliance

First, this adds a constructor to the Date object to be created from a
plain double. This is a first step to removing Core::DateTime as the
basis for the Date object. A subsequent commit will remove the now-
unused data from the object.

Next, this implements the constructor in accordance to the spec. The
constructor when NewTarget is undefined no longer allocates a Date on
the heap. The other constructor properly uses recently created AOs to
handle time zone and ensure the created [[DateValue]] is valid. Other
methods on the constructor (Date.now) have not been touched yet.

Last, the prototype is reimplemented. Again, we use other AOs to handle
time zones and time clipping. Not all prototypes are fixed; most of them
are, but a few (e.g. Date.prototype.getTimezoneOffset) were not fixed,
but left in a mostly unimplemented state for another commit.

In all of the above, spec comments are added. This is a rather large
change; but it's tough to do any of these parts individually without
breaking everything else.
This commit is contained in:
Timothy Flynn 2022-01-14 17:42:42 -05:00 committed by Linus Groh
parent d31e6b9391
commit d83ce7dd0b
9 changed files with 501 additions and 485 deletions

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2020-2021, Linus Groh <linusg@serenityos.org>
* Copyright (c) 2022, Tim Flynn <trflynn89@pm.me>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -20,6 +21,11 @@ Date* Date::create(GlobalObject& global_object, Core::DateTime datetime, i16 mil
return global_object.heap().allocate<Date>(global_object, datetime, milliseconds, is_invalid, *global_object.date_prototype());
}
Date* Date::create(GlobalObject& global_object, double date_value)
{
return global_object.heap().allocate<Date>(global_object, date_value, *global_object.date_prototype());
}
Date::Date(Core::DateTime datetime, i16 milliseconds, bool is_invalid, Object& prototype)
: Object(prototype)
, m_datetime(datetime)
@ -28,6 +34,12 @@ Date::Date(Core::DateTime datetime, i16 milliseconds, bool is_invalid, Object& p
{
}
Date::Date(double date_value, Object& prototype)
: Object(prototype)
, m_date_value(date_value)
{
}
Date::~Date()
{
}
@ -84,9 +96,7 @@ String Date::gmt_date_string() const
String Date::iso_date_string() const
{
auto tm = to_utc_tm();
int year = tm.tm_year + 1900;
int month = tm.tm_mon + 1;
int year = year_from_time(m_date_value);
StringBuilder builder;
if (year < 0)
@ -96,17 +106,17 @@ String Date::iso_date_string() const
else
builder.appendff("{:04}", year);
builder.append('-');
builder.appendff("{:02}", month);
builder.appendff("{:02}", month_from_time(m_date_value) + 1);
builder.append('-');
builder.appendff("{:02}", tm.tm_mday);
builder.appendff("{:02}", date_from_time(m_date_value));
builder.append('T');
builder.appendff("{:02}", tm.tm_hour);
builder.appendff("{:02}", hour_from_time(m_date_value));
builder.append(':');
builder.appendff("{:02}", tm.tm_min);
builder.appendff("{:02}", min_from_time(m_date_value));
builder.append(':');
builder.appendff("{:02}", tm.tm_sec);
builder.appendff("{:02}", sec_from_time(m_date_value));
builder.append('.');
builder.appendff("{:03}", m_milliseconds);
builder.appendff("{:03}", ms_from_time(m_date_value));
builder.append('Z');
return builder.build();