Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 192
Fix calendar-aligned elapsed durations#376
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
Closed
+233
−27
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
feecbdd
Fix calendar-aligned elapsed durations
francinelucca d6b5c94
Refactor calendar duration calculation
francinelucca 6972406
Correct premature calendar year estimates
francinelucca ca4886b
Document calendar duration helpers
francinelucca 0fd6fb3
Respect precision for anniversary remainders
francinelucca ee5730e
Use calendar anchors for year durations
francinelucca 549dc25
Clamp calendar month anchors
francinelucca 2edfb1c
Unify calendar duration semantics
francinelucca 9446af6
Potential fix for pull request finding
francinelucca File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -126,17 +126,16 @@ export class Duration { | ||
| } | ||
| export function applyDuration(date: Date | number, duration: Duration): Date { | ||
| const r = new Date(date) | ||
| let r = new Date(date) | ||
| const calendarMonths = duration.years * 12 + duration.months | ||
| if (duration.sign < 0) { | ||
| r.setUTCSeconds(r.getUTCSeconds() + duration.seconds) | ||
| r.setUTCMinutes(r.getUTCMinutes() + duration.minutes) | ||
| r.setUTCHours(r.getUTCHours() + duration.hours) | ||
| r.setUTCDate(r.getUTCDate() + duration.weeks * 7 + duration.days) | ||
| r.setUTCMonth(r.getUTCMonth() + duration.months) | ||
| r.setUTCFullYear(r.getUTCFullYear() + duration.years) | ||
| r = applyCalendarMonths(r, calendarMonths) | ||
| } else { | ||
| r.setUTCFullYear(r.getUTCFullYear() + duration.years) | ||
| r.setUTCMonth(r.getUTCMonth() + duration.months) | ||
| r = applyCalendarMonths(r, calendarMonths) | ||
| r.setUTCDate(r.getUTCDate() + duration.weeks * 7 + duration.days) | ||
| r.setUTCHours(r.getUTCHours() + duration.hours) | ||
| r.setUTCMinutes(r.getUTCMinutes() + duration.minutes) | ||
| @@ -145,6 +144,108 @@ export function applyDuration(date: Date | number, duration: Duration): Date { | ||
| return r | ||
| } | ||
| /** | ||
| * Applies a number of calendar months to a copy of the reference date. | ||
| * | ||
| * @param reference - Date from which to count. | ||
| * @param months - Signed number of months to apply. | ||
| * @returns The resulting date without modifying the reference. | ||
| */ | ||
| function applyCalendarMonths(reference: Date, months: number): Date { | ||
| const result = new Date(reference) | ||
| const referenceDay = result.getUTCDate() | ||
| // Move from the first to avoid rolling an invalid date into the next month. | ||
| result.setUTCDate(1) | ||
| result.setUTCMonth(result.getUTCMonth() + months) | ||
| const targetMonth = result.getUTCMonth() | ||
| result.setUTCMonth(targetMonth + 1, 0) | ||
| result.setUTCDate(Math.min(referenceDay, result.getUTCDate())) | ||
| return result | ||
| } | ||
| /** | ||
| * Checks whether two UTC times match through the requested precision. | ||
| * | ||
| * @param date - Date being compared. | ||
| * @param reference - Reference date for the comparison. | ||
| * @param precisionIndex - Index of the requested unit in {@link unitNames}. | ||
| */ | ||
| function hasSameTimeAtPrecision(date: Date, reference: Date, precisionIndex: number): boolean { | ||
| if (precisionIndex <= unitNames.indexOf('day')) return true | ||
| if (date.getUTCHours() !== reference.getUTCHours()) return false | ||
| if (precisionIndex === unitNames.indexOf('hour')) return true | ||
| if (date.getUTCMinutes() !== reference.getUTCMinutes()) return false | ||
| if (precisionIndex === unitNames.indexOf('minute')) return true | ||
| if (date.getUTCSeconds() !== reference.getUTCSeconds()) return false | ||
| if (precisionIndex === unitNames.indexOf('second')) return true | ||
| return date.getUTCMilliseconds() === reference.getUTCMilliseconds() | ||
| } | ||
| /** | ||
| * Returns a calendar-based duration for completed calendar-month spans or | ||
| * endpoints aligned at the requested precision, leaving shorter intervals on | ||
| * the fixed-duration path. | ||
| * | ||
| * @param date - Target date. | ||
| * @param reference - Date from which elapsed time is measured. | ||
| * @param precisionIndex - Index of the requested unit in {@link unitNames}. | ||
| * @returns The corrected duration, or `undefined` when no correction is needed. | ||
| */ | ||
Copilot marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| function calendarElapsedTime(date: Date, reference: Date, precisionIndex: number): Duration | undefined { | ||
| const calendarMonths = | ||
| (date.getUTCFullYear() - reference.getUTCFullYear()) * 12 + date.getUTCMonth() - reference.getUTCMonth() | ||
| // Anchor the candidate month count to the reference, then back it off if it | ||
| // crossed the target. This prevents 30-day estimates from inventing a year. | ||
| let wholeMonths = calendarMonths | ||
| let anchor = applyCalendarMonths(reference, calendarMonths) | ||
| const candidateAligned = | ||
| anchor.getUTCFullYear() === date.getUTCFullYear() && | ||
| anchor.getUTCMonth() === date.getUTCMonth() && | ||
| anchor.getUTCDate() === date.getUTCDate() && | ||
| hasSameTimeAtPrecision(date, anchor, precisionIndex) | ||
| const candidateOvershot = | ||
| calendarMonths !== 0 && !candidateAligned && (calendarMonths > 0 ? anchor > date : anchor < date) | ||
| if (candidateOvershot) { | ||
| wholeMonths += calendarMonths > 0 ? -1 : 1 | ||
| anchor = applyCalendarMonths(reference, wholeMonths) | ||
| } | ||
| const isCalendarAligned = | ||
| anchor.getUTCFullYear() === date.getUTCFullYear() && | ||
| anchor.getUTCMonth() === date.getUTCMonth() && | ||
| anchor.getUTCDate() === date.getUTCDate() && | ||
| hasSameTimeAtPrecision(date, anchor, precisionIndex) | ||
| if (!wholeMonths && !isCalendarAligned) return | ||
| // Calendar-aligned durations omit only units below the requested precision. | ||
| // Other corrected durations retain the remainder after the month anchor. | ||
| const sign = Math.sign(date.getTime() - reference.getTime()) | ||
| const remainder = isCalendarAligned ? 0 : Math.abs(date.getTime() - anchor.getTime()) | ||
Comment on lines
+224
to
+225
| ||
| const seconds = Math.floor(remainder / 1000) | ||
| const minutes = Math.floor(seconds / 60) | ||
| const hours = Math.floor(minutes / 60) | ||
| const days = Math.floor(hours / 24) | ||
| let years = 0 | ||
| let months = 0 | ||
| const durationMonths = isCalendarAligned ? calendarMonths : wholeMonths | ||
| const durationYears = Math.trunc(durationMonths / 12) | ||
| if (precisionIndex >= unitNames.indexOf('year')) years = durationYears | ||
| if (precisionIndex >= unitNames.indexOf('month')) months = durationMonths - durationYears * 12 | ||
| return new Duration( | ||
| years, | ||
| months, | ||
| 0, | ||
| precisionIndex >= unitNames.indexOf('day') ? days * sign : 0, | ||
| precisionIndex >= unitNames.indexOf('hour') ? (hours - days * 24) * sign : 0, | ||
| precisionIndex >= unitNames.indexOf('minute') ? (minutes - hours * 60) * sign : 0, | ||
| precisionIndex >= unitNames.indexOf('second') ? (seconds - minutes * 60) * sign : 0, | ||
| precisionIndex >= unitNames.indexOf('millisecond') ? (remainder - seconds * 1000) * sign : 0, | ||
| ) | ||
| } | ||
| export function elapsedTime(date: Date, precision: Unit = 'second', now = Date.now()): Duration { | ||
| const delta = date.getTime() - now | ||
| if (delta === 0) return new Duration() | ||
| @@ -157,6 +258,11 @@ export function elapsedTime(date: Date, precision: Unit = 'second', now = Date.n | ||
| const month = Math.floor(day / 30) | ||
| const year = Math.floor(month / 12) | ||
| const i = unitNames.indexOf(precision) | ||
| const nowDate = new Date(now) | ||
| const calendarDuration = calendarElapsedTime(date, nowDate, i) | ||
| if (calendarDuration) return calendarDuration | ||
| return new Duration( | ||
| i >= 0 ? year * sign : 0, | ||
| i >= 1 ? (month - year * 12) * sign : 0, | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.