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 {
  Microseconds(Float)
  Milliseconds(Float)
  Seconds(Float)
  Minutes(Float)
  Hours(Float)
  Days(Float)
  Weeks(Float)
}

Constructors

  • Microseconds(Float)
  • Milliseconds(Float)
  • Seconds(Float)
  • Minutes(Float)
  • Hours(Float)
  • Days(Float)
  • Weeks(Float)

Functions

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.Milliseconds(1.0) |> time.as_microseconds // 1000.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_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 humanise(this time: Time) -> Time

Convert a value to a more optimal unit, if possible.

Example:

time.Seconds(120.0) |> time.humanise // time.Minutes(2.0)
pub fn to_string(this time: Time) -> String

Format a value as a String, rounded to at most 2 decimal places, followed by a unit suffix.

Example:

time.Seconds(30.125) |> time.to_string // "30.13s"
Search Document