Skip to content

fix(date): keep past and future within the year range when refDate is a leap day - #4215

Merged
ST-DDT merged 5 commits into
faker-js:nextfrom
kevin9327:fix/date-past-future-leap-day
Oct 29, 2026
Merged

ST-DDT merged 5 commits into
faker-js:nextfrom
kevin9327:fix/date-past-future-leap-day

Conversation

@kevin9327

@kevin9327 kevin9327 commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Follow-up to #4208, as discussed there: past and future should handle a leap-day refDate the same way birthdate does.

faker.date.past() and faker.date.future() build their bounds by moving refDate by whole years with setUTCFullYear. When refDate is February 29 and the target year is a common year, the bound rolls over to March 1:

const refDate = new Date(Date.UTC(2024, 1, 29, 12));
faker.date.past({ years: { min: 1, max: 2 }, refDate });
// latest possible result: 2023-03-01T11:59:59Z, which is less than one year before refDate
faker.date.future({ years: { min: 1, max: 2 }, refDate });
// latest possible result: 2026-03-01T12:00:00Z, which is more than two years after refDate

This moves sameDayInYear (added for birthdate in #4208, which clamps February 29 to February 28 in a common year, like date-fns addYears/subYears) to src/modules/date/_utils.ts and uses it for both bounds in past and future. Other dates are unchanged.

Tests: four fixed-randomizer tests (like the existing past/future range tests) pin each end of the range for a 2024-02-29 refDate; all four fail on next and pass with the fix. test/modules/date.spec.ts and test/internal pass locally (775 tests), and no snapshots change.

AI-assisted: I directed the investigation and reviewed and verified the change myself.

@kevin9327
kevin9327 requested a review from a team as a code owner October 28, 2026 03:25
@netlify

netlify Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for fakerjs ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit f6b12ab
🔍 Latest deploy log https://app.netlify.com/projects/fakerjs/deploys/6ae31017bc630e76bd936d83
😎 Deploy Preview https://deploy-preview-4215.fakerjs.dev
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@codecov

codecov Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.18%. Comparing base (a9d2058) to head (f6b12ab).
⚠️ Report is 1 commits behind head on next.

Additional details and impacted files
@@           Coverage Diff           @@
##             next    #4215   +/-   ##
=======================================
  Coverage   99.18%   99.18%           
=======================================
  Files        1179     1180    +1     
  Lines        3947     3947           
  Branches      713      713           
=======================================
  Hits         3915     3915           
  Misses         28       28           
  Partials        4        4           
Files with missing lines Coverage Δ
src/modules/date/_utils.ts 100.00% <100.00%> (ø)
src/modules/date/birthdate.ts 100.00% <ø> (ø)
src/modules/date/future.ts 100.00% <100.00%> (ø)
src/modules/date/past.ts 100.00% <100.00%> (ø)

... and 1 file with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@ST-DDT ST-DDT added c: bug Something isn't working p: 1-normal Nothing urgent m: date Something is referring to the date module labels Oct 28, 2026
@ST-DDT ST-DDT added this to the v11.0 milestone Oct 28, 2026
Comment thread src/internal/date.ts Outdated
ST-DDT
ST-DDT previously approved these changes Oct 29, 2026

@ST-DDT ST-DDT left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about the lower bounds each, as it now is technically one day before the refDate.

…-day refDate

The earliest past() date must stay within years.max years and the earliest
future() date at least years.min years away, so those bounds keep the
March 1 rollover; only the latest bounds clamp to February 28.
@kevin9327

Copy link
Copy Markdown
Contributor Author

Good point, thanks. With refDate 2024-02-29, February 28 is the right answer only for the latest bound of each range; for the earliest bound it puts the result one day outside the range (2022-02-28 is more than two years before, 2025-02-28 is less than one year after). Fixed in 4e43327: the earliest bound of past and future keeps the March 1 rollover, the latest bound clamps to February 28. That matches how birthdate treats the max age (1943-03-01). The two tests for the earliest bound now expect March 1. The two tests for the latest bound still fail on next. test/modules/date.spec.ts and test/internal pass (775).

Comment thread test/modules/date.spec.ts Outdated
Comment thread test/modules/date.spec.ts Outdated
Comment thread src/modules/date/past.ts Outdated
Comment thread src/modules/date/past.ts Outdated
@kevin9327

Copy link
Copy Markdown
Contributor Author

Thanks for the suggestions, applied all four in f6b12ab: both test comments now use the "would be less/more than" wording, and in past and future each comment sits right above the line it describes (from → March 1, to → February 28). Comment-only change, no code changes.

@ST-DDT
ST-DDT added this pull request to the merge queue Oct 29, 2026
Merged via the queue into faker-js:next with commit e1f9a84 Oct 29, 2026
23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c: bug Something isn't working m: date Something is referring to the date module p: 1-normal Nothing urgent

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants