jqwik icon indicating copy to clipboard operation
jqwik copied to clipboard

Feature: support ZonedDateTime in ZonedDateTimeArbitrary atTheLatest, atTheEarliest and between

Open muItilingualism opened this issue 4 months ago • 1 comments

Hi

I am working with dates and switched from LocalDateTime to ZonedDateTime and encountered that ZonedDateTimeArbitrary atTheEarliest and atTheLatest only supports LocalDateTime.

Having to convert the ZonedDateTime to LocalDateTime leads to funky tests with data such as

first: 1900-01-01T00:00-10:29:20[Pacific/Kiritimati]
second: 1900-01-01T00:00+14:00[Etc/GMT-14]

Chronologically first is actually after second which took me embarrassingly long to calculate...

My ideal usecase would be

@Provide
Arbitrary<Pair<ZonedDateTime, ZonedDateTime>> validDatePairs() {
    Arbitrary<ZonedDateTime> first = DateTimes.zonedDateTimes();
    return first.flatMap(firstDate -> DateTimes.zonedDateTimes()
            .atTheEarliest(firstDate.plusNanos(1)) // accepts ZonedDateTime
            .map(secondDate -> new Pair<>(firstDate, secondDate)));
}

However, for anyone who has the same usecase, the following totally works just fine as well:

@Provide
Arbitrary<Pair<ZonedDateTime, ZonedDateTime>> validDatePairs() {
    Arbitrary<Duration> positiveDuration = Times.durations().between(
            Duration.ofNanos(1),
            Duration.ofDays(365 * 100));
    Arbitrary<ZonedDateTime> second = DateTimes.zonedDateTimes();
    return second.flatMap(
            secondDate -> positiveDuration.map(duration -> new Pair<>(secondDate.plus(duration), secondDate)));
}

This also applies to .between

Thanks

muItilingualism avatar Oct 14 '25 20:10 muItilingualism

Sounds like a reasonable extension of the time module. Thanks.

jlink avatar Oct 15 '25 05:10 jlink