Duration
The Duration module provides a type for representing time spans. Durations can be created from various time units and used with timestamps for date/time arithmetic.
Creation
Duration.from-millis
Int -> Duration
Creates a Duration from milliseconds.
dur = Duration.from-millis 5000
println (Duration.to-seconds dur)
# => 5
Duration.from-seconds
Int -> Duration
Creates a Duration from seconds.
dur = Duration.from-seconds 60
println (Duration.to-minutes dur)
# => 1
Duration.from-minutes
Int -> Duration
Creates a Duration from minutes.
dur = Duration.from-minutes 90
println (Duration.to-hours dur)
# => 1
Duration.from-hours
Int -> Duration
Creates a Duration from hours.
dur = Duration.from-hours 24
println (Duration.to-days dur)
# => 1
Duration.from-days
Int -> Duration
Creates a Duration from days.
dur = Duration.from-days 7
println (Duration.to-hours dur)
# => 168
Duration.parse
String -> Option Duration
Parses a duration string. Returns
Some on success, None on failure.
match Duration.parse "2h30m"
| Some dur -> println (Duration.to-minutes dur)
| None -> println "Invalid duration"
# => 150
Conversion
Duration.to-millis
Duration -> Int
Converts a Duration to milliseconds.
Duration.to-seconds
Duration -> Int
Converts a Duration to whole seconds.
Duration.to-minutes
Duration -> Int
Converts a Duration to whole minutes.
Duration.to-hours
Duration -> Int
Converts a Duration to whole hours.
Duration.to-days
Duration -> Int
Converts a Duration to whole days.
Duration.to-string
Duration -> String
Converts a Duration to a human-readable string.
dur = Duration.from-minutes 150
println (Duration.to-string dur)
# => "2h30m"
Arithmetic
Duration.add
Duration -> Duration -> Duration
Adds two durations together.
d1 = Duration.from-hours 2
d2 = Duration.from-minutes 30
total = Duration.add d1 d2
println (Duration.to-minutes total)
# => 150
Duration.subtract
Duration -> Duration -> Duration
Subtracts the second duration from the first.
d1 = Duration.from-hours 3
d2 = Duration.from-hours 1
diff = Duration.subtract d1 d2
println (Duration.to-hours diff)
# => 2
Duration.multiply
Duration -> Int -> Duration
Multiplies a duration by an integer factor.
dur = Duration.from-hours 2
tripled = Duration.multiply dur 3
println (Duration.to-hours tripled)
# => 6
Duration.divide
Duration -> Int -> Duration
Divides a duration by an integer factor.
dur = Duration.from-hours 6
halved = Duration.divide dur 2
println (Duration.to-hours halved)
# => 3
Duration.negate
Duration -> Duration
Negates a duration (reverses its sign).
dur = Duration.from-hours 5
neg = Duration.negate dur
println (Duration.to-hours neg)
# => -5
Duration.abs
Duration -> Duration
Returns the absolute value of a duration.
dur = Duration.negate (Duration.from-hours 5)
abs-dur = Duration.abs dur
println (Duration.to-hours abs-dur)
# => 5
Predicates
Duration.is-zero?
Duration -> Bool
Returns true if the duration is zero.
dur = Duration.from-millis 0
println (Duration.is-zero? dur)
# => true
Duration.is-negative?
Duration -> Bool
Returns true if the duration is negative.
dur = Duration.negate (Duration.from-hours 1)
println (Duration.is-negative? dur)
# => true