Kalends.DateTime.Parse
Summary
httpdate(rfc2616_string) | Parses a timestamp in RFC 2616 format |
rfc3339(rfc3339_string, time_zone) | Parses an RFC 3339 timestamp and shifts it to the specified time zone |
rfc3339_utc(rfc3339_string) | Parse RFC 3339 timestamp strings as UTC. If the timestamp is not in UTC it will be shifted to UTC |
Functions
Parses a timestamp in RFC 2616 format.
iex> httpdate("Sat, 06 Sep 2014 09:09:08 GMT")
{:ok, %Kalends.DateTime{year: 2014, month: 9, day: 6, hour: 9, min: 9, sec: 8, timezone: "UTC", abbr: "UTC", std_off: 0, utc_off: 0}}
iex> httpdate("invalid")
{:bad_format, nil}
iex> httpdate("Foo, 06 Foo 2014 09:09:08 GMT")
{:error, :invalid_datetime}
Parses an RFC 3339 timestamp and shifts it to the specified time zone.
iex> rfc3339("1996-12-19T16:39:57Z", "UTC")
{:ok, %Kalends.DateTime{year: 1996, month: 12, day: 19, hour: 16, min: 39, sec: 57, timezone: "UTC", abbr: "UTC", std_off: 0, utc_off: 0}}
iex> rfc3339("1996-12-19T16:39:57.1234Z", "UTC")
{:ok, %Kalends.DateTime{year: 1996, month: 12, day: 19, hour: 16, min: 39, sec: 57, timezone: "UTC", abbr: "UTC", std_off: 0, utc_off: 0, frac_sec: 0.1234}}
iex> rfc3339("1996-12-19T16:39:57-8:00", "America/Los_Angeles")
{:ok, %Kalends.DateTime{abbr: "PST", day: 19, hour: 16, min: 39, month: 12, sec: 57, std_off: 0, timezone: "America/Los_Angeles", utc_off: -28800, year: 1996}}
iex> rfc3339("invalid", "America/Los_Angeles")
{:bad_format, nil}
iex> rfc3339("1996-12-19T16:39:57-08:00", "invalid time zone name")
{:invalid_time_zone, nil}
Parse RFC 3339 timestamp strings as UTC. If the timestamp is not in UTC it will be shifted to UTC.
Examples
iex> rfc3339_utc("fooo")
{:bad_format, nil}
iex> rfc3339_utc("1996-12-19T16:39:57Z")
{:ok, %Kalends.DateTime{year: 1996, month: 12, day: 19, hour: 16, min: 39, sec: 57, timezone: "UTC", abbr: "UTC", std_off: 0, utc_off: 0}}
iex> rfc3339_utc("1996-12-19T16:39:57-08:00")
{:ok, %Kalends.DateTime{year: 1996, month: 12, day: 20, hour: 0, min: 39, sec: 57, timezone: "UTC", abbr: "UTC", std_off: 0, utc_off: 0}}