enkidatron/elm-cldr/3.2.1

Cldr.Format.Duration

Duration Formatting

type alias Duration =
{ years : Int
, months : Int
, weeks : Int
, days : Int
, hours : Int
, minutes : Int
, seconds : Int
, milliseconds : Int
}

A duration that can be formatted.

To be consistent with Intl.DurationFormat, years are the largest units. This only accepts integers for the various units, which is consistent with passing an object into Intl.DurationFormat. The browser's Intl.DurationFormat can also accept an ISO8601 duration string directly (which can have decimals in the numbers), although JS has the "luxury" of simply throwing an error if you provide a combination of decimals that it doesn't know how to handle. To use this library, you have to turn "1.5 hours" into {zero | hours = 1, minutes = 30} yourself.

zero : Duration

An empty duration. All units are set to 0.

type Length
= Narrow
| Short
| Long

There are three lengths that can be used when formatting a duration.

format : Length -> Locale -> Duration -> String

Formats a Duration using the specified Length and Locale

import Cldr.Locale exposing (en)


format Long en { zero | hours = 1, minutes = 30}
--> "1 hour, 30 minutes"
= Standard
| TrimLargest
| TrimSmallest
| TrimLargestAndSmallest
| IncludeUnitsIfZero UnitsToDisplay

These are the different options for which units to display when formatting a duration. Standard only displays units that have a non-zero value. TrimLargest starts from the largest (years) and removes units until it finds a non-zero value or there is only one value left. TrimSmallest starts from the smallest (milliseconds) and removes units until it finds a non-zero value or there is only one value left. TrimLargestAndSmallest does TrimLargest and then TrimSmallest. IncludeUnitsIfZero includes all units that are not zero, and also all units marked as True in the UnitsToDisplay record.

type alias UnitsToDisplay =
{ years : Bool
, months : Bool
, weeks : Bool
, days : Bool
, hours : Bool
, minutes : Bool
, seconds : Bool
, milliseconds : Bool
}

If you want to display specific units in your duration

formatWithOptions : UnitDisplayOptions -> Length -> Locale -> Duration -> String

Formats a Duration using the specified UnitDisplayOptions, Length, and Locale

import Cldr.Locale exposing (en)


formatWithOptions Standard Long en { zero | hours = 1, minutes = 30}
--> "1 hour, 30 minutes"
formatWithOptions TrimLargest Long en { zero | hours = 1, minutes = 30}
--> "1 hour, 30 minutes, 0 seconds, 0 milliseconds"
formatWithOptions TrimLargestAndSmallest Long en zero
--> "0 milliseconds"