Skip to content

Instantly share code, notes, and snippets.

@daviddenton
Created June 21, 2023 09:22
Show Gist options
  • Save daviddenton/d5dfcc51cb5c47b01c0e5cac8d096f72 to your computer and use it in GitHub Desktop.
Save daviddenton/d5dfcc51cb5c47b01c0e5cac8d096f72 to your computer and use it in GitHub Desktop.
AdjustableTickingClock
class AdjustableTickingClock(
private val startTime: Instant = EPOCH,
private val tick: Duration = Duration.ofSeconds(5)
) : Clock() {
private var currentTime = startTime
override fun getZone() = ZoneId.of("UTC")
override fun withZone(zone: ZoneId?) = this
override fun instant() = currentTime.also { currentTime += tick }
fun reset(): AdjustableTickingClock {
currentTime = startTime
return this
}
fun goBackBy(duration: Duration): AdjustableTickingClock {
currentTime -= duration
return this
}
fun goForwardBy(duration: Duration): AdjustableTickingClock {
currentTime += duration
return this
}
fun nowIs(now: Instant) {
currentTime = now
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment