[Temporal] Add toString to PlainDateTime

Implement AOs: RoundISODateTime
Spec Text:
https://tc39.es/proposal-temporal/#sec-temporal.plaindatetime.prototype.tostring
https://tc39.es/proposal-temporal/#sec-temporal-roundisodatetime

Bug: v8:11544
Change-Id: Ib0318be8047bb01e593a43cef4405caa5979d1af
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3687568
Reviewed-by: Adam Klein <adamk@chromium.org>
Commit-Queue: Frank Tang <ftang@chromium.org>
Cr-Commit-Position: refs/heads/main@{#80985}
This commit is contained in:
Frank Tang 2022-06-06 22:30:29 -07:00 committed by V8 LUCI CQ
parent e781520367
commit dc88fb92ed
4 changed files with 123 additions and 37 deletions

View File

@ -58,8 +58,6 @@ TO_BE_IMPLEMENTED(TemporalPlainDateTimePrototypeUntil)
TO_BE_IMPLEMENTED(TemporalPlainDateTimePrototypeSince)
/* Temporal #sec-temporal.plaindatetime.prototype.round */
TO_BE_IMPLEMENTED(TemporalPlainDateTimePrototypeRound)
/* Temporal #sec-temporal.plaindatetime.prototype.tostring */
TO_BE_IMPLEMENTED(TemporalPlainDateTimePrototypeToString)
/* Temporal.ZonedDateTime */
/* Temporal #sec-temporal.zoneddatetime.from */
@ -442,6 +440,7 @@ TEMPORAL_PROTOTYPE_METHOD0(PlainDateTime, ToPlainDate, toPlainDate)
TEMPORAL_PROTOTYPE_METHOD0(PlainDateTime, ToPlainTime, toPlainTime)
TEMPORAL_PROTOTYPE_METHOD0(PlainDateTime, ToJSON, toJSON)
TEMPORAL_PROTOTYPE_METHOD2(PlainDateTime, ToLocaleString, toLocaleString)
TEMPORAL_PROTOTYPE_METHOD1(PlainDateTime, ToString, toString)
TEMPORAL_VALUE_OF(PlainDateTime)
// PlainYearMonth

View File

@ -9343,6 +9343,123 @@ MaybeHandle<String> JSTemporalPlainDateTime::ToLocaleString(
ShowCalendar::kAuto);
}
namespace {
DateTimeRecordCommon RoundTime(Isolate* isolate, const TimeRecordCommon& time,
double increment, Unit unit,
RoundingMode rounding_mode,
double day_length_ns);
// #sec-temporal-roundisodatetime
DateTimeRecordCommon RoundISODateTime(Isolate* isolate,
const DateTimeRecordCommon& date_time,
double increment, Unit unit,
RoundingMode rounding_mode,
double day_length_ns) {
TEMPORAL_ENTER_FUNC();
// 3. Let roundedTime be ! RoundTime(hour, minute, second, millisecond,
// microsecond, nanosecond, increment, unit, roundingMode, dayLength).
DateTimeRecordCommon rounded_time = RoundTime(
isolate, date_time.time, increment, unit, rounding_mode, day_length_ns);
// 4. Let balanceResult be ! BalanceISODate(year, month, day +
// roundedTime.[[Days]]).
rounded_time.date.year = date_time.date.year;
rounded_time.date.month = date_time.date.month;
rounded_time.date.day += date_time.date.day;
DateRecordCommon balance_result = BalanceISODate(isolate, rounded_time.date);
// 5. Return the Record { [[Year]]: balanceResult.[[Year]], [[Month]]:
// balanceResult.[[Month]], [[Day]]: balanceResult.[[Day]], [[Hour]]:
// roundedTime.[[Hour]], [[Minute]]: roundedTime.[[Minute]], [[Second]]:
// roundedTime.[[Second]], [[Millisecond]]: roundedTime.[[Millisecond]],
// [[Microsecond]]: roundedTime.[[Microsecond]], [[Nanosecond]]:
// roundedTime.[[Nanosecond]] }.
return {balance_result, rounded_time.time};
}
DateTimeRecordCommon RoundISODateTime(Isolate* isolate,
const DateTimeRecordCommon& date_time,
double increment, Unit unit,
RoundingMode rounding_mode) {
TEMPORAL_ENTER_FUNC();
// 2. If dayLength is not present, set dayLength to 8.64 × 10^13.
return RoundISODateTime(isolate, date_time, increment, unit, rounding_mode,
86400000000000LLU);
}
// #sec-temporal-tosecondsstringprecision
struct StringPrecision {
Precision precision;
Unit unit;
double increment;
};
// #sec-temporal-tosecondsstringprecision
Maybe<StringPrecision> ToSecondsStringPrecision(
Isolate* isolate, Handle<JSReceiver> normalized_options,
const char* method_name);
} // namespace
//
// #sec-temporal.plaindatetime.prototype.tostring
MaybeHandle<String> JSTemporalPlainDateTime::ToString(
Isolate* isolate, Handle<JSTemporalPlainDateTime> date_time,
Handle<Object> options_obj) {
const char* method_name = "Temporal.PlainDateTime.prototype.toString";
// 1. Let dateTime be the this value.
// 2. Perform ? RequireInternalSlot(dateTime,
// [[InitializedTemporalDateTime]]).
// 3. Set options to ? GetOptionsObject(options).
Handle<JSReceiver> options;
ASSIGN_RETURN_ON_EXCEPTION(
isolate, options, GetOptionsObject(isolate, options_obj, method_name),
String);
// 4. Let precision be ? ToSecondsStringPrecision(options).
StringPrecision precision;
MAYBE_ASSIGN_RETURN_ON_EXCEPTION_VALUE(
isolate, precision,
ToSecondsStringPrecision(isolate, options, method_name),
Handle<String>());
// 5. Let roundingMode be ? ToTemporalRoundingMode(options, "trunc").
RoundingMode rounding_mode;
MAYBE_ASSIGN_RETURN_ON_EXCEPTION_VALUE(
isolate, rounding_mode,
ToTemporalRoundingMode(isolate, options, RoundingMode::kTrunc,
method_name),
Handle<String>());
// 6. Let showCalendar be ? ToShowCalendarOption(options).
ShowCalendar show_calendar;
MAYBE_ASSIGN_RETURN_ON_EXCEPTION_VALUE(
isolate, show_calendar,
ToShowCalendarOption(isolate, options, method_name), Handle<String>());
// 7. Let result be ! RoundISODateTime(dateTime.[[ISOYear]],
// dateTime.[[ISOMonth]], dateTime.[[ISODay]], dateTime.[[ISOHour]],
// dateTime.[[ISOMinute]], dateTime.[[ISOSecond]],
// dateTime.[[ISOMillisecond]], dateTime.[[ISOMicrosecond]],
// dateTime.[[ISONanosecond]], precision.[[Increment]], precision.[[Unit]],
// roundingMode).
DateTimeRecordCommon result = RoundISODateTime(
isolate,
{{date_time->iso_year(), date_time->iso_month(), date_time->iso_day()},
{date_time->iso_hour(), date_time->iso_minute(), date_time->iso_second(),
date_time->iso_millisecond(), date_time->iso_microsecond(),
date_time->iso_nanosecond()}},
precision.increment, precision.unit, rounding_mode);
// 8. Return ? TemporalDateTimeToString(result.[[Year]], result.[[Month]],
// result.[[Day]], result.[[Hour]], result.[[Minute]], result.[[Second]],
// result.[[Millisecond]], result.[[Microsecond]], result.[[Nanosecond]],
// dateTime.[[Calendar]], precision.[[Precision]], showCalendar).
return TemporalDateTimeToString(isolate, result,
handle(date_time->calendar(), isolate),
precision.precision, show_calendar);
}
// #sec-temporal.now.plaindatetime
MaybeHandle<JSTemporalPlainDateTime> JSTemporalPlainDateTime::Now(
Isolate* isolate, Handle<Object> calendar_like,
@ -10992,13 +11109,6 @@ Maybe<Precision> GetFractionalSecondDigits(Isolate* isolate,
return Just(Precision::kAuto);
}
// #sec-temporal-tosecondsstringprecision
struct StringPrecision {
Precision precision;
Unit unit;
double increment;
};
// #sec-temporal-tosecondsstringprecision
Maybe<StringPrecision> ToSecondsStringPrecision(
Isolate* isolate, Handle<JSReceiver> normalized_options,

View File

@ -443,6 +443,11 @@ class JSTemporalPlainDateTime
Isolate* isolate, Handle<JSTemporalPlainDateTime> date_time,
Handle<Object> locales, Handle<Object> options);
// #sec-temporal.plaindatetime.prototype.tostring
V8_WARN_UNUSED_RESULT static MaybeHandle<String> ToString(
Isolate* isolate, Handle<JSTemporalPlainDateTime> date_time,
Handle<Object> options);
// #sec-temporal.plaindatetime.prototype.add
V8_WARN_UNUSED_RESULT static MaybeHandle<JSTemporalPlainDateTime> Add(
Isolate* isolate, Handle<JSTemporalPlainDateTime> date_time,

View File

@ -789,26 +789,7 @@
'built-ins/Temporal/PlainDateTime/prototype/since/smallestunit-wrong-type': [FAIL],
'built-ins/Temporal/PlainDateTime/prototype/subtract/balance-negative-time-units': [FAIL],
'built-ins/Temporal/PlainDateTime/prototype/toPlainDate/limits': [FAIL],
'built-ins/Temporal/PlainDateTime/prototype/toString/branding': [FAIL],
'built-ins/Temporal/PlainDateTime/prototype/toString/calendarname-invalid-string': [FAIL],
'built-ins/Temporal/PlainDateTime/prototype/toString/calendarname-undefined': [FAIL],
'built-ins/Temporal/PlainDateTime/prototype/toString/calendarname-wrong-type': [FAIL],
'built-ins/Temporal/PlainDateTime/prototype/toString/fractionalseconddigits-invalid-string': [FAIL],
'built-ins/Temporal/PlainDateTime/prototype/toString/fractionalseconddigits-nan': [FAIL],
'built-ins/Temporal/PlainDateTime/prototype/toString/fractionalseconddigits-non-integer': [FAIL],
'built-ins/Temporal/PlainDateTime/prototype/toString/fractionalseconddigits-out-of-range': [FAIL],
'built-ins/Temporal/PlainDateTime/prototype/toString/fractionalseconddigits-undefined': [FAIL],
'built-ins/Temporal/PlainDateTime/prototype/toString/fractionalseconddigits-wrong-type': [FAIL],
'built-ins/Temporal/PlainDateTime/prototype/toString/options-undefined': [FAIL],
'built-ins/Temporal/PlainDateTime/prototype/toString/roundingmode-invalid-string': [FAIL],
'built-ins/Temporal/PlainDateTime/prototype/toString/roundingmode-undefined': [FAIL],
'built-ins/Temporal/PlainDateTime/prototype/toString/roundingmode-wrong-type': [FAIL],
'built-ins/Temporal/PlainDateTime/prototype/toString/smallestunit-invalid-string': [FAIL],
'built-ins/Temporal/PlainDateTime/prototype/toString/smallestunit-plurals-accepted': [FAIL],
'built-ins/Temporal/PlainDateTime/prototype/toString/smallestunit-undefined': [FAIL],
'built-ins/Temporal/PlainDateTime/prototype/toString/smallestunit-valid-units': [FAIL],
'built-ins/Temporal/PlainDateTime/prototype/toString/smallestunit-wrong-type': [FAIL],
'built-ins/Temporal/PlainDateTime/prototype/toString/year-format': [FAIL],
'built-ins/Temporal/PlainDateTime/prototype/toZonedDateTime/balance-negative-time-units': [FAIL],
'built-ins/Temporal/PlainDateTime/prototype/toZonedDateTime/disambiguation-undefined': [FAIL],
'built-ins/Temporal/PlainDateTime/prototype/toZonedDateTime/options-undefined': [FAIL],
@ -1604,7 +1585,6 @@
'built-ins/Temporal/PlainDateTime/prototype/add/negative-duration': [FAIL],
'built-ins/Temporal/PlainDateTime/prototype/subtract/argument-duration': [FAIL],
'built-ins/Temporal/PlainDateTime/prototype/subtract/hour-overflow': [FAIL],
'built-ins/Temporal/PlainDateTime/prototype/toString/basic': [FAIL],
'built-ins/Temporal/PlainDateTime/prototype/until/casts-argument': [FAIL],
'built-ins/Temporal/PlainDateTime/prototype/until/different-calendars-throws': [FAIL],
'built-ins/Temporal/PlainDateTime/prototype/until/inverse': [FAIL],
@ -1681,16 +1661,8 @@
'built-ins/Temporal/PlainDateTime/prototype/since/round-relative-to-receiver': [FAIL],
'built-ins/Temporal/PlainDateTime/prototype/since/subseconds': [FAIL],
'built-ins/Temporal/PlainDateTime/prototype/since/weeks-months-mutually-exclusive': [FAIL],
'built-ins/Temporal/PlainDateTime/prototype/toString/fractionalseconddigits-auto': [FAIL],
'built-ins/Temporal/PlainDateTime/prototype/toString/fractionalseconddigits-number': [FAIL],
'built-ins/Temporal/PlainDateTime/prototype/toString/options-wrong-type': [FAIL],
'built-ins/Temporal/PlainDateTime/prototype/toString/rounding-cross-midnight': [FAIL],
'built-ins/Temporal/PlainDateTime/prototype/toString/rounding-direction': [FAIL],
'built-ins/Temporal/PlainDateTime/prototype/toString/roundingmode-ceil': [FAIL],
'built-ins/Temporal/PlainDateTime/prototype/toString/roundingmode-floor': [FAIL],
'built-ins/Temporal/PlainDateTime/prototype/toString/roundingmode-halfExpand': [FAIL],
'built-ins/Temporal/PlainDateTime/prototype/toString/roundingmode-trunc': [FAIL],
'built-ins/Temporal/PlainDateTime/prototype/toString/smallestunit-fractionalseconddigits': [FAIL],
'built-ins/Temporal/PlainDateTime/prototype/until/argument-object': [FAIL],
'built-ins/Temporal/PlainDateTime/prototype/until/argument-string': [FAIL],
'built-ins/Temporal/PlainDateTime/prototype/until/options-empty': [FAIL],