Skip to content

Commit

Permalink
FULLCALENDAR- adjustments for Duration::round/total not correctly usi…
Browse files Browse the repository at this point in the history
…ng DST for bounding-window
  • Loading branch information
arshaw committed Apr 13, 2024
1 parent e349fca commit fd9a0d7
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ const timeZone = TemporalHelpers.springForwardFallBackTimeZone();
TemporalHelpers.assertDuration(duration.round({ smallestUnit: "months", relativeTo }),
0, 2, 0, 0, 0, 0, 0, 0, 0, 0,
"1 month 15 days 12 hours should be exactly 1.5 months, which rounds up to 2 months");

// TODO: did naive modification but needs more salient tests
TemporalHelpers.assertDuration(duration.round({ smallestUnit: "months", roundingMode: 'halfTrunc', relativeTo }),
0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
0, 2, 0, 0, 0, 0, 0, 0, 0, 0,
"1 month 15 days 12 hours should be exactly 1.5 months, which rounds down to 1 month");
}

Expand All @@ -36,10 +38,61 @@ const timeZone = TemporalHelpers.springForwardFallBackTimeZone();
951991200_000_000_000n /* = 2000-03-02T10Z */,
timeZone); /* = 2000-03-02T02-08 in local time */

// TODO: did naive modification but needs more salient tests
TemporalHelpers.assertDuration(duration.round({ smallestUnit: "months", relativeTo }),
0, 2, 0, 0, 0, 0, 0, 0, 0, 0,
0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
"1 month 15 days should be exactly 1.5 months, which rounds up to 2 months");

TemporalHelpers.assertDuration(duration.round({ smallestUnit: "months", roundingMode: 'halfTrunc', relativeTo }),
0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
"1 month 15 days should be exactly 1.5 months, which rounds down to 1 month");
}

// Day rounding
// DST spring-forward hour skipped at 2000-04-02T02:00 (23 hour day)
// 11.5 hours is 0.5
{
const duration = new Temporal.Duration(0, 0, 0, 0, 11, 30);
const instant = timeZone.getPossibleInstantsFor(Temporal.PlainDateTime.from('2000-04-02T00:00:00'))[0]
const relativeTo = instant.toZonedDateTimeISO(timeZone)

TemporalHelpers.assertDuration(
duration.round({ relativeTo, smallestUnit: "days" }),
0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
);

TemporalHelpers.assertDuration(
duration.round({ relativeTo, smallestUnit: "days", roundingMode: 'halfTrunc' }),
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
);
}

// Hour rounding,
// BUT should still bubble-up units to days if hours-in-day exceeded (like AdjustRoundedDurationDays)
// DST spring-forward hour skipped at 2000-04-02T02:00 (23 hour day)
// 13hrs with 12hr ceil rounding should go up to 24hrs (exceeds current day), then 12hrs into NEXT day
{
const duration = new Temporal.Duration(0, 0, 0, 0, 13);
const instant = timeZone.getPossibleInstantsFor(Temporal.PlainDateTime.from('2000-04-02T00:00:00'))[0]
const relativeTo = instant.toZonedDateTimeISO(timeZone)

TemporalHelpers.assertDuration(
duration.round({ relativeTo, smallestUnit: "hours", roundingIncrement: 12, roundingMode: "ceil" }),
0, 0, 0, 1, 12, 0, 0, 0, 0, 0,
);
}

// Hour rounding, WITH MONTH ADDITION
// BUT should still bubble-up units to days if hours-in-day exceeded (like AdjustRoundedDurationDays)
// DST spring-forward hour skipped at 2000-04-02T02:00 (23 hour day)
// 13hrs with 12hr ceil rounding should go up to 24hrs (exceeds current day), then 12hrs into NEXT day
{
const duration = new Temporal.Duration(0, 1, 0, 0, 13);
const instant = timeZone.getPossibleInstantsFor(Temporal.PlainDateTime.from('2000-03-02T00:00:00'))[0]
const relativeTo = instant.toZonedDateTimeISO(timeZone)

TemporalHelpers.assertDuration(
duration.round({ relativeTo, smallestUnit: "hours", roundingIncrement: 12, roundingMode: "ceil" }),
0, 1, 0, 1, 12, 0, 0, 0, 0, 0,
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,16 @@ const timeZone = TemporalHelpers.springForwardFallBackTimeZone();
950868000_000_000_000n /* = 2000-02-18T10Z */,
timeZone); /* = 2000-02-18T02-08 in local time */

assert.sameValue(duration.total({ unit: "months", relativeTo }), 1.5,
// TODO: make this test nicer
const month1 = relativeTo.add({ months: 1 })
const middle = relativeTo.add(duration)
const month2 = relativeTo.add({ months: 2 })
const answer = 1 + (
Number(middle.epochNanoseconds - month1.epochNanoseconds) /
Number(month2.epochNanoseconds - month1.epochNanoseconds)
)

assert.sameValue(duration.total({ unit: "months", relativeTo }), answer,
"1 month 15 days 12 hours should be exactly 1.5 months");
}

Expand All @@ -32,6 +41,15 @@ const timeZone = TemporalHelpers.springForwardFallBackTimeZone();
951991200_000_000_000n /* = 2000-03-02T10Z */,
timeZone); /* = 2000-03-02T02-08 in local time */

assert.sameValue(duration.total({ unit: "months", relativeTo }), 1.5,
// TODO: make this test nicer
const month1 = relativeTo.add({ months: 1 })
const middle = relativeTo.add(duration)
const month2 = relativeTo.add({ months: 2 })
const answer = 1 + (
Number(middle.epochNanoseconds - month1.epochNanoseconds) /
Number(month2.epochNanoseconds - month1.epochNanoseconds)
)

assert.sameValue(duration.total({ unit: "months", relativeTo }), answer,
"1 month 15 days should be exactly 1.5 months");
}

0 comments on commit fd9a0d7

Please sign in to comment.