element.version === currentVersion)) { versionNodes.unshift({ version: currentVersion, url: "#" }); } document.querySelector("#project-version").innerHTML = versionNodes.reduce( (acc, element) => { const status = currentVersion === element.version ? "selected disabled" : ""; return ` ${acc} `; }, `
duration_format · v1.1.1

duration_format

Package Version Hex Docs

Parse and format gleam/time/duration.Duration values in established string formats. Currently supports Go’s time.ParseDuration grammar — the same format used by Prometheus, Kubernetes, HashiCorp tools (Terraform, Consul, Nomad), and InfluxDB.

gleam add duration_format
import duration_format/go
import gleam/io

pub fn main() -> Nil {
  let assert Ok(d) = go.parse("1h30m")
  io.println(go.to_string(d))
  // -> "1h30m0s"

  case go.parse("1d") {
    Ok(_) -> Nil
    Error(go.UnknownUnit(u)) -> io.println("unsupported unit: " <> u)
    Error(_) -> Nil
  }
}

Supported formats

ModuleFormat
duration_format/goGo’s time.ParseDuration grammar ("1h30m", "-2m3.4s", "500ms", …)

Each format module exposes its own parse, to_string, and Error type. go.to_string_trimmed formats like to_string but drops trailing zero components ("1h0m0s" becomes "1h"), keeping intermediate zeros intact.

Other time libraries

duration_format only handles duration string formats. For broader time work, consider:

  • gleam_time — the standard library’s core types for timestamps and durations.
  • birl — date/time handling with its own whitespace-based duration grammar ("1 hour + 30 minutes").
  • gtempo — a datetime-centric, mockable time library with parsing and formatting.
  • gtz — a timezone data provider that pairs with gtempo.
  • rada — a Date type for calendar dates without times or zones, ported from Elm’s justinmimbs/date.
  • timeago — formats timestamps as human-readable relative strings like "5 minutes ago".

Development

gleam test                       # run tests
gleam format --check src test    # check formatting

Further documentation is at https://hexdocs.pm/duration_format.

Search Document