Bug 1874684 - Part 14: Return DateDuration from DifferenceDate. r=sfink

Similar to the previous part, prefer `DateDuration` to avoid unnecessary
`int64_t` -> `double` -> `int64_t` conversions.

Differential Revision: https://phabricator.services.mozilla.com/D198548
This commit is contained in:
André Bargull 2024-04-15 18:27:22 +00:00
Родитель e9325c941d
Коммит 8c9d45d621
4 изменённых файлов: 36 добавлений и 29 удалений

Просмотреть файл

@ -2162,7 +2162,7 @@ static bool AddDuration(JSContext* cx, const Duration& one, const Duration& two,
auto dateLargestUnit = std::min(TemporalUnit::Day, largestUnit);
// Steps 7.i-k.
Duration dateDifference;
DateDuration dateDifference;
if (!DifferenceDate(cx, calendar, plainRelativeTo, end, dateLargestUnit,
&dateDifference)) {
return false;
@ -2193,11 +2193,11 @@ static bool AddDuration(JSContext* cx, const Duration& one, const Duration& two,
// Steps 7.q.
*result = {
dateDifference.years, dateDifference.months,
dateDifference.weeks, double(balanced.days),
double(balanced.hours), double(balanced.minutes),
double(balanced.seconds), double(balanced.milliseconds),
balanced.microseconds, balanced.nanoseconds,
double(dateDifference.years), double(dateDifference.months),
double(dateDifference.weeks), double(balanced.days),
double(balanced.hours), double(balanced.minutes),
double(balanced.seconds), double(balanced.milliseconds),
balanced.microseconds, balanced.nanoseconds,
};
MOZ_ASSERT(IsValidDuration(*result));
return true;
@ -3536,14 +3536,14 @@ static bool RoundDurationYear(JSContext* cx, const NormalizedDuration& duration,
}
// Steps 10.j-l.
Duration timePassed;
DateDuration timePassed;
if (!DifferenceDate(cx, calendar, newRelativeTo, wholeDaysLater,
TemporalUnit::Year, &timePassed)) {
return false;
}
// Step 10.m.
int64_t yearsPassed = int64_t(timePassed.years);
int64_t yearsPassed = timePassed.years;
// Step 10.n.
years += yearsPassed;
@ -3678,14 +3678,14 @@ static bool RoundDurationMonth(JSContext* cx,
}
// Steps 11.j-l.
Duration timePassed;
DateDuration timePassed;
if (!DifferenceDate(cx, calendar, newRelativeTo, wholeDaysLater,
TemporalUnit::Month, &timePassed)) {
return false;
}
// Step 11.m.
int64_t monthsPassed = int64_t(timePassed.months);
int64_t monthsPassed = timePassed.months;
// Step 11.n.
months += monthsPassed;
@ -3788,14 +3788,14 @@ static bool RoundDurationWeek(JSContext* cx, const NormalizedDuration& duration,
}
// Steps 12.c-e.
Duration timePassed;
DateDuration timePassed;
if (!DifferenceDate(cx, calendar, dateRelativeTo, wholeDaysLater,
TemporalUnit::Week, &timePassed)) {
return false;
}
// Step 12.f.
int64_t weeksPassed = int64_t(timePassed.weeks);
int64_t weeksPassed = timePassed.weeks;
// Step 12.g.
weeks += weeksPassed;

Просмотреть файл

@ -1162,7 +1162,7 @@ bool js::temporal::DifferenceDate(JSContext* cx,
Handle<Wrapped<PlainDateObject*>> one,
Handle<Wrapped<PlainDateObject*>> two,
Handle<PlainObject*> options,
Duration* result) {
DateDuration* result) {
auto* unwrappedOne = one.unwrap(cx);
if (!unwrappedOne) {
return false;
@ -1207,13 +1207,18 @@ bool js::temporal::DifferenceDate(JSContext* cx,
int32_t days = DaysUntil(oneDate, twoDate);
// Step 6.b.
*result = {0, 0, 0, double(days)};
*result = {0, 0, 0, days};
return true;
}
}
// Step 7.
return CalendarDateUntil(cx, calendar, one, two, options, result);
Duration duration;
if (!CalendarDateUntil(cx, calendar, one, two, options, &duration)) {
return false;
}
*result = duration.toDateDuration();
return true;
}
/**
@ -1223,7 +1228,8 @@ bool js::temporal::DifferenceDate(JSContext* cx,
Handle<CalendarRecord> calendar,
Handle<Wrapped<PlainDateObject*>> one,
Handle<Wrapped<PlainDateObject*>> two,
TemporalUnit largestUnit, Duration* result) {
TemporalUnit largestUnit,
DateDuration* result) {
auto* unwrappedOne = one.unwrap(cx);
if (!unwrappedOne) {
return false;
@ -1250,12 +1256,17 @@ bool js::temporal::DifferenceDate(JSContext* cx,
int32_t days = DaysUntil(oneDate, twoDate);
// Step 6.b.
*result = {0, 0, 0, double(days)};
*result = {0, 0, 0, days};
return true;
}
// Step 7.
return CalendarDateUntil(cx, calendar, one, two, largestUnit, result);
Duration duration;
if (!CalendarDateUntil(cx, calendar, one, two, largestUnit, &duration)) {
return false;
}
*result = duration.toDateDuration();
return true;
}
/**
@ -1596,20 +1607,16 @@ static bool DifferenceTemporalPlainDate(JSContext* cx,
}
// Step 9.
Duration duration;
if (!DifferenceDate(cx, calendar, temporalDate, other, resolvedOptions,
&duration)) {
&difference)) {
return false;
}
difference = duration.toDateDuration();
} else {
// Steps 8-9.
Duration duration;
if (!DifferenceDate(cx, calendar, temporalDate, other, settings.largestUnit,
&duration)) {
&difference)) {
return false;
}
difference = duration.toDateDuration();
}
// Step 10.

Просмотреть файл

@ -211,7 +211,7 @@ DateDuration DifferenceISODate(const PlainDate& start, const PlainDate& end,
bool DifferenceDate(JSContext* cx, JS::Handle<CalendarRecord> calendar,
JS::Handle<Wrapped<PlainDateObject*>> one,
JS::Handle<Wrapped<PlainDateObject*>> two,
JS::Handle<PlainObject*> options, Duration* result);
JS::Handle<PlainObject*> options, DateDuration* result);
/**
* DifferenceDate ( calendarRec, one, two, options )
@ -219,7 +219,7 @@ bool DifferenceDate(JSContext* cx, JS::Handle<CalendarRecord> calendar,
bool DifferenceDate(JSContext* cx, JS::Handle<CalendarRecord> calendar,
JS::Handle<Wrapped<PlainDateObject*>> one,
JS::Handle<Wrapped<PlainDateObject*>> two,
TemporalUnit largestUnit, Duration* result);
TemporalUnit largestUnit, DateDuration* result);
/**
* CompareISODate ( y1, m1, d1, y2, m2, d2 )

Просмотреть файл

@ -843,7 +843,7 @@ static bool DifferenceISODateTime(JSContext* cx, const PlainDateTime& one,
// Step 11.
auto dateLargestUnit = std::min(TemporalUnit::Day, largestUnit);
Duration dateDifference;
DateDuration dateDifference;
if (maybeOptions) {
// FIXME: spec issue - this copy is no longer needed, all callers have
// already copied the user input object.
@ -878,8 +878,8 @@ static bool DifferenceISODateTime(JSContext* cx, const PlainDateTime& one,
}
// Step 15.
return CreateNormalizedDurationRecord(cx, dateDifference.toDateDuration(),
timeDuration, result);
return CreateNormalizedDurationRecord(cx, dateDifference, timeDuration,
result);
}
/**