diff options
author | Luke Wilde <lukew@serenityos.org> | 2021-11-10 00:26:33 +0000 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2021-11-10 12:56:56 +0000 |
commit | a9ad993e780930970d7611b23ea79df22abec9d5 (patch) | |
tree | b6276baca3aee46dd87ada2e1c1a04dfcb599ea6 | |
parent | dc72d416b259031155fdfad251c4fb53008b8189 (diff) | |
download | serenity-a9ad993e780930970d7611b23ea79df22abec9d5.zip |
LibJS: Implement Temporal.ZonedDateTime.prototype.toString
3 files changed, 130 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp index 34b764a98a..de82aedc34 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp @@ -70,6 +70,7 @@ void ZonedDateTimePrototype::initialize(GlobalObject& global_object) define_native_function(vm.names.withTimeZone, with_time_zone, 1, attr); define_native_function(vm.names.withCalendar, with_calendar, 1, attr); define_native_function(vm.names.equals, equals, 1, attr); + define_native_function(vm.names.toString, to_string, 0, attr); define_native_function(vm.names.valueOf, value_of, 0, attr); define_native_function(vm.names.startOfDay, start_of_day, 0, attr); define_native_function(vm.names.toInstant, to_instant, 0, attr); @@ -832,6 +833,35 @@ JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::equals) return Value(TRY(calendar_equals(global_object, zoned_date_time->calendar(), other->calendar()))); } +// 6.3.41 Temporal.ZonedDateTime.prototype.toString ( [ options ] ), https://tc39.es/proposal-temporal/#sec-temporal.zoneddatetime.prototype.tostring +JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::to_string) +{ + // 1. Let zonedDateTime be the this value. + // 2. Perform ? RequireInternalSlot(zonedDateTime, [[InitializedTemporalZonedDateTime]]). + auto* zoned_date_time = TRY(typed_this_object(global_object)); + + // 3. Set options to ? GetOptionsObject(options). + auto* options = TRY(get_options_object(global_object, vm.argument(0))); + + // 4. Let precision be ? ToSecondsStringPrecision(options). + auto precision = TRY(to_seconds_string_precision(global_object, *options)); + + // 5. Let roundingMode be ? ToTemporalRoundingMode(options, "trunc"). + auto rounding_mode = TRY(to_temporal_rounding_mode(global_object, *options, "trunc")); + + // 6. Let showCalendar be ? ToShowCalendarOption(options). + auto show_calendar = TRY(to_show_calendar_option(global_object, *options)); + + // 7. Let showTimeZone be ? ToShowTimeZoneNameOption(options). + auto show_time_zone = TRY(to_show_time_zone_name_option(global_object, *options)); + + // 8. Let showOffset be ? ToShowOffsetOption(options). + auto show_offset = TRY(to_show_offset_option(global_object, *options)); + + // 9. Return ? TemporalZonedDateTimeToString(zonedDateTime, precision.[[Precision]], showCalendar, showTimeZone, showOffset, precision.[[Increment]], precision.[[Unit]], roundingMode). + return js_string(vm, TRY(temporal_zoned_date_time_to_string(global_object, *zoned_date_time, precision.precision, show_calendar, show_time_zone, show_offset, precision.increment, precision.unit, rounding_mode))); +} + // 6.3.44 Temporal.ZonedDateTime.prototype.valueOf ( ), https://tc39.es/proposal-temporal/#sec-temporal.zoneddatetime.prototype.valueof JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::value_of) { diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.h b/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.h index b0f268b6da..a290750734 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.h @@ -54,6 +54,7 @@ private: JS_DECLARE_NATIVE_FUNCTION(with_time_zone); JS_DECLARE_NATIVE_FUNCTION(with_calendar); JS_DECLARE_NATIVE_FUNCTION(equals); + JS_DECLARE_NATIVE_FUNCTION(to_string); JS_DECLARE_NATIVE_FUNCTION(value_of); JS_DECLARE_NATIVE_FUNCTION(start_of_day); JS_DECLARE_NATIVE_FUNCTION(to_instant); diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/ZonedDateTime/ZonedDateTime.prototype.toString.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/ZonedDateTime/ZonedDateTime.prototype.toString.js new file mode 100644 index 0000000000..eb2f8938b8 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/ZonedDateTime/ZonedDateTime.prototype.toString.js @@ -0,0 +1,99 @@ +describe("correct behavior", () => { + test("length is 0", () => { + expect(Temporal.ZonedDateTime.prototype.toString).toHaveLength(0); + }); + + test("basic functionality", () => { + const plainDateTime = new Temporal.PlainDateTime(2021, 11, 3, 1, 33, 5, 100, 200, 300); + const timeZone = new Temporal.TimeZone("UTC"); + const zonedDateTime = plainDateTime.toZonedDateTime(timeZone); + expect(zonedDateTime.toString()).toBe("2021-11-03T01:33:05.1002003+00:00[UTC]"); + }); + + test("fractionalSecondDigits option", () => { + const plainDateTime = new Temporal.PlainDateTime(2021, 11, 3, 1, 33, 5, 100, 200, 300); + const timeZone = new Temporal.TimeZone("UTC"); + const zonedDateTime = plainDateTime.toZonedDateTime(timeZone); + const values = [ + ["auto", "2021-11-03T01:33:05.1002003+00:00[UTC]"], + [0, "2021-11-03T01:33:05+00:00[UTC]"], + [1, "2021-11-03T01:33:05.1+00:00[UTC]"], + [2, "2021-11-03T01:33:05.10+00:00[UTC]"], + [3, "2021-11-03T01:33:05.100+00:00[UTC]"], + [4, "2021-11-03T01:33:05.1002+00:00[UTC]"], + [5, "2021-11-03T01:33:05.10020+00:00[UTC]"], + [6, "2021-11-03T01:33:05.100200+00:00[UTC]"], + [7, "2021-11-03T01:33:05.1002003+00:00[UTC]"], + [8, "2021-11-03T01:33:05.10020030+00:00[UTC]"], + [9, "2021-11-03T01:33:05.100200300+00:00[UTC]"], + ]; + + for (const [fractionalSecondDigits, expected] of values) { + const options = { fractionalSecondDigits }; + expect(zonedDateTime.toString(options)).toBe(expected); + } + + // Ignored when smallestUnit is given + expect(zonedDateTime.toString({ smallestUnit: "minute", fractionalSecondDigits: 9 })).toBe( + "2021-11-03T01:33+00:00[UTC]" + ); + }); + + test("smallestUnit option", () => { + const plainDateTime = new Temporal.PlainDateTime(2021, 11, 3, 1, 33, 5, 100, 200, 300); + const timeZone = new Temporal.TimeZone("UTC"); + const zonedDateTime = plainDateTime.toZonedDateTime(timeZone); + const values = [ + ["minute", "2021-11-03T01:33+00:00[UTC]"], + ["second", "2021-11-03T01:33:05+00:00[UTC]"], + ["millisecond", "2021-11-03T01:33:05.100+00:00[UTC]"], + ["microsecond", "2021-11-03T01:33:05.100200+00:00[UTC]"], + ["nanosecond", "2021-11-03T01:33:05.100200300+00:00[UTC]"], + ]; + + for (const [smallestUnit, expected] of values) { + const singularOptions = { smallestUnit }; + const pluralOptions = { smallestUnit: `${smallestUnit}s` }; + expect(zonedDateTime.toString(singularOptions)).toBe(expected); + expect(zonedDateTime.toString(pluralOptions)).toBe(expected); + } + }); + + test("timeZoneName option", () => { + const plainDateTime = new Temporal.PlainDateTime(2021, 11, 3, 1, 33, 5, 100, 200, 300); + const timeZone = new Temporal.TimeZone("UTC"); + const zonedDateTime = plainDateTime.toZonedDateTime(timeZone); + const values = [ + ["auto", "2021-11-03T01:33:05.1002003+00:00[UTC]"], + ["never", "2021-11-03T01:33:05.1002003+00:00"], + ]; + + for (const [timeZoneName, expected] of values) { + const options = { timeZoneName }; + expect(zonedDateTime.toString(options)).toBe(expected); + } + }); + + test("offset option", () => { + const plainDateTime = new Temporal.PlainDateTime(2021, 11, 3, 1, 33, 5, 100, 200, 300); + const timeZone = new Temporal.TimeZone("UTC"); + const zonedDateTime = plainDateTime.toZonedDateTime(timeZone); + const values = [ + ["auto", "2021-11-03T01:33:05.1002003+00:00[UTC]"], + ["never", "2021-11-03T01:33:05.1002003[UTC]"], + ]; + + for (const [offset, expected] of values) { + const options = { offset }; + expect(zonedDateTime.toString(options)).toBe(expected); + } + }); +}); + +describe("errors", () => { + test("this value must be a Temporal.ZonedDateTime object", () => { + expect(() => { + Temporal.ZonedDateTime.prototype.toString.call("foo"); + }).toThrowWithMessage(TypeError, "Not an object of type Temporal.ZonedDateTime"); + }); +}); |