mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 09:37:34 +00:00
LibJS: Implement Temporal.ZonedDateTime.prototype.offset
This commit is contained in:
parent
f937e9b966
commit
6c345c8107
6 changed files with 45 additions and 2 deletions
|
@ -283,6 +283,7 @@ namespace JS {
|
||||||
P(next) \
|
P(next) \
|
||||||
P(now) \
|
P(now) \
|
||||||
P(of) \
|
P(of) \
|
||||||
|
P(offset) \
|
||||||
P(offsetNanoseconds) \
|
P(offsetNanoseconds) \
|
||||||
P(ownKeys) \
|
P(ownKeys) \
|
||||||
P(padEnd) \
|
P(padEnd) \
|
||||||
|
|
|
@ -416,7 +416,7 @@ double get_offset_nanoseconds_for(GlobalObject& global_object, Value time_zone,
|
||||||
}
|
}
|
||||||
|
|
||||||
// 11.6.12 BuiltinTimeZoneGetOffsetStringFor ( timeZone, instant )
|
// 11.6.12 BuiltinTimeZoneGetOffsetStringFor ( timeZone, instant )
|
||||||
Optional<String> builtin_time_zone_get_offset_string_for(GlobalObject& global_object, TimeZone& time_zone, Instant& instant)
|
Optional<String> builtin_time_zone_get_offset_string_for(GlobalObject& global_object, Object& time_zone, Instant& instant)
|
||||||
{
|
{
|
||||||
auto& vm = global_object.vm();
|
auto& vm = global_object.vm();
|
||||||
|
|
||||||
|
|
|
@ -43,7 +43,7 @@ double parse_time_zone_offset_string(GlobalObject&, String const&);
|
||||||
String format_time_zone_offset_string(double offset_nanoseconds);
|
String format_time_zone_offset_string(double offset_nanoseconds);
|
||||||
Object* to_temporal_time_zone(GlobalObject&, Value temporal_time_zone_like);
|
Object* to_temporal_time_zone(GlobalObject&, Value temporal_time_zone_like);
|
||||||
double get_offset_nanoseconds_for(GlobalObject&, Value time_zone, Instant&);
|
double get_offset_nanoseconds_for(GlobalObject&, Value time_zone, Instant&);
|
||||||
Optional<String> builtin_time_zone_get_offset_string_for(GlobalObject&, TimeZone&, Instant&);
|
Optional<String> builtin_time_zone_get_offset_string_for(GlobalObject&, Object& time_zone, Instant&);
|
||||||
PlainDateTime* builtin_time_zone_get_plain_date_time_for(GlobalObject&, Value time_zone, Instant&, Object& calendar);
|
PlainDateTime* builtin_time_zone_get_plain_date_time_for(GlobalObject&, Value time_zone, Instant&, Object& calendar);
|
||||||
|
|
||||||
bool is_valid_time_zone_numeric_utc_offset_syntax(String const&);
|
bool is_valid_time_zone_numeric_utc_offset_syntax(String const&);
|
||||||
|
|
|
@ -55,6 +55,7 @@ void ZonedDateTimePrototype::initialize(GlobalObject& global_object)
|
||||||
define_native_accessor(vm.names.monthsInYear, months_in_year_getter, {}, Attribute::Configurable);
|
define_native_accessor(vm.names.monthsInYear, months_in_year_getter, {}, Attribute::Configurable);
|
||||||
define_native_accessor(vm.names.inLeapYear, in_leap_year_getter, {}, Attribute::Configurable);
|
define_native_accessor(vm.names.inLeapYear, in_leap_year_getter, {}, Attribute::Configurable);
|
||||||
define_native_accessor(vm.names.offsetNanoseconds, offset_nanoseconds_getter, {}, Attribute::Configurable);
|
define_native_accessor(vm.names.offsetNanoseconds, offset_nanoseconds_getter, {}, Attribute::Configurable);
|
||||||
|
define_native_accessor(vm.names.offset, offset_getter, {}, Attribute::Configurable);
|
||||||
}
|
}
|
||||||
|
|
||||||
static ZonedDateTime* typed_this(GlobalObject& global_object)
|
static ZonedDateTime* typed_this(GlobalObject& global_object)
|
||||||
|
@ -671,4 +672,23 @@ JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::offset_nanoseconds_getter)
|
||||||
return Value(get_offset_nanoseconds_for(global_object, &time_zone, *instant));
|
return Value(get_offset_nanoseconds_for(global_object, &time_zone, *instant));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 6.3.29 get Temporal.ZonedDateTime.prototype.offset, https://tc39.es/proposal-temporal/#sec-get-temporal.zoneddatetime.prototype.offset
|
||||||
|
JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::offset_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 instant be ! CreateTemporalInstant(zonedDateTime.[[Nanoseconds]]).
|
||||||
|
auto* instant = create_temporal_instant(global_object, zoned_date_time->nanoseconds());
|
||||||
|
|
||||||
|
// 4. Return ? BuiltinTimeZoneGetOffsetStringFor(zonedDateTime.[[TimeZone]], instant).
|
||||||
|
auto offset_string = builtin_time_zone_get_offset_string_for(global_object, zoned_date_time->time_zone(), *instant);
|
||||||
|
if (vm.exception())
|
||||||
|
return {};
|
||||||
|
return js_string(vm, move(*offset_string));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -44,6 +44,7 @@ private:
|
||||||
JS_DECLARE_NATIVE_FUNCTION(months_in_year_getter);
|
JS_DECLARE_NATIVE_FUNCTION(months_in_year_getter);
|
||||||
JS_DECLARE_NATIVE_FUNCTION(in_leap_year_getter);
|
JS_DECLARE_NATIVE_FUNCTION(in_leap_year_getter);
|
||||||
JS_DECLARE_NATIVE_FUNCTION(offset_nanoseconds_getter);
|
JS_DECLARE_NATIVE_FUNCTION(offset_nanoseconds_getter);
|
||||||
|
JS_DECLARE_NATIVE_FUNCTION(offset_getter);
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,21 @@
|
||||||
|
describe("correct behavior", () => {
|
||||||
|
test("basic functionality", () => {
|
||||||
|
const timeZone = new Temporal.TimeZone("UTC");
|
||||||
|
const zonedDateTime = new Temporal.ZonedDateTime(0n, timeZone);
|
||||||
|
expect(zonedDateTime.offset).toBe("+00:00");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("custom offset", () => {
|
||||||
|
const timeZone = new Temporal.TimeZone("+01:30");
|
||||||
|
const zonedDateTime = new Temporal.ZonedDateTime(0n, timeZone);
|
||||||
|
expect(zonedDateTime.offset).toBe("+01:30");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test("errors", () => {
|
||||||
|
test("this value must be a Temporal.ZonedDateTime object", () => {
|
||||||
|
expect(() => {
|
||||||
|
Reflect.get(Temporal.ZonedDateTime.prototype, "offset", "foo");
|
||||||
|
}).toThrowWithMessage(TypeError, "Not a Temporal.ZonedDateTime");
|
||||||
|
});
|
||||||
|
});
|
Loading…
Add table
Add a link
Reference in a new issue