DayEx.Duration (day_ex v1.10.115)

Copy Markdown View Source

Represents a duration of time with decomposed fields.

Supports construction from milliseconds, maps, or ISO 8601 duration strings.

Summary

Functions

Adds two durations field-wise.

Returns total duration in days.

Returns total duration in hours.

Returns total duration in milliseconds.

Returns total duration in minutes.

Returns total duration in months.

Returns total duration in seconds.

Returns total duration in weeks.

Returns total duration in years.

Returns a human-readable string for the duration.

Create a Duration from milliseconds (integer), a map of fields, or an ISO 8601 duration string.

Subtracts two durations field-wise.

Returns the duration as an ISO 8601 duration string.

Types

t()

@type t() :: %DayEx.Duration{
  days: integer(),
  hours: integer(),
  milliseconds: integer(),
  minutes: integer(),
  months: integer(),
  seconds: integer(),
  years: integer()
}

Functions

add(a, b)

@spec add(t(), t()) :: t()

Adds two durations field-wise.

as_days(d)

@spec as_days(t()) :: float()

Returns total duration in days.

as_hours(d)

@spec as_hours(t()) :: float()

Returns total duration in hours.

as_milliseconds(d)

@spec as_milliseconds(t()) :: number()

Returns total duration in milliseconds.

as_minutes(d)

@spec as_minutes(t()) :: float()

Returns total duration in minutes.

as_months(d)

@spec as_months(t()) :: float()

Returns total duration in months.

as_seconds(d)

@spec as_seconds(t()) :: float()

Returns total duration in seconds.

as_weeks(d)

@spec as_weeks(t()) :: float()

Returns total duration in weeks.

as_years(d)

@spec as_years(t()) :: float()

Returns total duration in years.

days(duration)

@spec days(t()) :: integer()

hours(duration)

@spec hours(t()) :: integer()

humanize(d, with_suffix \\ false)

@spec humanize(t(), boolean()) :: String.t()

Returns a human-readable string for the duration.

When with_suffix is true, prefixes positive durations with "in ".

milliseconds(duration)

@spec milliseconds(t()) :: integer()

minutes(duration)

@spec minutes(t()) :: integer()

months(duration)

@spec months(t()) :: integer()

new(ms)

@spec new(integer() | map() | String.t()) :: t()

Create a Duration from milliseconds (integer), a map of fields, or an ISO 8601 duration string.

From milliseconds: decomposes into hours/minutes/seconds/milliseconds only (no calendar days). From map: direct field assignment with defaults of 0. From ISO string: parses "P[nY][nM][nD][T[nH][nM][nS]]".

seconds(duration)

@spec seconds(t()) :: integer()

subtract(a, b)

@spec subtract(t(), t()) :: t()

Subtracts two durations field-wise.

to_iso_string(d)

@spec to_iso_string(t()) :: String.t()

Returns the duration as an ISO 8601 duration string.

Zero duration returns "P0D".

valid?(duration)

@spec valid?(term()) :: boolean()

years(duration)

@spec years(t()) :: integer()