Skip to main content

TimeGens

TimeGens provides generators for Instant and LocalDate ranges.

Import

import hedgehog.*
import hedgehog.extra.TimeGens
import java.time.{Instant, LocalDate}
import scala.concurrent.duration.*

Instant generators

val fromInstant = Instant.parse("2026-01-01T00:00:00Z")
// fromInstant: Instant = 2026-01-01T00:00:00Z
val toInstant = Instant.parse("2026-12-31T23:59:59Z")
// toInstant: Instant = 2026-12-31T23:59:59Z

val instantGen: Gen[Instant] =
TimeGens.genInstant(fromInstant, toInstant)
// instantGen: GenT[Instant] = GenT(
// run = hedgehog.core.GenT$$Lambda$11268/0x00007f0539e251c8@6b8ffe29
// )

val baseInstant = Instant.parse("2026-06-01T00:00:00Z")
// baseInstant: Instant = 2026-06-01T00:00:00Z
val aroundBaseInstantGen: Gen[Instant] =
TimeGens.genInstantFrom(baseInstant, 7.days, 14.days)
// aroundBaseInstantGen: GenT[Instant] = GenT(
// run = hedgehog.core.GenT$$Lambda$11268/0x00007f0539e251c8@170cda50
// )
import hedgehog.*
import hedgehog.runner.*

object InstantGeneratorsExampleSpec extends Properties {
override def tests: List[Test] = List(
property(
"Example for instantGen",
testExampleForInstantGen
).withTests(5), // only for documentation to show fewer logs
property(
"Example for aroundBaseInstantGen",
testExampleForAroundBaseInstantGen
).withTests(5) // only for documentation to show fewer logs
)

def testExampleForInstantGen: Property =
for {
actual <- instantGen.log("actual")
} yield {
println(s"instant: $actual") // only for documentation to show the value
Result
.assert(!actual.isBefore(fromInstant) && !actual.isAfter(toInstant))
.log("Generated Instant should be in [fromInstant, toInstant]")
}

def testExampleForAroundBaseInstantGen: Property = {
val expectedFrom = baseInstant.minusMillis(7.days.toMillis)
val expectedTo = baseInstant.plusMillis(14.days.toMillis)

for {
actual <- aroundBaseInstantGen.log("actual")
} yield {
println(s"aroundBaseInstant: $actual") // only for documentation to show the value
Result
.assert(!actual.isBefore(expectedFrom) && !actual.isAfter(expectedTo))
.log("Generated Instant should be in [baseInstant - 7.days, baseInstant + 14.days]")
}
}
}

// This is only for doc. Your test code should be run by sbt (or maybe another build tool)
InstantGeneratorsExampleSpec.main(Array.empty)
// Using random seed: 136942156428
// instant: 2026-02-22T11:25:48.077Z
// instant: 2026-04-24T16:24:37.536Z
// instant: 2026-06-15T05:44:07.469Z
// instant: 2026-08-13T12:28:11.224Z
// instant: 2026-09-26T21:15:11.754Z
// + repl.MdocSession$MdocApp$InstantGeneratorsExampleSpec$.Example for instantGen: OK, passed 5 tests
// aroundBaseInstant: 2026-05-26T19:02:03.406388603Z
// aroundBaseInstant: 2026-05-31T14:18:57.908563972Z
// aroundBaseInstant: 2026-06-03T11:26:36.606278289Z
// aroundBaseInstant: 2026-06-06T03:13:31.480959634Z
// aroundBaseInstant: 2026-06-05T06:35:21.648214296Z
// + repl.MdocSession$MdocApp$InstantGeneratorsExampleSpec$.Example for aroundBaseInstantGen: OK, passed 5 tests

LocalDate generators

val fromDate = LocalDate.parse("2026-01-01")
// fromDate: LocalDate = 2026-01-01
val toDate = LocalDate.parse("2026-12-31")
// toDate: LocalDate = 2026-12-31

val localDateGen: Gen[LocalDate] =
TimeGens.genLocalDate(fromDate, toDate)
// localDateGen: GenT[LocalDate] = GenT(
// run = hedgehog.core.GenT$$Lambda$11268/0x00007f0539e251c8@22e6f53c
// )

val baseDate = LocalDate.parse("2026-06-01")
// baseDate: LocalDate = 2026-06-01
val aroundBaseDateGen: Gen[LocalDate] =
TimeGens.genLocalDateFrom(baseDate, 30.days, 30.days)
// aroundBaseDateGen: GenT[LocalDate] = GenT(
// run = hedgehog.core.GenT$$Lambda$11268/0x00007f0539e251c8@63ded6d7
// )
import hedgehog.*
import hedgehog.runner.*

object LocalDateGeneratorsExampleSpec extends Properties {
override def tests: List[Test] = List(
property(
"Example for localDateGen",
testExampleForLocalDateGen
).withTests(5), // only for documentation to show fewer logs
property(
"Example for aroundBaseDateGen",
testExampleForAroundBaseDateGen
).withTests(5) // only for documentation to show fewer logs
)

def testExampleForLocalDateGen: Property =
for {
actual <- localDateGen.log("actual")
} yield {
println(s"localDate: $actual") // only for documentation to show the value
Result
.assert(
(actual.isEqual(fromDate) || actual.isAfter(fromDate)) &&
(actual.isEqual(toDate) || actual.isBefore(toDate))
)
.log("Generated LocalDate should be in [fromDate, toDate]")
}

def testExampleForAroundBaseDateGen: Property = {
val expectedFrom = baseDate.minusDays(30.days.toDays)
val expectedTo = baseDate.plusDays(30.days.toDays)

for {
actual <- aroundBaseDateGen.log("actual")
} yield {
println(s"aroundBaseDate: $actual") // only for documentation to show the value
Result
.assert(
(actual.isEqual(expectedFrom) || actual.isAfter(expectedFrom)) &&
(actual.isEqual(expectedTo) || actual.isBefore(expectedTo))
)
.log("Generated LocalDate should be in [baseDate - 30.days, baseDate + 30.days]")
}
}
}

// This is only for doc. Your test code should be run by sbt (or maybe another build tool)
LocalDateGeneratorsExampleSpec.main(Array.empty)
// Using random seed: 136947109984
// localDate: 2026-02-25
// localDate: 2026-04-08
// localDate: 2026-01-19
// localDate: 2026-05-06
// localDate: 2026-12-02
// + repl.MdocSession$MdocApp$LocalDateGeneratorsExampleSpec$.Example for localDateGen: OK, passed 5 tests
// aroundBaseDate: 2026-05-13
// aroundBaseDate: 2026-05-07
// aroundBaseDate: 2026-05-30
// aroundBaseDate: 2026-06-03
// aroundBaseDate: 2026-06-06
// + repl.MdocSession$MdocApp$LocalDateGeneratorsExampleSpec$.Example for aroundBaseDateGen: OK, passed 5 tests