@@ -62,6 +62,9 @@ void ZonedDateTimePrototype::initialize(Realm& realm)
62
62
define_native_accessor (realm, vm.names .offset , offset_getter, {}, Attribute::Configurable);
63
63
64
64
u8 attr = Attribute::Writable | Attribute::Configurable;
65
+ define_native_function (realm, vm.names .toString , to_string, 0 , attr);
66
+ define_native_function (realm, vm.names .toLocaleString , to_locale_string, 0 , attr);
67
+ define_native_function (realm, vm.names .toJSON , to_json, 0 , attr);
65
68
define_native_function (realm, vm.names .valueOf , value_of, 0 , attr);
66
69
}
67
70
@@ -339,6 +342,72 @@ JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::offset_getter)
339
342
return PrimitiveString::create (vm, format_utc_offset_nanoseconds (offset_nanoseconds));
340
343
}
341
344
345
+ // 6.3.41 Temporal.ZonedDateTime.prototype.toString ( [ options ] ), https://tc39.es/proposal-temporal/#sec-temporal.zoneddatetime.prototype.tostring
346
+ JS_DEFINE_NATIVE_FUNCTION (ZonedDateTimePrototype::to_string)
347
+ {
348
+ // 1. Let zonedDateTime be the this value.
349
+ // 2. Perform ? RequireInternalSlot(zonedDateTime, [[InitializedTemporalZonedDateTime]]).
350
+ auto zoned_date_time = TRY (typed_this_object (vm));
351
+
352
+ // 3. Let resolvedOptions be ? GetOptionsObject(options).
353
+ auto resolved_options = TRY (get_options_object (vm, vm.argument (0 )));
354
+
355
+ // 4. NOTE: The following steps read options and perform independent validation in alphabetical order
356
+ // (GetTemporalShowCalendarNameOption reads "calendarName", GetTemporalFractionalSecondDigitsOption reads
357
+ // "fractionalSecondDigits", GetTemporalShowOffsetOption reads "offset", and GetRoundingModeOption reads "roundingMode").
358
+
359
+ // 5. Let showCalendar be ? GetTemporalShowCalendarNameOption(resolvedOptions).
360
+ auto show_calendar = TRY (get_temporal_show_calendar_name_option (vm, resolved_options));
361
+
362
+ // 6. Let digits be ? GetTemporalFractionalSecondDigitsOption(resolvedOptions).
363
+ auto digits = TRY (get_temporal_fractional_second_digits_option (vm, resolved_options));
364
+
365
+ // 7. Let showOffset be ? GetTemporalShowOffsetOption(resolvedOptions).
366
+ auto show_offset = TRY (get_temporal_show_offset_option (vm, resolved_options));
367
+
368
+ // 8. Let roundingMode be ? GetRoundingModeOption(resolvedOptions, TRUNC).
369
+ auto rounding_mode = TRY (get_rounding_mode_option (vm, resolved_options, RoundingMode::Trunc));
370
+
371
+ // 9. Let smallestUnit be ? GetTemporalUnitValuedOption(resolvedOptions, "smallestUnit", TIME, UNSET).
372
+ auto smallest_unit = TRY (get_temporal_unit_valued_option (vm, resolved_options, vm.names .smallestUnit , UnitGroup::Time, Unset {}));
373
+
374
+ // 10. If smallestUnit is hour, throw a RangeError exception.
375
+ if (auto const * unit = smallest_unit.get_pointer <Unit>(); unit && *unit == Unit::Hour)
376
+ return vm.throw_completion <RangeError>(ErrorType::OptionIsNotValidValue, temporal_unit_to_string (*unit), vm.names .smallestUnit );
377
+
378
+ // 11. Let showTimeZone be ? GetTemporalShowTimeZoneNameOption(resolvedOptions).
379
+ auto show_time_zone = TRY (get_temporal_show_time_zone_name_option (vm, resolved_options));
380
+
381
+ // 12. Let precision be ToSecondsStringPrecisionRecord(smallestUnit, digits).
382
+ auto precision = to_seconds_string_precision_record (smallest_unit, digits);
383
+
384
+ // 13. Return TemporalZonedDateTimeToString(zonedDateTime, precision.[[Precision]], showCalendar, showTimeZone, showOffset, precision.[[Increment]], precision.[[Unit]], roundingMode).
385
+ return PrimitiveString::create (vm, temporal_zoned_date_time_to_string (zoned_date_time, precision.precision , show_calendar, show_time_zone, show_offset, precision.increment , precision.unit , rounding_mode));
386
+ }
387
+
388
+ // 6.3.42 Temporal.ZonedDateTime.prototype.toLocaleString ( [ locales [ , options ] ] ), https://tc39.es/proposal-temporal/#sec-temporal.zoneddatetime.prototype.tolocalestring
389
+ // NOTE: This is the minimum toLocaleString implementation for engines without ECMA-402.
390
+ JS_DEFINE_NATIVE_FUNCTION (ZonedDateTimePrototype::to_locale_string)
391
+ {
392
+ // 1. Let zonedDateTime be the this value.
393
+ // 2. Perform ? RequireInternalSlot(zonedDateTime, [[InitializedTemporalZonedDateTime]]).
394
+ auto zoned_date_time = TRY (typed_this_object (vm));
395
+
396
+ // 3. Return TemporalZonedDateTimeToString(zonedDateTime, AUTO, AUTO, AUTO, AUTO).
397
+ return PrimitiveString::create (vm, temporal_zoned_date_time_to_string (zoned_date_time, Auto {}, ShowCalendar::Auto, ShowTimeZoneName::Auto, ShowOffset::Auto));
398
+ }
399
+
400
+ // 6.3.43 Temporal.ZonedDateTime.prototype.toJSON ( ), https://tc39.es/proposal-temporal/#sec-temporal.zoneddatetime.prototype.tojson
401
+ JS_DEFINE_NATIVE_FUNCTION (ZonedDateTimePrototype::to_json)
402
+ {
403
+ // 1. Let zonedDateTime be the this value.
404
+ // 2. Perform ? RequireInternalSlot(zonedDateTime, [[InitializedTemporalZonedDateTime]]).
405
+ auto zoned_date_time = TRY (typed_this_object (vm));
406
+
407
+ // 3. Return TemporalZonedDateTimeToString(zonedDateTime, AUTO, AUTO, AUTO, AUTO).
408
+ return PrimitiveString::create (vm, temporal_zoned_date_time_to_string (zoned_date_time, Auto {}, ShowCalendar::Auto, ShowTimeZoneName::Auto, ShowOffset::Auto));
409
+ }
410
+
342
411
// 6.3.44 Temporal.ZonedDateTime.prototype.valueOf ( ), https://tc39.es/proposal-temporal/#sec-temporal.zoneddatetime.prototype.valueof
343
412
JS_DEFINE_NATIVE_FUNCTION (ZonedDateTimePrototype::value_of)
344
413
{
0 commit comments