Skip to content

Rebase: ISO Date-Time Record refactoring #316

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 25 commits into from
Mar 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
837338d
Editorial: Remove dead code from PlainDateTime.with
ptomato Oct 7, 2024
d2b678a
Editorial: Use ISO Date(-Time) Record in ISODate(Time)WithinLimits
ptomato Oct 5, 2024
10326c6
Editorial: Use ISO Date Record in CompareISODate and ISODateSurpasses
ptomato Oct 5, 2024
4d9556b
Editorial: Use Time Record in DifferenceTime
ptomato Oct 5, 2024
57d4539
Editorial: Use Time Record in TemporalTimeToString, and rename
ptomato Oct 5, 2024
ee82c39
Editorial: Use Time Record in CompareTemporalTime, and rename
ptomato Oct 5, 2024
45d32d4
Editorial: Use Time Record in AddTime
ptomato Oct 5, 2024
cb78543
Editorial: Use ISO Date-Time Record in RoundISODateTime
ptomato Oct 5, 2024
5921a05
Editorial: Use Time Record in RoundTime
ptomato Oct 5, 2024
d0924d6
Editorial: Use ISO Date-Time Record in TemporalDateTimeToString, and …
ptomato Oct 6, 2024
7bf554c
Editorial: Compose ISO Date-Time Record of ISO Date Record and Time R…
ptomato Oct 6, 2024
c4413a6
Editorial: Use ISO Date-Time Record in CompareISODateTime
ptomato Oct 6, 2024
0dc44f6
Editorial: Use ISO Date-Time Record in DifferenceISODateTime
ptomato Oct 6, 2024
48b8529
Editorial: Use ISO Date-Time Record in DifferencePlainDateTimeWithRou…
ptomato Oct 6, 2024
0953bd5
Editorial: Use ISO Date-Time Record in DifferencePlainDateTimeWithTotal
ptomato Oct 6, 2024
56af575
Editorial: Use ISO Date Record in InterpretISODateTimeOffset
ptomato Oct 6, 2024
52b6db4
Editorial: Use ISO Date Record in ISOYearMonthWithinLimits
ptomato Oct 6, 2024
1c9e45b
Editorial: Use ISO Date-Time Record in GetUTCEpochNanoseconds
ptomato Oct 6, 2024
259cd84
Editorial: Use ISO Date-Time Record in GetNamedTimeZoneEpochNanoseconds
ptomato Oct 6, 2024
5e714a9
Editorial: Add MidnightTimeRecord and NoonTimeRecord
ptomato Oct 6, 2024
4237a1f
Editorial: Use records in internal slots of Temporal objects
ptomato Oct 6, 2024
5d2cc49
Editorial: Inline ConstrainTime into RegulateTime
ptomato Oct 7, 2024
0a71d06
Editorial: Return ISO Date-Time Record from SystemDateTime
ptomato Oct 7, 2024
ad8ef09
Editorial: Remove TemporalObjectToFields
ptomato Oct 7, 2024
c50e0ba
Editorial: Return Time Record from ToTemporalTimeOrMidnight, and rename
ptomato Oct 7, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions lib/calendar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,15 @@ function calendarDateWeekOfYear(
return { week: woy, year: yow };
}

function ISODateSurpasses(sign: -1 | 0 | 1, y1: number, m1: number, d1: number, y2: number, m2: number, d2: number) {
const cmp = ES.CompareISODate(y1, m1, d1, y2, m2, d2);
return sign * cmp === 1;
function ISODateSurpasses(sign: -1 | 0 | 1, y1: number, m1: number, d1: number, isoDate2: ISODate) {
if (y1 !== isoDate2.year) {
if (sign * (y1 - isoDate2.year) > 0) return true;
} else if (m1 !== isoDate2.month) {
if (sign * (m1 - isoDate2.month) > 0) return true;
} else if (d1 !== isoDate2.day) {
if (sign * (d1 - isoDate2.day) > 0) return true;
}
return false;
}

interface CalendarDateRecord {
Expand Down Expand Up @@ -260,7 +266,7 @@ impl['iso8601'] = {
return ES.BalanceISODate(year, month, day);
},
dateUntil(one, two, largestUnit) {
const sign = -ES.CompareISODate(one.year, one.month, one.day, two.year, two.month, two.day);
const sign = -ES.CompareISODate(one, two);
if (sign === 0) return { years: 0, months: 0, weeks: 0, days: 0 };
ES.uncheckedAssertNarrowedType<-1 | 1>(sign, "the - operator's return type is number");

Expand All @@ -273,15 +279,15 @@ impl['iso8601'] = {
let candidateYears = two.year - one.year;
if (candidateYears !== 0) candidateYears -= sign;
// loops at most twice
while (!ISODateSurpasses(sign, one.year + candidateYears, one.month, one.day, two.year, two.month, two.day)) {
while (!ISODateSurpasses(sign, one.year + candidateYears, one.month, one.day, two)) {
years = candidateYears;
candidateYears += sign;
}

let candidateMonths = sign;
intermediate = ES.BalanceISOYearMonth(one.year + years, one.month + candidateMonths);
// loops at most 12 times
while (!ISODateSurpasses(sign, intermediate.year, intermediate.month, one.day, two.year, two.month, two.day)) {
while (!ISODateSurpasses(sign, intermediate.year, intermediate.month, one.day, two)) {
months = candidateMonths;
candidateMonths += sign;
intermediate = ES.BalanceISOYearMonth(intermediate.year, intermediate.month + sign);
Expand Down Expand Up @@ -2336,8 +2342,10 @@ class NonIsoCalendar implements CalendarImpl {
const added = this.helper.addCalendar(calendarDate, { years, months, weeks, days }, overflow, cache);
const isoAdded = this.helper.calendarToIsoDate(added, 'constrain', cache);
// The new object's cache starts with the cache of the old object
const newCache = new OneObjectCache(cache);
newCache.setObject(isoAdded);
if (!OneObjectCache.getCacheForObject(isoAdded)) {
const newCache = new OneObjectCache(cache);
newCache.setObject(isoAdded);
}
return isoAdded;
}
dateUntil(one: ISODate, two: ISODate, largestUnit: Temporal.DateUnit) {
Expand Down
56 changes: 12 additions & 44 deletions lib/duration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
EPOCHNANOSECONDS,
CreateSlots,
GetSlot,
ISO_DATE,
SetSlot,
TIME_ZONE
} from './slots';
Expand Down Expand Up @@ -250,33 +251,19 @@ export class Duration implements Temporal.Duration {

if (plainRelativeTo) {
let duration = ES.NormalizeDurationWith24HourDays(this);
const targetTime = ES.AddTime(0, 0, 0, 0, 0, 0, duration.norm);
const targetTime = ES.AddTime(ES.MidnightTimeRecord(), duration.norm);

// Delegate the date part addition to the calendar
const isoRelativeToDate = ES.TemporalObjectToISODateRecord(plainRelativeTo);
const isoRelativeToDate = GetSlot(plainRelativeTo, ISO_DATE);
const calendar = GetSlot(plainRelativeTo, CALENDAR);
const dateDuration = ES.AdjustDateDurationRecord(duration.date, targetTime.deltaDays);
const targetDate = ES.CalendarDateAdd(calendar, isoRelativeToDate, dateDuration, 'constrain');

const isoDateTime = ES.CombineISODateAndTimeRecord(isoRelativeToDate, ES.MidnightTimeRecord());
const targetDateTime = ES.CombineISODateAndTimeRecord(targetDate, targetTime);
duration = ES.DifferencePlainDateTimeWithRounding(
isoRelativeToDate.year,
isoRelativeToDate.month,
isoRelativeToDate.day,
0,
0,
0,
0,
0,
0,
targetDate.year,
targetDate.month,
targetDate.day,
targetTime.hour,
targetTime.minute,
targetTime.second,
targetTime.millisecond,
targetTime.microsecond,
targetTime.nanosecond,
isoDateTime,
targetDateTime,
calendar,
largestUnit,
roundingIncrement,
Expand Down Expand Up @@ -320,36 +307,17 @@ export class Duration implements Temporal.Duration {

if (plainRelativeTo) {
const duration = ES.NormalizeDurationWith24HourDays(this);
let targetTime = ES.AddTime(0, 0, 0, 0, 0, 0, duration.norm);
let targetTime = ES.AddTime(ES.MidnightTimeRecord(), duration.norm);

// Delegate the date part addition to the calendar
const isoRelativeToDate = ES.TemporalObjectToISODateRecord(plainRelativeTo);
const isoRelativeToDate = GetSlot(plainRelativeTo, ISO_DATE);
const calendar = GetSlot(plainRelativeTo, CALENDAR);
const dateDuration = ES.AdjustDateDurationRecord(duration.date, targetTime.deltaDays);
const targetDate = ES.CalendarDateAdd(calendar, isoRelativeToDate, dateDuration, 'constrain');

return ES.DifferencePlainDateTimeWithTotal(
isoRelativeToDate.year,
isoRelativeToDate.month,
isoRelativeToDate.day,
0,
0,
0,
0,
0,
0,
targetDate.year,
targetDate.month,
targetDate.day,
targetTime.hour,
targetTime.minute,
targetTime.second,
targetTime.millisecond,
targetTime.microsecond,
targetTime.nanosecond,
calendar,
unit
);
const isoDateTime = ES.CombineISODateAndTimeRecord(isoRelativeToDate, ES.MidnightTimeRecord());
const targetDateTime = ES.CombineISODateAndTimeRecord(targetDate, targetTime);
return ES.DifferencePlainDateTimeWithTotal(isoDateTime, targetDateTime, calendar, unit);
}

// No reference date to calculate difference relative to
Expand Down
Loading