chronic v3.0.0 Chronic

Chronic is a Pure Elixir natural language parser for times and dates

Summary

Functions

Parses the specified time. Will return {:ok, time, utc_offset} if it knows a time, otherwise {:error, :unknown_format}

Functions

parse(time, opts \\ [])

Parses the specified time. Will return {:ok, time, utc_offset} if it knows a time, otherwise {:error, :unknown_format}

## Examples

ISO8601 times will return an offset if one is specified:

iex> Chronic.parse("2012-08-02T13:00:00")
{ :ok, %NaiveDateTime{day: 2, hour: 13, minute: 0, month: 8, second: 0, microsecond: {0,0}, year: 2012}, nil }

iex> Chronic.parse("2012-08-02T13:00:00+01:00")
{ :ok, %NaiveDateTime{day: 2, hour: 13, minute: 0, month: 8, second: 0, microsecond: {0,0}, year: 2012}, 3600 }

You can pass an option to define the “current” time for Chronic:

iex> Chronic.parse("aug 2", currently: {{1999, 1, 1}, {0,0,0}})
{ :ok, %NaiveDateTime{day: 2, hour: 0, minute: 0, month: 8, second: 0, microsecond: {0, 6}, year: 1999}, 0 }

All examples here use currently so that they are not affected by the passing of time. You may leave the currently option off.

iex> Chronic.parse("aug 2 9am", currently: {{2016, 1, 1}, {0,0,0}})
{ :ok, %NaiveDateTime{day: 2, hour: 9, minute: 0, month: 8, second: 0, microsecond: {0, 6}, year: 2016}, 0 }

iex> Chronic.parse("aug 2 9:15am", currently: {{2016, 1, 1}, {0,0,0}})
{ :ok, %NaiveDateTime{day: 2, hour: 9, minute: 15, month: 8, second: 0, microsecond: {0, 6}, year: 2016}, 0 }

iex> Chronic.parse("aug 2nd 9:15am", currently: {{2016, 1, 1}, {0,0,0}})
{ :ok, %NaiveDateTime{day: 2, hour: 9, minute: 15, month: 8, second: 0, microsecond: {0, 6}, year: 2016}, 0 }

iex> Chronic.parse("aug. 2nd 9:15am", currently: {{2016, 1, 1}, {0,0,0}})
{ :ok, %NaiveDateTime{day: 2, hour: 9, minute: 15, month: 8, second: 0, microsecond: {0, 6}, year: 2016}, 0 }

iex> Chronic.parse("2 aug 9:15am", currently: {{2016, 1, 1}, {0,0,0}})
{ :ok, %NaiveDateTime{day: 2, hour: 9, minute: 15, month: 8, second: 0, microsecond: {0, 6}, year: 2016}, 0 }

iex> Chronic.parse("2 aug at 9:15am", currently: {{2016, 1, 1}, {0,0,0}})
{ :ok, %NaiveDateTime{day: 2, hour: 9, minute: 15, month: 8, second: 0, microsecond: {0, 6}, year: 2016}, 0 }

iex> Chronic.parse("2nd of aug 9:15am", currently: {{2016, 1, 1}, {0,0,0}})
{ :ok, %NaiveDateTime{day: 2, hour: 9, minute: 15, month: 8, second: 0, microsecond: {0, 6}, year: 2016}, 0 }

iex> Chronic.parse("2nd of aug at 9:15am", currently: {{2016, 1, 1}, {0,0,0}})
{ :ok, %NaiveDateTime{day: 2, hour: 9, minute: 15, month: 8, second: 0, microsecond: {0,6}, year: 2016}, 0 }

iex> Chronic.parse("2nd of aug at 9:15 am", currently: {{2016, 1, 1}, {0,0,0}})
{ :ok, %NaiveDateTime{day: 2, hour: 9, minute: 15, month: 8, second: 0, microsecond: {0,6}, year: 2016}, 0 }

iex> Chronic.parse("Feb 29", currently: {{2016, 1, 1}, {0,0,0}})
{ :ok, %NaiveDateTime{day: 29, hour: 0, minute: 0, month: 2, second: 0, microsecond: {0,6}, year: 2016}, 0 }

iex> Chronic.parse("Feb 29", currently: {{2017, 1, 1}, {0,0,0}})
{ :error, :invalid_datetime }

iex> Chronic.parse("Nov 31")
{ :error, :invalid_datetime }