You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fix(date): keep birthdate within the age range when refDate is a leap day - #4208
faker.date.birthdate({ mode: 'age' }) builds its range by moving refDate back by whole years with setUTCFullYear. When refDate is February 29 and the target year is a common year, setUTCFullYear rolls the date over to March 1. That puts the latest birthdate one day too late, so the result can be one year younger than min:
constrefDate=newDate(Date.UTC(2024,1,29,12));// 20,000 draws of faker.date.birthdate({ mode: 'age', min: 18, max: 18, refDate })// 31 are born on 2006-03-01 and are 17 on refDate (their 18th birthday is 2024-03-01)
The fix moves a February 29 refDate to February 28 when the target year has no leap day, for both ends of the range. The latest birthdate becomes 2006-02-28 (18 on refDate), and the earliest becomes 1943-03-01 instead of 1943-03-02 for max: 80 (still 80 on refDate, so it was wrongly excluded). Other dates, and mode: 'year', are unchanged.
Two tests use a fixed randomizer (like the past/future range tests) to pin each end of the range for a leap-day refDate. Both fail on next (2006-03-01 and 1943-03-02) and pass with the fix. test/modules/date.spec.ts passes (711 tests) and no snapshots change.
AI-assisted: I directed the investigation and reviewed and verified the change myself.
Thanks! Switched the randomizer to MERSENNE_MAX_VALUE and applied the suggested comment in 6f51ed4. test/modules/date.spec.ts passes locally (711 tests).
Yes, partly. past and future build their bounds with the same setUTCFullYear, so with a refDate of 2024-02-29 "1 year ago" becomes 2023-03-01 and "1 year ahead" 2025-03-01. Both bounds move by the same day, so the range keeps its length. Nothing breaks there the way the age check did in birthdate, but past({ years: { min: 1, max: 2 } }) can return 2023-02-28, which is less than a year before the refDate if you count Feb 28 as the anniversary (date-fns subYears clamps that way). If you'd like past/future to clamp to Feb 28 as well, I can open a follow-up PR with tests for it.
Looks like we are getting some weak runners today.
(On Monday 26 PRs merged without a single failure, today we are at 3/3 failures)
I think i found a way to address this issue and will create a fix for it later.
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
c: bugSomething isn't workingm: dateSomething is referring to the date modulep: 1-normalNothing urgent
3 participants
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.
faker.date.birthdate({ mode: 'age' })builds its range by movingrefDateback by whole years withsetUTCFullYear. WhenrefDateis February 29 and the target year is a common year,setUTCFullYearrolls the date over to March 1. That puts the latest birthdate one day too late, so the result can be one year younger thanmin:The fix moves a February 29
refDateto February 28 when the target year has no leap day, for both ends of the range. The latest birthdate becomes 2006-02-28 (18 on refDate), and the earliest becomes 1943-03-01 instead of 1943-03-02 formax: 80(still 80 on refDate, so it was wrongly excluded). Other dates, andmode: 'year', are unchanged.Two tests use a fixed randomizer (like the
past/futurerange tests) to pin each end of the range for a leap-dayrefDate. Both fail onnext(2006-03-01 and 1943-03-02) and pass with the fix.test/modules/date.spec.tspasses (711 tests) and no snapshots change.AI-assisted: I directed the investigation and reviewed and verified the change myself.