Skip to content

Add TemporalUnit overloads to timeAndDate(), deprecate TimeUnit variants - #1912

Merged
kingthorin merged 2 commits into
datafaker-net:mainfrom
LouisDeconinck:feature/temporal-unit-timeanddate-1911
Sep 12, 2026
Merged

Add TemporalUnit overloads to timeAndDate(), deprecate TimeUnit variants#1912
kingthorin merged 2 commits into
datafaker-net:mainfrom
LouisDeconinck:feature/temporal-unit-timeanddate-1911

Conversation

@LouisDeconinck

Copy link
Copy Markdown
Contributor

Closes #1911

What

timeAndDate().past(10, TimeUnit.of(ChronoUnit.YEARS)) fails today because java.util.concurrent.TimeUnit maxes out at days. Per the approach agreed in the issue: the twelve future/past overloads taking TimeUnit are deprecated (@Deprecated(since = "3.0.0", forRemoval = true), delegating via unit.toChronoUnit()), and twelve equivalents taking java.time.temporal.TemporalUnit are added — so ChronoUnit.YEARS, MONTHS, etc. now work.

Bounds are computed via referenceDate.atZone(ZoneId.systemDefault()).plus/minus(atMost, unit) so calendar units are handled correctly (Instant.plus would reject units above days); the random instant still comes from the existing between(from, to) helper, preserving the uniform distribution and strict-before/after invariants.

Testing

  • ./mvnw test -Dtest=TimeAndDateTest — 1930 tests pass, including new ChronoUnit.YEARS bounds cases and TemporalUnit variants of the bounds/minimum/reference-date tests
  • FakerTest + DateAndTimeTest pass — existing TimeUnit behavior and #{date.past '15','SECONDS'} expressions unaffected
  • ./mvnw spotless:check clean

Docs (date-format.md) examples updated to ChronoUnit.

…datafaker-net#1911)

java.util.concurrent.TimeUnit cannot express units larger than days, so
timeAndDate().past()/future() could not be used with e.g. years.

The past()/future() overloads taking TimeUnit are now deprecated and
replaced by equivalent overloads taking java.time.temporal.TemporalUnit,
which also supports calendar-based units such as ChronoUnit.YEARS.
@what-the-diff

what-the-diff Bot commented Sep 12, 2026

Copy link
Copy Markdown

PR Summary

  • Updated Date Formatting in Documentation
    This Pull Request updates how we show dates in our documentation. Previously, we used a method called TimeUnit. However, now we are switching to a different method called ChronoUnit. This is pertaining to methods that deal with dates in the future and the past.

  • Increased Test Coverage
    We've added multiple new tests in a file called TimeAndDateTest.java. These tests are to ensure our date functionalities (those dealing with future and past dates) using ChronoUnit are working as expected. These tests check various scenarios, like future dates with different units (such as seconds, and years) and past dates with various units and reference dates. We've also added tests to check date operations involving masking.

  • Warning Suppression Annotation
    There's an update in TimeAndDateTest class where we have introduced a suppression annotation. This annotation is mainly used to suppress or mute any warnings that arise due to code removal in this class.

between() includes its lower bound, so past() could return exactly
referenceDate - atMost, tripping the strict > assertion in
testPastDateWithReferenceDate. Mirror the original -1 - nextLong(bound-1)
formula so the range stays (ref - atMost, ref) exclusive.

Signed-off-by: Louis Deconinck <louis.dck@gmail.com>
@codecov-commenter

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.28%. Comparing base (36536ad) to head (93ff100).
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@             Coverage Diff              @@
##               main    #1912      +/-   ##
============================================
- Coverage     92.38%   92.28%   -0.10%     
- Complexity     3601     3608       +7     
============================================
  Files           348      348              
  Lines          7115     7127      +12     
  Branches        695      695              
============================================
+ Hits           6573     6577       +4     
- Misses          373      377       +4     
- Partials        169      173       +4     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 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.

@bodiam

bodiam commented Sep 12, 2026

Copy link
Copy Markdown
Contributor

Looks good to me! Thanks for the contribution! Anyone else feedback?

@kingthorin kingthorin left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM

@asolntsev asolntsev left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Looks good!

@asolntsev asolntsev added this to the 3.0.0 milestone Sep 12, 2026
@asolntsev asolntsev added the enhancement New feature or request label Sep 12, 2026
@kingthorin
kingthorin merged commit ae81cdb into datafaker-net:main Sep 12, 2026
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

timeAndDate() should use java.time.temporal.TemporalUnit rather than java.util.concurrent.TimeUnit

5 participants