mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 12:07:45 +00:00
LibJS: Implement ECMA-402 Date.prototype.toLocaleTimeString
This commit is contained in:
parent
4d310fd7aa
commit
2e4e0195de
2 changed files with 81 additions and 6 deletions
|
@ -731,17 +731,29 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::to_locale_string)
|
||||||
return js_string(vm, move(formatted));
|
return js_string(vm, move(formatted));
|
||||||
}
|
}
|
||||||
|
|
||||||
// 21.4.4.40 Date.prototype.toLocaleTimeString ( [ reserved1 [ , reserved2 ] ] ), https://tc39.es/ecma262/#sec-date.prototype.tolocaletimestring
|
// 18.4.3 Date.prototype.toLocaleTimeString ( [ locales [ , options ] ] ), https://tc39.es/ecma402/#sup-date.prototype.tolocaletimestring
|
||||||
JS_DEFINE_NATIVE_FUNCTION(DatePrototype::to_locale_time_string)
|
JS_DEFINE_NATIVE_FUNCTION(DatePrototype::to_locale_time_string)
|
||||||
{
|
{
|
||||||
|
auto locales = vm.argument(0);
|
||||||
|
auto options = vm.argument(1);
|
||||||
|
|
||||||
|
// 1. Let x be ? thisTimeValue(this value).
|
||||||
auto* this_object = TRY(typed_this_object(global_object));
|
auto* this_object = TRY(typed_this_object(global_object));
|
||||||
|
auto time = this_object->is_invalid() ? js_nan() : this_object->value_of();
|
||||||
|
|
||||||
if (this_object->is_invalid())
|
// 2. If x is NaN, return "Invalid Date".
|
||||||
return js_string(vm, "Invalid Date");
|
if (time.is_nan())
|
||||||
|
return js_string(vm, "Invalid Date"sv);
|
||||||
|
|
||||||
// FIXME: Optional locales, options params.
|
// 3. Let options be ? ToDateTimeOptions(options, "time", "time").
|
||||||
auto string = this_object->locale_time_string();
|
options = Value(TRY(Intl::to_date_time_options(global_object, options, Intl::OptionRequired::Time, Intl::OptionDefaults::Time)));
|
||||||
return js_string(vm, move(string));
|
|
||||||
|
// 4. Let timeFormat be ? Construct(%DateTimeFormat%, « locales, options »).
|
||||||
|
auto* time_format = TRY(construct_date_time_format(global_object, locales, options));
|
||||||
|
|
||||||
|
// 5. Return ? FormatDateTime(dateFormat, x).
|
||||||
|
auto formatted = TRY(Intl::format_date_time(global_object, *time_format, time));
|
||||||
|
return js_string(vm, move(formatted));
|
||||||
}
|
}
|
||||||
|
|
||||||
// 21.4.4.42 Date.prototype.toTimeString ( ), https://tc39.es/ecma262/#sec-date.prototype.totimestring
|
// 21.4.4.42 Date.prototype.toTimeString ( ), https://tc39.es/ecma262/#sec-date.prototype.totimestring
|
||||||
|
|
|
@ -0,0 +1,63 @@
|
||||||
|
describe("errors", () => {
|
||||||
|
test("called on non-Date object", () => {
|
||||||
|
expect(() => {
|
||||||
|
Date.prototype.toLocaleTimeString();
|
||||||
|
}).toThrowWithMessage(TypeError, "Not an object of type Date");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("called with value that cannot be converted to a number", () => {
|
||||||
|
expect(() => {
|
||||||
|
new Date(Symbol.hasInstance).toLocaleTimeString();
|
||||||
|
}).toThrowWithMessage(TypeError, "Cannot convert symbol to number");
|
||||||
|
|
||||||
|
expect(() => {
|
||||||
|
new Date(1n).toLocaleTimeString();
|
||||||
|
}).toThrowWithMessage(TypeError, "Cannot convert BigInt to number");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("time value cannot be clipped", () => {
|
||||||
|
expect(() => {
|
||||||
|
new Date(-8.65e15).toLocaleTimeString();
|
||||||
|
}).toThrowWithMessage(RangeError, "Time value must be between -8.64E15 and 8.64E15");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("dateStyle may not be specified", () => {
|
||||||
|
expect(() => {
|
||||||
|
new Date().toLocaleTimeString([], { dateStyle: "short" });
|
||||||
|
}).toThrowWithMessage(TypeError, "Option dateStyle cannot be set when also providing time");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("correct behavior", () => {
|
||||||
|
test("NaN", () => {
|
||||||
|
const d = new Date(NaN);
|
||||||
|
expect(d.toLocaleTimeString()).toBe("Invalid Date");
|
||||||
|
});
|
||||||
|
|
||||||
|
const d0 = new Date(Date.UTC(2021, 11, 7, 17, 40, 50, 456));
|
||||||
|
const d1 = new Date(Date.UTC(1989, 0, 23, 7, 8, 9, 45));
|
||||||
|
|
||||||
|
test("defaults to time", () => {
|
||||||
|
expect(d0.toLocaleTimeString("en", { timeZone: "UTC" })).toBe("5:40:50 PM");
|
||||||
|
expect(d1.toLocaleTimeString("en", { timeZone: "UTC" })).toBe("7:08:09 AM");
|
||||||
|
|
||||||
|
expect(d0.toLocaleTimeString("ar", { timeZone: "UTC" })).toBe("٥:٤٠:٥٠ م");
|
||||||
|
expect(d1.toLocaleTimeString("ar", { timeZone: "UTC" })).toBe("٧:٠٨:٠٩ ص");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("timeStyle may be set", () => {
|
||||||
|
expect(d0.toLocaleTimeString("en", { timeStyle: "long", timeZone: "UTC" })).toBe(
|
||||||
|
"5:40:50 PM UTC"
|
||||||
|
);
|
||||||
|
expect(d1.toLocaleTimeString("en", { timeStyle: "long", timeZone: "UTC" })).toBe(
|
||||||
|
"7:08:09 AM UTC"
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(d0.toLocaleTimeString("ar", { timeStyle: "long", timeZone: "UTC" })).toBe(
|
||||||
|
"٥:٤٠:٥٠ م UTC"
|
||||||
|
);
|
||||||
|
expect(d1.toLocaleTimeString("ar", { timeStyle: "long", timeZone: "UTC" })).toBe(
|
||||||
|
"٧:٠٨:٠٩ ص UTC"
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
Loading…
Add table
Add a link
Reference in a new issue