LibJS: Implement Date.prototype.getTimezoneOffset

This commit is contained in:
Timothy Flynn 2022-01-15 00:48:49 -05:00 committed by Linus Groh
parent 032664332b
commit 6576d0291c
Notes: sideshowbarker 2024-07-17 20:50:34 +09:00
2 changed files with 36 additions and 4 deletions

View file

@ -245,13 +245,15 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_time)
// 21.4.4.11 Date.prototype.getTimezoneOffset ( ), https://tc39.es/ecma262/#sec-date.prototype.gettimezoneoffset
JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_timezone_offset)
{
auto* this_object = TRY(typed_this_object(global_object));
// 1. Let t be ? thisTimeValue(this value).
auto time = TRY(this_time_value(global_object, vm.this_value(global_object)));
if (!Value(this_object->date_value()).is_finite_number())
// 2. If t is NaN, return NaN.
if (time.is_nan())
return js_nan();
// FIXME: Make this actually do something once we support timezones instead of just UTC
return Value(0);
// 3. Return (t - LocalTime(t)) / msPerMinute.
return Value((time.as_double() - local_time(time.as_double())) / Date::ms_per_minute);
}
// 21.4.4.12 Date.prototype.getUTCDate ( ), https://tc39.es/ecma262/#sec-date.prototype.getutcdate

View file

@ -0,0 +1,30 @@
describe("errors", () => {
test("called on non-Date object", () => {
expect(() => {
Date.prototype.getTimezoneOffset();
}).toThrowWithMessage(TypeError, "Not an object of type Date");
});
});
describe("correct behavior", () => {
test("NaN", () => {
const d = new Date(NaN);
expect(d.getTimezoneOffset()).toBeNaN();
});
test("time clip", () => {
const d = new Date(-8.65e15);
expect(d.getTimezoneOffset()).toBeNaN();
});
test("basic functionality", () => {
// Exact return values from getTimezoneOffset depend on the time zone of the host machine.
// So we can't test exact values, but that value should not change here.
const d0 = new Date(Date.parse("1989-01-23T14:30-00:00"));
const d1 = new Date(Date.parse("1989-01-23T14:30-05:00"));
const offset0 = d0.getTimezoneOffset();
const offset1 = d1.getTimezoneOffset();
expect(offset0).toBe(offset1);
});
});