Add TemporalUnit overloads to timeAndDate(), deprecate TimeUnit variants - #1912
Conversation
…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.
PR Summary
|
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 Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
|
Looks good to me! Thanks for the contribution! Anyone else feedback? |
Closes #1911
What
timeAndDate().past(10, TimeUnit.of(ChronoUnit.YEARS))fails today becausejava.util.concurrent.TimeUnitmaxes out at days. Per the approach agreed in the issue: the twelvefuture/pastoverloads takingTimeUnitare deprecated (@Deprecated(since = "3.0.0", forRemoval = true), delegating viaunit.toChronoUnit()), and twelve equivalents takingjava.time.temporal.TemporalUnitare added — soChronoUnit.YEARS,MONTHS, etc. now work.Bounds are computed via
referenceDate.atZone(ZoneId.systemDefault()).plus/minus(atMost, unit)so calendar units are handled correctly (Instant.pluswould reject units above days); the random instant still comes from the existingbetween(from, to)helper, preserving the uniform distribution and strict-before/after invariants.Testing
./mvnw test -Dtest=TimeAndDateTest— 1930 tests pass, including newChronoUnit.YEARSbounds cases and TemporalUnit variants of the bounds/minimum/reference-date testsFakerTest+DateAndTimeTestpass — existingTimeUnitbehavior and#{date.past '15','SECONDS'}expressions unaffected./mvnw spotless:checkcleanDocs (
date-format.md) examples updated toChronoUnit.