humanise/time
This module contains functions for formatting durations of time to Strings (e.g. "100.0ms", "1.5s").
Usage generally looks like this:
time.Millisecond(2000.0) |> time.humanise |> time.to_string // "2.0s"
// or, if you don't want to change the unit
time.Millisecond(2000.0) |> time.to_string // "2000.0ms"
Types
The main type for holding time information.
Use its constructors directly to specify a unit for the value you want to format.
pub type Time {
Nanoseconds(Float)
Microseconds(Float)
Milliseconds(Float)
Seconds(Float)
Minutes(Float)
Hours(Float)
Days(Float)
Weeks(Float)
}
Constructors
-
Nanoseconds(Float) -
Microseconds(Float) -
Milliseconds(Float) -
Seconds(Float) -
Minutes(Float) -
Hours(Float) -
Days(Float) -
Weeks(Float)
Values
pub fn as_days(this time: Time) -> Float
Convert a value to days.
Example:
time.Hours(24.0) |> time.as_days // 1.0
pub fn as_hours(this time: Time) -> Float
Convert a value to hours.
Example:
time.Minutes(60.0) |> time.as_hours // 1.0
pub fn as_microseconds(this time: Time) -> Float
Convert a value to microseconds.
Example:
time.Nanoseconds(1000.0) |> time.as_microseconds // 1.0
pub fn as_milliseconds(this time: Time) -> Float
Convert a value to milliseconds.
Example:
time.Microseconds(1000.0) |> time.as_milliseconds // 1.0
pub fn as_minutes(this time: Time) -> Float
Convert a value to minutes.
Example:
time.Seconds(60.0) |> time.as_minutes // 1.0
pub fn as_nanoseconds(this time: Time) -> Float
Convert a value to nanoseconds.
Example:
time.Microseconds(1.0) |> time.as_nanoseconds // 1000.0
pub fn as_seconds(this time: Time) -> Float
Convert a value to seconds.
Example:
time.Milliseconds(1000.0) |> time.as_seconds // 1.0
pub fn as_weeks(this time: Time) -> Float
Convert a value to weeks.
Example:
time.Days(7.0) |> time.as_weeks // 1.0
pub fn from_duration(this duration: duration.Duration) -> Time
Convert a Duration from gleam/time.
Example:
duration.seconds(120) |> time.from_duration // time.Minutes(2.0)