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(
// hedgehog.core.GenT$$Lambda$11543/0x00007fa1b9e8a6e0@7be125f8
// )

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(
// hedgehog.core.GenT$$Lambda$11543/0x00007fa1b9e8a6e0@f9f43cd
// )
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: 81739531082
// instant: 2026-01-21T01:03:24.347Z
// instant: 2026-03-06T23:29:02.999Z
// instant: 2026-01-12T04:09:21.518Z
// instant: 2026-10-07T08:22:21.075Z
// instant: 2026-01-03T19:57:12.301Z
// + repl.MdocSession$MdocApp$InstantGeneratorsExampleSpec$.Example for instantGen: OK, passed 5 tests
// aroundBaseInstant: 2026-05-25T22:59:52.752066485Z
// aroundBaseInstant: 2026-05-31T03:02:20.526988241Z
// aroundBaseInstant: 2026-05-30T10:04:56.297562588Z
// aroundBaseInstant: 2026-05-25T13:41:42.205717277Z
// aroundBaseInstant: 2026-05-30T14:28:27.887533249Z
// + 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(
// hedgehog.core.GenT$$Lambda$11543/0x00007fa1b9e8a6e0@3af77ef0
// )

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(
// hedgehog.core.GenT$$Lambda$11543/0x00007fa1b9e8a6e0@421d1f4c
// )
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: 81745859762
// localDate: 2026-02-02
// localDate: 2026-05-23
// localDate: 2026-05-02
// localDate: 2026-09-03
// localDate: 2026-09-14
// + repl.MdocSession$MdocApp$LocalDateGeneratorsExampleSpec$.Example for localDateGen: OK, passed 5 tests
// aroundBaseDate: 2026-05-14
// aroundBaseDate: 2026-05-21
// aroundBaseDate: 2026-05-21
// aroundBaseDate: 2026-06-02
// aroundBaseDate: 2026-05-16
// + repl.MdocSession$MdocApp$LocalDateGeneratorsExampleSpec$.Example for aroundBaseDateGen: OK, passed 5 tests