Calendar v1.0.0 Calendar.DateTime.Parse View Source

Link to this section Summary

Functions

Parses a timestamp in RFC 2616 format.

Like httpdate/1, but returns the result without tagging it with :ok in case of success. In case of errors it raises.

Parse JavaScript style milliseconds since epoch.

Parses an RFC 2822 or RFC 1123 datetime string.

Parses an RFC 3339 timestamp and shifts it to the specified time zone.

Parse RFC 3339 timestamp strings as UTC. If the timestamp is not in UTC it will be shifted to UTC.

Parses an RFC 5545 datetime string of FORM #2 (UTC) or #3 (with time zone identifier)

Parses an RFC 822 datetime string and shifts it to UTC.

Takes unix time as an integer or float. Returns a DateTime struct.

Link to this section Functions

Link to this function

httpdate(rfc2616_string) View Source

Parses a timestamp in RFC 2616 format.

iex> httpdate("Sat, 06 Sep 2014 09:09:08 GMT")
{:ok, %DateTime{year: 2014, month: 9, day: 6, hour: 9, minute: 9, second: 8, time_zone: "Etc/UTC", zone_abbr: "UTC", std_offset: 0, utc_offset: 0, microsecond: {0, 0}}}

iex> httpdate("invalid")
{:bad_format, nil}

iex> httpdate("Foo, 06 Foo 2014 09:09:08 GMT")
{:error, :invalid_datetime}
Link to this function

httpdate!(rfc2616_string) View Source

Like httpdate/1, but returns the result without tagging it with :ok in case of success. In case of errors it raises.

iex> httpdate!("Sat, 06 Sep 2014 09:09:08 GMT")
%DateTime{year: 2014, month: 9, day: 6, hour: 9, minute: 9, second: 8, time_zone: "Etc/UTC", zone_abbr: "UTC", std_offset: 0, utc_offset: 0}

Parse JavaScript style milliseconds since epoch.

Examples

iex> js_ms!("1000000000123")
%DateTime{zone_abbr: "UTC", day: 9, microsecond: {123000,3}, hour: 1, minute: 46, month: 9, second: 40, std_offset: 0, time_zone: "Etc/UTC", utc_offset: 0, year: 2001}

iex> js_ms!(1_000_000_000_123)
%DateTime{zone_abbr: "UTC", day: 9, microsecond: {123000,3}, hour: 1, minute: 46, month: 9, second: 40, std_offset: 0, time_zone: "Etc/UTC", utc_offset: 0, year: 2001}

iex> js_ms!(1424102000000)
%DateTime{zone_abbr: "UTC", day: 16, hour: 15, microsecond: {0, 3}, minute: 53, month: 2, second: 20, std_offset: 0, time_zone: "Etc/UTC", utc_offset: 0, year: 2015}

Parses an RFC 2822 or RFC 1123 datetime string.

The datetime is shifted to UTC.

Examples

iex> rfc2822_utc("Sat, 13 Mar 2010 11:23:03 -0800")
{:ok,
      %DateTime{zone_abbr: "UTC", day: 13, hour: 19, minute: 23, month: 3, second: 3, std_offset: 0,
       time_zone: "Etc/UTC", microsecond: {0, 0}, utc_offset: 0, year: 2010}}

# PST is the equivalent of -0800 in the RFC 2822 standard
iex> rfc2822_utc("Sat, 13 Mar 2010 11:23:03 PST")
{:ok,
      %DateTime{zone_abbr: "UTC", day: 13, hour: 19, minute: 23, month: 3, second: 3, std_offset: 0,
       time_zone: "Etc/UTC", microsecond: {0, 0}, utc_offset: 0, year: 2010}}

# Z is the equivalent of UTC
iex> rfc2822_utc("Sat, 13 Mar 2010 11:23:03 Z")
{:ok,
      %DateTime{zone_abbr: "UTC", day: 13, hour: 11, minute: 23, month: 3, second: 3, std_offset: 0,
       time_zone: "Etc/UTC", microsecond: {0, 0}, utc_offset: 0, year: 2010}}
Link to this function

rfc3339(rfc3339_string, time_zone) View Source

Parses an RFC 3339 timestamp and shifts it to the specified time zone.

iex> rfc3339("1996-12-19T16:39:57Z", "Etc/UTC")
{:ok, %DateTime{year: 1996, month: 12, day: 19, hour: 16, minute: 39, second: 57, time_zone: "Etc/UTC", zone_abbr: "UTC", std_offset: 0, utc_offset: 0}}

iex> rfc3339("1996-12-19T16:39:57.1234Z", "Etc/UTC")
{:ok, %DateTime{year: 1996, month: 12, day: 19, hour: 16, minute: 39, second: 57, time_zone: "Etc/UTC", zone_abbr: "UTC", std_offset: 0, utc_offset: 0, microsecond: {123400, 4}}}

iex> rfc3339("1996-12-19T16:39:57-8:00", "America/Los_Angeles")
{:ok, %DateTime{zone_abbr: "PST", day: 19, hour: 16, minute: 39, month: 12, second: 57, std_offset: 0, time_zone: "America/Los_Angeles", utc_offset: -28800, year: 1996}}

iex> rfc3339("1996-12-19T16:39:57.1234-8:00", "America/Los_Angeles")
{:ok, %DateTime{zone_abbr: "PST", day: 19, hour: 16, minute: 39, month: 12, second: 57, std_offset: 0, time_zone: "America/Los_Angeles", utc_offset: -28800, year: 1996, microsecond: {123400, 4}}}

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}
Link to this function

rfc3339_utc(rfc3339_string) View Source

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:57")
{:bad_format, nil}

iex> rfc3339_utc("1996-12-19T16:39:57Z")
{:ok, %DateTime{year: 1996, month: 12, day: 19, hour: 16, minute: 39, second: 57, time_zone: "Etc/UTC", zone_abbr: "UTC", std_offset: 0, utc_offset: 0}}

iex> rfc3339_utc("1996-12-19T16:39:57.123Z")
{:ok, %DateTime{year: 1996, month: 12, day: 19, hour: 16, minute: 39, second: 57, time_zone: "Etc/UTC", zone_abbr: "UTC", std_offset: 0, utc_offset: 0, microsecond: {123000, 3}}}

iex> rfc3339_utc("1996-12-19T16:39:57,123Z")
{:ok, %DateTime{year: 1996, month: 12, day: 19, hour: 16, minute: 39, second: 57, time_zone: "Etc/UTC", zone_abbr: "UTC", std_offset: 0, utc_offset: 0, microsecond: {123000, 3}}}

iex> rfc3339_utc("1996-12-19T16:39:57-08:00")
{:ok, %DateTime{year: 1996, month: 12, day: 20, hour: 0, minute: 39, second: 57, time_zone: "Etc/UTC", zone_abbr: "UTC", std_offset: 0, utc_offset: 0}}

# No seperation chars between numbers. Not RFC3339, but we still parse it.
iex> rfc3339_utc("19961219T163957-08:00")
{:ok, %DateTime{year: 1996, month: 12, day: 20, hour: 0, minute: 39, second: 57, time_zone: "Etc/UTC", zone_abbr: "UTC", std_offset: 0, utc_offset: 0}}

# Offset does not have colon (-0800). That makes it ISO8601, but not RFC3339. We still parse it.
iex> rfc3339_utc("1996-12-19T16:39:57-0800")
{:ok, %DateTime{year: 1996, month: 12, day: 20, hour: 0, minute: 39, second: 57, time_zone: "Etc/UTC", zone_abbr: "UTC", std_offset: 0, utc_offset: 0}}

Parses an RFC 5545 datetime string of FORM #2 (UTC) or #3 (with time zone identifier)

Examples

FORM #2 with a Z at the end to indicate UTC

iex> rfc5545("19980119T020321Z")
{:ok, %DateTime{calendar: Calendar.ISO, day: 19, hour: 2, microsecond: {0, 0}, minute: 3, month: 1, second: 21, std_offset: 0, time_zone: "Etc/UTC", utc_offset: 0, year: 1998, zone_abbr: "UTC"}}

FORM #3 has a time zone identifier

iex> rfc5545("TZID=America/New_York:19980119T020000")
{:ok, %DateTime{calendar: Calendar.ISO, day: 19, hour: 2, microsecond: {0, 0}, minute: 0, month: 1, second: 0, std_offset: 0, time_zone: "America/New_York", utc_offset: -18000, year: 1998, zone_abbr: "EST"}}

From RFC 5455: "If, based on the definition of the referenced time zone, the local time described occurs more than once (when changing from daylight to standard time), the DATE-TIME value refers to the first occurrence of the referenced time. Thus, TZID=America/New_York:20071104T013000 indicates November 4, 2007 at 1:30 A.M. EDT (UTC-04:00)."

iex> rfc5545("TZID=America/New_York:20071104T013000")
{:ok, %DateTime{calendar: Calendar.ISO,
         day: 4, hour: 1, microsecond: {0, 0}, minute: 30, month: 11, second: 0,
         std_offset: 3600, time_zone: "America/New_York", utc_offset: -18000, year: 2007,
         zone_abbr: "EDT"}}

iex> rfc5545("TZID=America/New_York:19980119T020000.123456")
{:ok, %DateTime{calendar: Calendar.ISO, day: 19, hour: 2, microsecond: {123456, 6}, minute: 0, month: 1, second: 0, std_offset: 0, time_zone: "America/New_York", utc_offset: -18000, year: 1998, zone_abbr: "EST"}}

RFC 5545 : "If the local time described does not occur (when

changing from standard to daylight time), the DATE-TIME value is
interpreted using the UTC offset before the gap in local times.
Thus, TZID=America/New_York:20070311T023000 indicates March 11,
2007 at 3:30 A.M. EDT (UTC-04:00), one hour after 1:30 A.M. EST
(UTC-05:00)."

The way this is implemented: When there is a gap for "spring forward" the difference between the two offsets before and after is added. E.g. usually the difference in offset between summer and winter time is one hour. Then one hour is added.

iex> rfc5545("TZID=America/New_York:20070311T023000")
{:ok, %DateTime{calendar: Calendar.ISO,
         day: 11, hour: 3, microsecond: {0, 0}, minute: 30, month: 3, second: 0,
         std_offset: 3600, time_zone: "America/New_York", utc_offset: -18000, year: 2007,
         zone_abbr: "EDT"}}
Link to this function

rfc822_utc(string, year_guessing_base \\ 2015) View Source

Parses an RFC 822 datetime string and shifts it to UTC.

Takes an RFC 822 string and year_guessing_base. The year_guessing_base argument is used in case of a two digit year which is allowed in RFC 822. The function tries to guess possible four digit versions of the year and chooses the one closest to year_guessing_base. It defaults to 2015.

Examples

# 2 digit year
iex> "5 Jul 15 20:26:13 PST" |> rfc822_utc
{:ok,
      %DateTime{zone_abbr: "UTC", day: 6, hour: 4, minute: 26, month: 7,
       second: 13, std_offset: 0, time_zone: "Etc/UTC", microsecond: {0, 0}, utc_offset: 0,
       year: 2015}}
# 82 as year
iex> "5 Jul 82 20:26:13 PST" |> rfc822_utc
{:ok,
      %DateTime{zone_abbr: "UTC", day: 6, hour: 4, minute: 26, month: 7,
       second: 13, std_offset: 0, time_zone: "Etc/UTC", microsecond: {0, 0}, utc_offset: 0,
       year: 1982}}
# 1982 as year
iex> "5 Jul 82 20:26:13 PST" |> rfc822_utc
{:ok,
      %DateTime{zone_abbr: "UTC", day: 6, hour: 4, minute: 26, month: 7,
       second: 13, std_offset: 0, time_zone: "Etc/UTC", microsecond: {0, 0}, utc_offset: 0,
       year: 1982}}
# 2 digit year and we use 2099 as the base guessing year
# which means that 15 should be interpreted as 2115 no 2015
iex> "5 Jul 15 20:26:13 PST" |> rfc822_utc(2099)
{:ok,
      %DateTime{zone_abbr: "UTC", day: 6, hour: 4, minute: 26, month: 7,
       second: 13, std_offset: 0, time_zone: "Etc/UTC", microsecond: {0, 0}, utc_offset: 0,
       year: 2115}}

Takes unix time as an integer or float. Returns a DateTime struct.

Examples

iex> unix!(1_000_000_000)
%DateTime{zone_abbr: "UTC", day: 9, microsecond: {0, 0}, hour: 1, minute: 46, month: 9, second: 40, std_offset: 0, time_zone: "Etc/UTC", utc_offset: 0, year: 2001}

iex> unix!("1000000000")
%DateTime{zone_abbr: "UTC", day: 9, microsecond: {0, 0}, hour: 1, minute: 46, month: 9, second: 40, std_offset: 0, time_zone: "Etc/UTC", utc_offset: 0, year: 2001}

iex> unix!("1000000000.010")
%DateTime{zone_abbr: "UTC", day: 9, microsecond: {10_000, 3}, hour: 1, minute: 46, month: 9, second: 40, std_offset: 0, time_zone: "Etc/UTC", utc_offset: 0, year: 2001}

iex> unix!(1_000_000_000.9876)
%DateTime{zone_abbr: "UTC", day: 9, microsecond: {987600, 6}, hour: 1, minute: 46, month: 9, second: 40, std_offset: 0, time_zone: "Etc/UTC", utc_offset: 0, year: 2001}

iex> unix!(1_000_000_000.999999)
%DateTime{zone_abbr: "UTC", day: 9, microsecond: {999999, 6}, hour: 1, minute: 46, month: 9, second: 40, std_offset: 0, time_zone: "Etc/UTC", utc_offset: 0, year: 2001}