Millisecond (Millisecond v0.1.0) View Source

A tiny library to parse human readable formats into milliseconds.

Link to this section Summary

Functions

A tiny library to parse human readable formats into milliseconds to make configurations easier.

Adds the Millisecond to a DateTime to produce a future datetime.

Converts a string format into milliseconds.

This is ms/1 but returns milliseconds directly on success and raises an error otherwise.

Converts a string to a Millisecond struct.

This is parse/1 but returns the data directly on success and raises an error otherwise.

Subtracts the Millisecond from a DateTime to produce a past datetime.

Converts an Millisecond struct into a Timex.Duration struct.

Converts a Millisecond struct to the intended milliseconds format.

Link to this section Types

Specs

millisecond() :: pos_integer()

Specs

t() :: %Millisecond{
  day: number(),
  hour: number(),
  millisecond: number(),
  minute: number(),
  month: number(),
  second: number(),
  week: number(),
  year: number()
}

Link to this section Functions

A tiny library to parse human readable formats into milliseconds to make configurations easier.

Examples

iex> Millisecond.ms("100")
{:ok, 100}

iex> Millisecond.ms("1s")
{:ok, 1_000}

iex> Millisecond.ms!("1s")
1_000

iex> Millisecond.ms("1m")
{:ok, 60_000}

iex> Millisecond.ms("1.5m")
{:ok, 9.0e4}

iex> Millisecond.ms("-0.5m")
{:ok, -3.0e4}

iex> Millisecond.ms("1h")
{:ok, 3_600_000}

iex> Millisecond.ms("1h 1m 1s")
{:ok, 3_661_000}

iex> Millisecond.ms("1y 1mo 1d")
{:ok, 34_236_000_000}

iex> Millisecond.ms("RANDOM STRING")
:error

iex> Millisecond.ms!("1nvalid")
** (ArgumentError) Format is invalid: "1nvalid"

iex> Millisecond.ms("1d 1mo 1y")
:error

iex> Millisecond.ms("1hour 1minute 1second")
{:ok, 3_661_000}

iex> Millisecond.ms("1   minutes     1 milliseconds")
{:ok, 60001}

iex> Millisecond.ms("1year 1month 1day")
{:ok, 34_236_000_000}

Specs

add(DateTime.t(), t()) :: DateTime.t()

Adds the Millisecond to a DateTime to produce a future datetime.

THis is an example of its intended use for configuration.

Examples

iex> ms = Millisecond.parse!("100ms") iex> now = DateTime.utc_now() iex> now |> Millisecond.add(ms) |> DateTime.diff(now, :millisecond) 100

Specs

ms(charlist()) :: {:ok, millisecond()} | :error

Converts a string format into milliseconds.

The main function of this library.

Example

iex> import Millisecond, only: [ms: 1, ms!: 1]
iex> ms!('2 days')
172800000
iex> ms!('1d')
86400000
iex> ms!('10h')
36000000
iex> ms!('2.5 hrs')
9000000
iex> ms!('2h')
7200000
iex> ms!('1m')
60000
iex> ms!('5s')
5000
iex> ms!('1y')
31557600000
iex> ms!('100')
100
iex> ms!('-3 days')
-259200000
iex> ms!('-1h')
-3600000
iex> ms!('-200')
-200

Specs

ms!(charlist()) :: millisecond()

This is ms/1 but returns milliseconds directly on success and raises an error otherwise.

Specs

parse(charlist()) :: {:ok, t()} | :error

Converts a string to a Millisecond struct.

This is intended to be an low-level function to mainly separate parsing and conversion.

Examples

iex> Millisecond.parse("1h 1m 1s")
{:ok, %Millisecond{hour: 1, minute: 1, second: 1}}
iex> Millisecond.parse("invalid format")
:error

Specs

parse!(charlist()) :: t()

This is parse/1 but returns the data directly on success and raises an error otherwise.

Link to this function

subtract(datetime, data)

View Source

Specs

subtract(DateTime.t(), t()) :: DateTime.t()

Subtracts the Millisecond from a DateTime to produce a past datetime.

THis is an example of its intended use for configuration.

Examples

iex> ms = Millisecond.parse!("100ms") iex> now = DateTime.utc_now() iex> now |> Millisecond.subtract(ms) |> DateTime.diff(now, :millisecond) -100

Specs

to_duration(t()) :: Timex.Duration.t()

Converts an Millisecond struct into a Timex.Duration struct.

This is inteded to show that Millisecond can be converted into a more appropriate date/time struct such Timex.Duration. If you have Timex already, perhaps use Timex.Duration.parse/1 as it uses the ISO 8601 Duration format.

Examples

iex> {:ok, data} = Millisecond.parse("1h 1s") iex> duration = Millisecond.to_duration(data) iex> Timex.Duration.to_string(duration) "PT1H1S"

iex> {:ok, data} = Millisecond.parse("1d 1h") iex> {:ok, duration} = Timex.Duration.parse("P1DT1H") iex> Millisecond.to_duration(data) == duration true

Specs

to_milliseconds(t()) :: millisecond()

Converts a Millisecond struct to the intended milliseconds format.

This is intended to be an low-level function to mainly separate parsing and conversion.

Examples

iex> data = Millisecond.parse!("1h 1m 1s")
iex> Millisecond.to_milliseconds(data)
3_661_000

iex> data = Millisecond.parse!("1y 1mo 1d")
iex> Millisecond.to_milliseconds(data)
34_236_000_000