summaryrefslogtreecommitdiffstats
path: root/js/src/builtin/temporal/Duration.h
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-12 05:43:14 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-12 05:43:14 +0000
commit8dd16259287f58f9273002717ec4d27e97127719 (patch)
tree3863e62a53829a84037444beab3abd4ed9dfc7d0 /js/src/builtin/temporal/Duration.h
parentReleasing progress-linux version 126.0.1-1~progress7.99u1. (diff)
downloadfirefox-8dd16259287f58f9273002717ec4d27e97127719.tar.xz
firefox-8dd16259287f58f9273002717ec4d27e97127719.zip
Merging upstream version 127.0.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'js/src/builtin/temporal/Duration.h')
-rw-r--r--js/src/builtin/temporal/Duration.h225
1 files changed, 191 insertions, 34 deletions
diff --git a/js/src/builtin/temporal/Duration.h b/js/src/builtin/temporal/Duration.h
index 47708458f4..e1aea4d1d4 100644
--- a/js/src/builtin/temporal/Duration.h
+++ b/js/src/builtin/temporal/Duration.h
@@ -89,11 +89,31 @@ enum class TemporalUnit;
int32_t DurationSign(const Duration& duration);
/**
+ * DurationSign ( years, months, weeks, days, hours, minutes, seconds,
+ * milliseconds, microseconds, nanoseconds )
+ */
+int32_t DurationSign(const DateDuration& duration);
+
+/**
* IsValidDuration ( years, months, weeks, days, hours, minutes, seconds,
* milliseconds, microseconds, nanoseconds )
*/
bool IsValidDuration(const Duration& duration);
+#ifdef DEBUG
+/**
+ * IsValidDuration ( years, months, weeks, days, hours, minutes, seconds,
+ * milliseconds, microseconds, nanoseconds )
+ */
+bool IsValidDuration(const DateDuration& duration);
+
+/**
+ * IsValidDuration ( years, months, weeks, days, hours, minutes, seconds,
+ * milliseconds, microseconds, nanoseconds )
+ */
+bool IsValidDuration(const NormalizedDuration& duration);
+#endif
+
/**
* IsValidDuration ( years, months, weeks, days, hours, minutes, seconds,
* milliseconds, microseconds, nanoseconds )
@@ -101,6 +121,135 @@ bool IsValidDuration(const Duration& duration);
bool ThrowIfInvalidDuration(JSContext* cx, const Duration& duration);
/**
+ * IsValidDuration ( years, months, weeks, days, hours, minutes, seconds,
+ * milliseconds, microseconds, nanoseconds )
+ */
+bool ThrowIfInvalidDuration(JSContext* cx, const DateDuration& duration);
+
+/**
+ * IsValidDuration ( years, months, weeks, days, hours, minutes, seconds,
+ * milliseconds, microseconds, nanoseconds )
+ */
+inline bool IsValidNormalizedTimeDuration(
+ const NormalizedTimeDuration& duration) {
+ MOZ_ASSERT(0 <= duration.nanoseconds && duration.nanoseconds <= 999'999'999);
+
+ // Step 4.
+ //
+ // The absolute value of the seconds part of normalized time duration must be
+ // less-or-equal to `2**53 - 1` and the nanoseconds part must be less or equal
+ // to `999'999'999`.
+ return NormalizedTimeDuration::min() <= duration &&
+ duration <= NormalizedTimeDuration::max();
+}
+
+/**
+ * NormalizeTimeDuration ( hours, minutes, seconds, milliseconds, microseconds,
+ * nanoseconds )
+ */
+NormalizedTimeDuration NormalizeTimeDuration(int32_t hours, int32_t minutes,
+ int32_t seconds,
+ int32_t milliseconds,
+ int32_t microseconds,
+ int32_t nanoseconds);
+
+/**
+ * NormalizeTimeDuration ( hours, minutes, seconds, milliseconds, microseconds,
+ * nanoseconds )
+ */
+NormalizedTimeDuration NormalizeTimeDuration(const Duration& duration);
+
+/**
+ * CompareNormalizedTimeDuration ( one, two )
+ */
+inline int32_t CompareNormalizedTimeDuration(
+ const NormalizedTimeDuration& one, const NormalizedTimeDuration& two) {
+ MOZ_ASSERT(IsValidNormalizedTimeDuration(one));
+ MOZ_ASSERT(IsValidNormalizedTimeDuration(two));
+
+ // Step 1.
+ if (one > two) {
+ return 1;
+ }
+
+ // Step 2.
+ if (one < two) {
+ return -1;
+ }
+
+ // Step 3.
+ return 0;
+}
+
+/**
+ * NormalizedTimeDurationSign ( d )
+ */
+inline int32_t NormalizedTimeDurationSign(const NormalizedTimeDuration& d) {
+ MOZ_ASSERT(IsValidNormalizedTimeDuration(d));
+
+ // Steps 1-3.
+ return CompareNormalizedTimeDuration(d, NormalizedTimeDuration{});
+}
+
+/**
+ * Add24HourDaysToNormalizedTimeDuration ( d, days )
+ */
+bool Add24HourDaysToNormalizedTimeDuration(JSContext* cx,
+ const NormalizedTimeDuration& d,
+ int64_t days,
+ NormalizedTimeDuration* result);
+
+/**
+ * CreateNormalizedDurationRecord ( years, months, weeks, days, norm )
+ */
+inline NormalizedDuration CreateNormalizedDurationRecord(
+ const DateDuration& date, const NormalizedTimeDuration& time) {
+ MOZ_ASSERT(IsValidDuration(date));
+ MOZ_ASSERT(IsValidNormalizedTimeDuration(time));
+#ifdef DEBUG
+ int64_t dateValues = date.years | date.months | date.weeks | date.days;
+ int32_t dateSign = dateValues ? dateValues < 0 ? -1 : 1 : 0;
+ int32_t timeSign = NormalizedTimeDurationSign(time);
+ MOZ_ASSERT((dateSign * timeSign) >= 0);
+#endif
+
+ return {date, time};
+}
+
+/**
+ * CreateNormalizedDurationRecord ( years, months, weeks, days, norm )
+ */
+inline NormalizedDuration CreateNormalizedDurationRecord(
+ const Duration& duration) {
+ return CreateNormalizedDurationRecord(duration.toDateDuration(),
+ NormalizeTimeDuration(duration));
+}
+
+/**
+ * CombineDateAndNormalizedTimeDuration ( dateDurationRecord, norm )
+ */
+bool CombineDateAndNormalizedTimeDuration(JSContext* cx,
+ const DateDuration& date,
+ const NormalizedTimeDuration& time,
+ NormalizedDuration* result);
+
+/**
+ * CreateNormalizedDurationRecord ( years, months, weeks, days, norm )
+ */
+inline bool CreateNormalizedDurationRecord(JSContext* cx,
+ const DateDuration& date,
+ const NormalizedTimeDuration& time,
+ NormalizedDuration* result) {
+ return CombineDateAndNormalizedTimeDuration(cx, date, time, result);
+}
+
+/**
+ * NormalizedTimeDurationFromEpochNanosecondsDifference ( one, two )
+ */
+NormalizedTimeDuration NormalizedTimeDurationFromEpochNanosecondsDifference(
+ const Instant& one, const Instant& two);
+
+/**
* CreateTemporalDuration ( years, months, weeks, days, hours, minutes, seconds,
* milliseconds, microseconds, nanoseconds [ , newTarget ] )
*/
@@ -126,17 +275,15 @@ bool ToTemporalDurationRecord(JSContext* cx,
Duration* result);
/**
- * BalanceTimeDuration ( days, hours, minutes, seconds, milliseconds,
- * microseconds, nanoseconds, largestUnit )
+ * BalanceTimeDuration ( norm, largestUnit )
*/
-bool BalanceTimeDuration(JSContext* cx, const Duration& duration,
- TemporalUnit largestUnit, TimeDuration* result);
+TimeDuration BalanceTimeDuration(const NormalizedTimeDuration& duration,
+ TemporalUnit largestUnit);
/**
- * BalanceTimeDuration ( days, hours, minutes, seconds, milliseconds,
- * microseconds, nanoseconds, largestUnit )
+ * BalanceTimeDuration ( norm, largestUnit )
*/
-bool BalanceTimeDuration(JSContext* cx, const InstantSpan& nanoseconds,
+bool BalanceTimeDuration(JSContext* cx, const NormalizedTimeDuration& duration,
TemporalUnit largestUnit, TimeDuration* result);
/**
@@ -144,61 +291,71 @@ bool BalanceTimeDuration(JSContext* cx, const InstantSpan& nanoseconds,
* smallestUnit, plainRelativeTo, calendarRec )
*/
bool BalanceDateDurationRelative(
- JSContext* cx, const Duration& duration, TemporalUnit largestUnit,
+ JSContext* cx, const DateDuration& duration, TemporalUnit largestUnit,
TemporalUnit smallestUnit,
JS::Handle<Wrapped<PlainDateObject*>> plainRelativeTo,
JS::Handle<CalendarRecord> calendar, DateDuration* result);
/**
- * AdjustRoundedDurationDays ( years, months, weeks, days, hours, minutes,
- * seconds, milliseconds, microseconds, nanoseconds, increment, unit,
- * roundingMode, zonedRelativeTo, calendarRec, timeZoneRec,
+ * AdjustRoundedDurationDays ( years, months, weeks, days, norm, increment,
+ * unit, roundingMode, zonedRelativeTo, calendarRec, timeZoneRec,
* precalculatedPlainDateTime )
*/
-bool AdjustRoundedDurationDays(JSContext* cx, const Duration& duration,
+bool AdjustRoundedDurationDays(JSContext* cx,
+ const NormalizedDuration& duration,
Increment increment, TemporalUnit unit,
TemporalRoundingMode roundingMode,
JS::Handle<ZonedDateTime> relativeTo,
JS::Handle<CalendarRecord> calendar,
JS::Handle<TimeZoneRecord> timeZone,
const PlainDateTime& precalculatedPlainDateTime,
- Duration* result);
+ NormalizedDuration* result);
/**
- * RoundDuration ( years, months, weeks, days, hours, minutes, seconds,
- * milliseconds, microseconds, nanoseconds, increment, unit, roundingMode [ ,
- * plainRelativeTo [ , calendarRec [ , zonedRelativeTo [ , timeZoneRec [ ,
- * precalculatedPlainDateTime ] ] ] ] ] )
+ * RoundDuration ( years, months, weeks, days, norm, increment, unit,
+ * roundingMode [ , plainRelativeTo [ , calendarRec [ , zonedRelativeTo [ ,
+ * timeZoneRec [ , precalculatedPlainDateTime ] ] ] ] ] )
*/
-bool RoundDuration(JSContext* cx, const Duration& duration, Increment increment,
- TemporalUnit unit, TemporalRoundingMode roundingMode,
- Duration* result);
+NormalizedTimeDuration RoundDuration(const NormalizedTimeDuration& duration,
+ Increment increment, TemporalUnit unit,
+ TemporalRoundingMode roundingMode);
/**
- * RoundDuration ( years, months, weeks, days, hours, minutes, seconds,
- * milliseconds, microseconds, nanoseconds, increment, unit, roundingMode [ ,
- * plainRelativeTo [ , calendarRec [ , zonedRelativeTo [ , timeZoneRec [ ,
- * precalculatedPlainDateTime ] ] ] ] ] )
+ * RoundDuration ( years, months, weeks, days, norm, increment, unit,
+ * roundingMode [ , plainRelativeTo [ , calendarRec [ , zonedRelativeTo [ ,
+ * timeZoneRec [ , precalculatedPlainDateTime ] ] ] ] ] )
*/
-bool RoundDuration(JSContext* cx, const Duration& duration, Increment increment,
- TemporalUnit unit, TemporalRoundingMode roundingMode,
+bool RoundDuration(JSContext* cx, const NormalizedTimeDuration& duration,
+ Increment increment, TemporalUnit unit,
+ TemporalRoundingMode roundingMode,
+ NormalizedTimeDuration* result);
+
+/**
+ * RoundDuration ( years, months, weeks, days, norm, increment, unit,
+ * roundingMode [ , plainRelativeTo [ , calendarRec [ , zonedRelativeTo [ ,
+ * timeZoneRec [ , precalculatedPlainDateTime ] ] ] ] ] )
+ */
+bool RoundDuration(JSContext* cx, const NormalizedDuration& duration,
+ Increment increment, TemporalUnit unit,
+ TemporalRoundingMode roundingMode,
JS::Handle<Wrapped<PlainDateObject*>> plainRelativeTo,
- JS::Handle<CalendarRecord> calendar, Duration* result);
+ JS::Handle<CalendarRecord> calendar,
+ NormalizedDuration* result);
/**
- * RoundDuration ( years, months, weeks, days, hours, minutes, seconds,
- * milliseconds, microseconds, nanoseconds, increment, unit, roundingMode [ ,
- * plainRelativeTo [ , calendarRec [ , zonedRelativeTo [ , timeZoneRec [ ,
- * precalculatedPlainDateTime ] ] ] ] ] )
+ * RoundDuration ( years, months, weeks, days, norm, increment, unit,
+ * roundingMode [ , plainRelativeTo [ , calendarRec [ , zonedRelativeTo [ ,
+ * timeZoneRec [ , precalculatedPlainDateTime ] ] ] ] ] )
*/
-bool RoundDuration(JSContext* cx, const Duration& duration, Increment increment,
- TemporalUnit unit, TemporalRoundingMode roundingMode,
+bool RoundDuration(JSContext* cx, const NormalizedDuration& duration,
+ Increment increment, TemporalUnit unit,
+ TemporalRoundingMode roundingMode,
JS::Handle<PlainDateObject*> plainRelativeTo,
JS::Handle<CalendarRecord> calendar,
JS::Handle<ZonedDateTime> zonedRelativeTo,
JS::Handle<TimeZoneRecord> timeZone,
const PlainDateTime& precalculatedPlainDateTime,
- Duration* result);
+ NormalizedDuration* result);
/**
* DaysUntil ( earlier, later )