Skip to content

Commit c4123d8

Browse files
committed
LibJS: Implement Temporal.TimeZone.prototype.getOffsetStringFor()
1 parent f987c11 commit c4123d8

File tree

6 files changed

+64
-0
lines changed

6 files changed

+64
-0
lines changed

Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ namespace JS {
183183
P(getMinutes) \
184184
P(getMonth) \
185185
P(getOffsetNanosecondsFor) \
186+
P(getOffsetStringFor) \
186187
P(getOwnPropertyDescriptor) \
187188
P(getOwnPropertyDescriptors) \
188189
P(getOwnPropertyNames) \

Userland/Libraries/LibJS/Runtime/Temporal/TimeZone.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,20 @@ double get_offset_nanoseconds_for(GlobalObject& global_object, Object& time_zone
410410
return offset_nanoseconds;
411411
}
412412

413+
// 11.6.12 BuiltinTimeZoneGetOffsetStringFor ( timeZone, instant )
414+
Optional<String> builtin_time_zone_get_offset_string_for(GlobalObject& global_object, TimeZone& time_zone, Instant& instant)
415+
{
416+
auto& vm = global_object.vm();
417+
418+
// 1. Let offsetNanoseconds be ? GetOffsetNanosecondsFor(timeZone, instant).
419+
auto offset_nanoseconds = get_offset_nanoseconds_for(global_object, time_zone, instant);
420+
if (vm.exception())
421+
return {};
422+
423+
// 2. Return ! FormatTimeZoneOffsetString(offsetNanoseconds).
424+
return format_time_zone_offset_string(offset_nanoseconds);
425+
}
426+
413427
// 11.6.13 BuiltinTimeZoneGetPlainDateTimeFor ( timeZone, instant, calendar ), https://tc39.es/proposal-temporal/#sec-temporal-builtintimezonegetplaindatetimefor
414428
PlainDateTime* builtin_time_zone_get_plain_date_time_for(GlobalObject& global_object, Object& time_zone, Instant& instant, Object& calendar)
415429
{

Userland/Libraries/LibJS/Runtime/Temporal/TimeZone.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ double parse_time_zone_offset_string(GlobalObject&, String const&);
4747
String format_time_zone_offset_string(double offset_nanoseconds);
4848
Object* to_temporal_time_zone(GlobalObject&, Value temporal_time_zone_like);
4949
double get_offset_nanoseconds_for(GlobalObject&, Object& time_zone, Instant&);
50+
Optional<String> builtin_time_zone_get_offset_string_for(GlobalObject&, TimeZone&, Instant&);
5051
PlainDateTime* builtin_time_zone_get_plain_date_time_for(GlobalObject&, Object& time_zone, Instant&, Object& calendar);
5152

5253
bool is_valid_time_zone_numeric_utc_offset_syntax(String const&);

Userland/Libraries/LibJS/Runtime/Temporal/TimeZonePrototype.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ void TimeZonePrototype::initialize(GlobalObject& global_object)
2727
u8 attr = Attribute::Writable | Attribute::Configurable;
2828
define_native_accessor(vm.names.id, id_getter, {}, Attribute::Configurable);
2929
define_native_function(vm.names.getOffsetNanosecondsFor, get_offset_nanoseconds_for, 1, attr);
30+
define_native_function(vm.names.getOffsetStringFor, get_offset_string_for, 1, attr);
3031
define_native_function(vm.names.toString, to_string, 0, attr);
3132
define_native_function(vm.names.toJSON, to_json, 0, attr);
3233

@@ -79,6 +80,27 @@ JS_DEFINE_NATIVE_FUNCTION(TimeZonePrototype::get_offset_nanoseconds_for)
7980
return Value((double)get_iana_time_zone_offset_nanoseconds(instant->nanoseconds(), time_zone->identifier()));
8081
}
8182

83+
// 11.4.5 Temporal.TimeZone.prototype.getOffsetStringFor ( instant ), https://tc39.es/proposal-temporal/#sec-temporal.timezone.prototype.getoffsetstringfor
84+
JS_DEFINE_NATIVE_FUNCTION(TimeZonePrototype::get_offset_string_for)
85+
{
86+
// 1. Let timeZone be the this value.
87+
// 2. Perform ? RequireInternalSlot(timeZone, [[InitializedTemporalTimeZone]]).
88+
auto* time_zone = typed_this(global_object);
89+
if (vm.exception())
90+
return {};
91+
92+
// 3. Set instant to ? ToTemporalInstant(instant).
93+
auto* instant = to_temporal_instant(global_object, vm.argument(0));
94+
if (vm.exception())
95+
return {};
96+
97+
// 4. Return ? BuiltinTimeZoneGetOffsetStringFor(timeZone, instant).
98+
auto offset_string = builtin_time_zone_get_offset_string_for(global_object, *time_zone, *instant);
99+
if (vm.exception())
100+
return {};
101+
return js_string(vm, move(*offset_string));
102+
}
103+
82104
// 11.4.11 Temporal.TimeZone.prototype.toString ( ), https://tc39.es/proposal-temporal/#sec-temporal.timezone.prototype.tostring
83105
JS_DEFINE_NATIVE_FUNCTION(TimeZonePrototype::to_string)
84106
{

Userland/Libraries/LibJS/Runtime/Temporal/TimeZonePrototype.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class TimeZonePrototype final : public Object {
2121
private:
2222
JS_DECLARE_NATIVE_FUNCTION(id_getter);
2323
JS_DECLARE_NATIVE_FUNCTION(get_offset_nanoseconds_for);
24+
JS_DECLARE_NATIVE_FUNCTION(get_offset_string_for);
2425
JS_DECLARE_NATIVE_FUNCTION(to_string);
2526
JS_DECLARE_NATIVE_FUNCTION(to_json);
2627
};
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
describe("correct behavior", () => {
2+
test("length is 1", () => {
3+
expect(Temporal.TimeZone.prototype.getOffsetStringFor).toHaveLength(1);
4+
});
5+
6+
test("basic functionality", () => {
7+
const timeZone = new Temporal.TimeZone("UTC");
8+
const instant = new Temporal.Instant(0n);
9+
expect(timeZone.getOffsetStringFor(instant)).toBe("+00:00");
10+
});
11+
12+
test("custom offset", () => {
13+
const timeZone = new Temporal.TimeZone("+01:30");
14+
const instant = new Temporal.Instant(0n);
15+
expect(timeZone.getOffsetStringFor(instant)).toBe("+01:30");
16+
});
17+
});
18+
19+
test("errors", () => {
20+
test("this value must be a Temporal.TimeZone object", () => {
21+
expect(() => {
22+
Temporal.TimeZone.prototype.getOffsetStringFor.call("foo");
23+
}).toThrowWithMessage(TypeError, "Not a Temporal.TimeZone");
24+
});
25+
});

0 commit comments

Comments
 (0)