Skip to content

Commit

Permalink
Improve randomPastDate & randomFutureDate methods
Browse files Browse the repository at this point in the history
  • Loading branch information
mboisnard authored and serpro69 committed Mar 9, 2024
1 parent 6219997 commit c60787c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
12 changes: 12 additions & 0 deletions core/src/main/kotlin/io/github/serpro69/kfaker/IRandom.kt
Original file line number Diff line number Diff line change
Expand Up @@ -264,12 +264,24 @@ interface IRandom {
*/
fun randomPastDate(): OffsetDateTime

/**
* Returns a pseudorandom, uniformly distributed [OffsetDateTime] value
* between [min] (inclusive) and now (exclusive) using UTC zone offset
*/
fun randomPastDate(min: Instant): OffsetDateTime

/**
* Returns a pseudorandom, uniformly distributed [OffsetDateTime] value
* between now (exclusive) and now + 50 years (inclusive) using UTC zone offset
*/
fun randomFutureDate(): OffsetDateTime

/**
* Returns a pseudorandom, uniformly distributed [OffsetDateTime] value
* between now (exclusive) and [max] (inclusive) using UTC zone offset
*/
fun randomFutureDate(max: Instant): OffsetDateTime

/**
* Returns a pseudorandom, uniformly distributed [OffsetDateTime] value
* between [min] (inclusive) and [max] (inclusive) using the defined [zoneOffset]
Expand Down
19 changes: 16 additions & 3 deletions core/src/main/kotlin/io/github/serpro69/kfaker/RandomService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -226,13 +226,26 @@ class RandomService internal constructor(override val config: FakerConfig) : IRa
}

override fun randomPastDate(): OffsetDateTime {
return randomDate(Instant.ofEpochSecond(0), Instant.now().minusMillis(1), ZoneOffset.UTC)
return randomPastDate(Instant.ofEpochSecond(0))
}

override fun randomPastDate(min: Instant): OffsetDateTime {
val now = Instant.now()
require(min.isBefore(now))

return randomDate(min, now.minusMillis(1), ZoneOffset.UTC)
}

override fun randomFutureDate(): OffsetDateTime {
val maxInstant = Instant.now().plus(Duration.ofDays(50 * 365))
return randomFutureDate(maxInstant)
}

override fun randomFutureDate(max: Instant): OffsetDateTime {
val now = Instant.now()
val maxInstant = now.plus(Duration.ofDays(50 * 365))
return randomDate(now.plusMillis(1), maxInstant, ZoneOffset.UTC)
require(max.isAfter(now))

return randomDate(now.plusMillis(1), max, ZoneOffset.UTC)
}

override fun randomDate(min: Instant, max: Instant, zoneOffset: ZoneOffset): OffsetDateTime {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,12 @@ class RandomProvider internal constructor(

override fun randomPastDate(): OffsetDateTime = resolveUnique(RANDOM_DATE) { rs.randomPastDate() }

override fun randomPastDate(min: Instant): OffsetDateTime = resolveUnique(RANDOM_DATE) { rs.randomPastDate(min) }

override fun randomFutureDate(): OffsetDateTime = resolveUnique(RANDOM_DATE) { rs.randomFutureDate() }

override fun randomFutureDate(max: Instant): OffsetDateTime = resolveUnique(RANDOM_DATE) { rs.randomFutureDate(max) }

override fun randomDate(min: Instant, max: Instant, zoneOffset: ZoneOffset): OffsetDateTime =
resolveUnique(RANDOM_SUBSET) { rs.randomDate(min, max, zoneOffset) }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,12 @@ Faker().random.randomSublist(listOf(1, 2, 3, 4, 5, 6))
Faker().random.randomPastDate()
Faker().random.randomPastDate(min = Instant.ofEpochSecond(0))
Faker().random.randomFutureDate()
Faker().random.randomFutureDate(max = Instant.now().plus(Duration.ofDays(7)))
Faker().random.randomDate(min = Instant.ofEpochSecond(0), max = Instant.now(), zoneOffset = ZoneOffset.UTC)
// and more
Expand Down

0 comments on commit c60787c

Please sign in to comment.