/* * Copyright 2024 Magnus Madsen * * Use of this source code is governed by the Apache 2.0 license * that can be found in the LICENSE.md file. */pubmod Time.Clock {use Time.Clockuse Time.TimeUnitimport java.lang.Systemimport java.util.concurrent.{TimeUnit => JTimeUnit}////// An effect used to access the current (real-world) time.///pubeffClock {////// Returns a measure of time since the epoch in the given time unit `u`.///defcurrentTime(u: TimeUnit): Int64 }////// Returns the number of milliseconds since the epoch.////// Equivalent to `currentTime(TimeUnit.Milliseconds)`.///pubdefnow(): Int64 \ Clock = Clock.currentTime(TimeUnit.Milliseconds)////// Handles the `Clock` effect of the given function `f`.////// In other words, re-interprets the `Clock` effect using the `IO` effect.///pubdefhandle(f: a -> b \ ef): a -> b \ (ef - Clock) + IO = x ->run {f(x) } withhandlerClock {defcurrentTime(u, k) = {lettime = System.currentTimeMillis();letms = JTimeUnit.MILLISECONDS;matchu {case TimeUnit.Days => k(ms.toDays(time))case TimeUnit.Hours => k(ms.toHours(time))case TimeUnit.Microseconds => k(ms.toMicros(time))case TimeUnit.Milliseconds => k(ms.toMillis(time))case TimeUnit.Minutes => k(ms.toMinutes(time))case TimeUnit.Nanoseconds => k(ms.toNanos(time))case TimeUnit.Seconds => k(ms.toSeconds(time)) } } }////// Runs the `Clock` effect of the given function `f`.////// In other words, re-interprets the `Clock` effect using the `IO` effect.///@DefaultHandlerpubdefrunWithIO(f: Unit -> a \ ef): a \ (ef - Clock) + IO = handle(f)()}