duration_format
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_formatimport 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
| Module | Format |
|---|---|
duration_format/go |
Go's time.ParseDuration grammar ("1h30m", "-2m3.4s", "500ms", …) |
Each format module exposes its own parse, to_string, and Error type.
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 withgtempo.rada— a Date type for calendar dates without times or zones, ported from Elm'sjustinmimbs/date.timeago— formats timestamps as human-readable relative strings like"5 minutes ago".
Development
gleam test # run tests
gleam format --check src test # check formattingFurther documentation is at https://hexdocs.pm/duration_format.