1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 12:47:35 +00:00

LibJS: Implement Temporal.ZonedDateTime.prototype.epochMilliseconds

This commit is contained in:
Linus Groh 2021-08-04 23:02:52 +01:00 committed by Andreas Kling
parent 47135af987
commit a9162b7e98
3 changed files with 36 additions and 0 deletions

View file

@ -43,6 +43,7 @@ void ZonedDateTimePrototype::initialize(GlobalObject& global_object)
define_native_accessor(vm.names.microsecond, microsecond_getter, {}, Attribute::Configurable); define_native_accessor(vm.names.microsecond, microsecond_getter, {}, Attribute::Configurable);
define_native_accessor(vm.names.nanosecond, nanosecond_getter, {}, Attribute::Configurable); define_native_accessor(vm.names.nanosecond, nanosecond_getter, {}, Attribute::Configurable);
define_native_accessor(vm.names.epochSeconds, epoch_seconds_getter, {}, Attribute::Configurable); define_native_accessor(vm.names.epochSeconds, epoch_seconds_getter, {}, Attribute::Configurable);
define_native_accessor(vm.names.epochMilliseconds, epoch_milliseconds_getter, {}, Attribute::Configurable);
} }
static ZonedDateTime* typed_this(GlobalObject& global_object) static ZonedDateTime* typed_this(GlobalObject& global_object)
@ -373,4 +374,23 @@ JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::epoch_seconds_getter)
return Value((double)s.to_base(10).to_int<i64>().value()); return Value((double)s.to_base(10).to_int<i64>().value());
} }
// 6.3.16 get Temporal.ZonedDateTime.prototype.epochMilliseconds, https://tc39.es/proposal-temporal/#sec-get-temporal.zoneddatetime.prototype.epochmilliseconds
JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::epoch_milliseconds_getter)
{
// 1. Let zonedDateTime be the this value.
// 2. Perform ? RequireInternalSlot(zonedDateTime, [[InitializedTemporalZonedDateTime]]).
auto* zoned_date_time = typed_this(global_object);
if (vm.exception())
return {};
// 3. Let ns be zonedDateTime.[[Nanoseconds]].
auto& ns = zoned_date_time->nanoseconds();
// 4. Let ms be RoundTowardsZero((ns) / 10^6).
auto ms = ns.big_integer().divided_by(Crypto::UnsignedBigInteger { 1'000'000 }).quotient;
// 5. Return 𝔽(ms).
return Value((double)ms.to_base(10).to_int<i64>().value());
}
} }

View file

@ -32,6 +32,7 @@ private:
JS_DECLARE_NATIVE_FUNCTION(microsecond_getter); JS_DECLARE_NATIVE_FUNCTION(microsecond_getter);
JS_DECLARE_NATIVE_FUNCTION(nanosecond_getter); JS_DECLARE_NATIVE_FUNCTION(nanosecond_getter);
JS_DECLARE_NATIVE_FUNCTION(epoch_seconds_getter); JS_DECLARE_NATIVE_FUNCTION(epoch_seconds_getter);
JS_DECLARE_NATIVE_FUNCTION(epoch_milliseconds_getter);
}; };
} }

View file

@ -0,0 +1,15 @@
describe("correct behavior", () => {
test("basic functionality", () => {
const timeZone = new Temporal.TimeZone("UTC");
const zonedDateTime = new Temporal.ZonedDateTime(1625614921000000000n, timeZone);
expect(zonedDateTime.epochMilliseconds).toBe(1625614921000);
});
});
test("errors", () => {
test("this value must be a Temporal.ZonedDateTime object", () => {
expect(() => {
Reflect.get(Temporal.ZonedDateTime.prototype, "epochMilliseconds", "foo");
}).toThrowWithMessage(TypeError, "Not a Temporal.ZonedDateTime");
});
});