Calendar v1.0.0 Calendar.DateTime.Format View Source
Link to this section Summary
Functions
Takes a DateTime. Returns a string with the date-time in RFC 2616 format. This format is used in the HTTP protocol. Note that the date-time will always be "shifted" to UTC.
Format as ISO 8601 extended (alias for rfc3339/1)
Format as ISO 8601 Basic
Takes datetime and returns UTC timestamp in JavaScript format. That is milliseconds since 1970 unix epoch.
Format a DateTime as an RFC 2822 timestamp.
Takes a DateTime. Returns a string with the time in RFC3339 (a profile of ISO 8601)
Takes a DateTime and a integer for number of decimals. Returns a string with the time in RFC3339 (a profile of ISO 8601)
Formats a DateTime according to RFC5545.
Format a DateTime as an RFC 822 timestamp.
Format a DateTime as an RFC 850 timestamp.
Unix time. Unix time is defined as seconds since 1970-01-01 00:00:00 UTC without leap seconds.
Like unix_time but returns a float with fractional seconds. If the microsecond of the DateTime is nil, the fractional seconds will be treated as 0.0 as seen in the second example below
Link to this section Functions
httpdate(dt) View Source
Takes a DateTime. Returns a string with the date-time in RFC 2616 format. This format is used in the HTTP protocol. Note that the date-time will always be "shifted" to UTC.
Example
# The time is 6:09 in the morning in Montevideo, but 9:09 GMT/UTC.
iex> Calendar.DateTime.from_erl!({{2014, 9, 6}, {6, 9, 8}}, "America/Montevideo") |> Calendar.DateTime.Format.httpdate
"Sat, 06 Sep 2014 09:09:08 GMT"
iso8601(dt) View Source
Format as ISO 8601 extended (alias for rfc3339/1)
iso8601_basic(dt) View Source
Format as ISO 8601 Basic
Examples
iex> Calendar.DateTime.from_erl!({{2014, 9, 26}, {20, 10, 20}}, "Etc/UTC",5) |> Calendar.DateTime.Format.iso8601_basic
"20140926T201020Z"
iex> Calendar.DateTime.from_erl!({{2014, 9, 26}, {17, 10, 20}}, "America/Montevideo",5) |> Calendar.DateTime.Format.iso8601_basic
"20140926T171020-0300"
js_ms(date_time) View Source
Takes datetime and returns UTC timestamp in JavaScript format. That is milliseconds since 1970 unix epoch.
These millisecond numbers can be used to create new Javascript Dates in JavaScript like so: new Date(1000000000985) The UTC value of the javascript date will be the same as that of the Elixir Calendar.DateTime.
Examples
iex> Calendar.DateTime.from_erl!({{2001,09,09},{03,46,40}}, "Europe/Copenhagen", 985085) |> Calendar.DateTime.Format.js_ms
1000000000985
iex> Calendar.DateTime.from_erl!({{2001,09,09},{03,46,40}}, "Europe/Copenhagen", 98508) |> Calendar.DateTime.Format.js_ms
1000000000098
rfc2822(dt) View Source
Format a DateTime as an RFC 2822 timestamp.
Examples
iex> Calendar.DateTime.from_erl!({{2010, 3, 13}, {11, 23, 03}}, "America/Los_Angeles") |> rfc2822
"Sat, 13 Mar 2010 11:23:03 -0800"
iex> Calendar.DateTime.from_erl!({{2010, 3, 13}, {11, 23, 03}}, "Etc/UTC") |> rfc2822
"Sat, 13 Mar 2010 11:23:03 +0000"
rfc3339(dt) View Source
Takes a DateTime. Returns a string with the time in RFC3339 (a profile of ISO 8601)
Examples
Without microseconds
iex> Calendar.DateTime.from_erl!({{2014, 9, 26}, {17, 10, 20}}, "America/Montevideo") |> Calendar.DateTime.Format.rfc3339
"2014-09-26T17:10:20-03:00"
With microseconds
iex> Calendar.DateTime.from_erl!({{2014, 9, 26}, {17, 10, 20, 5}}, "America/Montevideo") |> Calendar.DateTime.Format.rfc3339
"2014-09-26T17:10:20.000005-03:00"
rfc3339(dt, decimal_count) View Source
Takes a DateTime and a integer for number of decimals. Returns a string with the time in RFC3339 (a profile of ISO 8601)
The decimal_count integer defines the number fractional second digits. The decimal_count must be between 0 and 6.
Fractional seconds are not rounded up, but rather trucated.
Examples
DateTime does not have microseconds, but 3 digits of fractional seconds requested. We assume 0 microseconds and display three zeroes.
iex> Calendar.DateTime.from_erl!({{2014, 9, 26}, {17, 10, 20}}, "America/Montevideo") |> Calendar.DateTime.Format.rfc3339(3)
"2014-09-26T17:10:20.000-03:00"
DateTime has microseconds and decimal count set to 6
iex> Calendar.DateTime.from_erl!({{2014, 9, 26}, {17, 10, 20}}, "America/Montevideo",{5, 6}) |> Calendar.DateTime.Format.rfc3339(6)
"2014-09-26T17:10:20.000005-03:00"
DateTime has microseconds and decimal count set to 5
iex> Calendar.DateTime.from_erl!({{2014, 9, 26}, {17, 10, 20}}, "America/Montevideo",{5, 6}) |> Calendar.DateTime.Format.rfc3339(5)
"2014-09-26T17:10:20.00000-03:00"
DateTime has microseconds and decimal count set to 0
iex> Calendar.DateTime.from_erl!({{2014, 9, 26}, {17, 10, 20}}, "America/Montevideo",{5, 6}) |> Calendar.DateTime.Format.rfc3339(0)
"2014-09-26T17:10:20-03:00"
rfc5545(datetime) View Source
Formats a DateTime according to RFC5545.
Examples
FORM #2 is used for UTC DateTimes.
iex> {:ok, datetime, _} = "1998-01-19 07:00:00Z" |> DateTime.from_iso8601
iex> Calendar.DateTime.Format.rfc5545(datetime)
"19980119T070000Z"
FORM #3 (WITH LOCAL TIME AND TIME ZONE REFERENCE) is used for non-UTC datetimes
iex> {:ok, cph_datetime} = Calendar.DateTime.from_naive(~N[2001-09-09T03:46:40.985085], "Europe/Copenhagen")
iex> Calendar.DateTime.Format.rfc5545(cph_datetime)
"TZID=Europe/Copenhagen:20010909T034640.985085"
iex> {:ok, ny_datetime} = Calendar.DateTime.from_naive(~N[1998-01-19T02:00:00], "America/New_York")
iex> Calendar.DateTime.Format.rfc5545(ny_datetime)
"TZID=America/New_York:19980119T020000"
rfc822(dt) View Source
Format a DateTime as an RFC 822 timestamp.
Note that this format is old and uses only 2 digits to denote the year!
Examples
iex> Calendar.DateTime.from_erl!({{2010, 3, 13}, {11, 23, 03}}, "America/Los_Angeles") |> rfc822
"Sat, 13 Mar 10 11:23:03 -0800"
iex> Calendar.DateTime.from_erl!({{2010, 3, 13}, {11, 23, 03}}, "Etc/UTC") |> rfc822
"Sat, 13 Mar 10 11:23:03 +0000"
rfc850(dt) View Source
Format a DateTime as an RFC 850 timestamp.
Note that this format is old and uses only 2 digits to denote the year!
Examples
iex> Calendar.DateTime.from_erl!({{2010, 3, 13}, {11, 23, 03}}, "America/Los_Angeles") |> rfc850
"Sat, 13-Mar-10 11:23:03 PST"
unix(dt) View Source
Unix time. Unix time is defined as seconds since 1970-01-01 00:00:00 UTC without leap seconds.
Examples
iex> Calendar.DateTime.from_erl!({{2001,09,09},{03,46,40}}, "Europe/Copenhagen", {55, 2}) |> Calendar.DateTime.Format.unix
1_000_000_000
unix_micro(date_time) View Source
Like unix_time but returns a float with fractional seconds. If the microsecond of the DateTime is nil, the fractional seconds will be treated as 0.0 as seen in the second example below:
Examples
iex> Calendar.DateTime.from_erl!({{2001,09,09},{03,46,40}}, "Europe/Copenhagen", {985085, 6}) |> Calendar.DateTime.Format.unix_micro
1_000_000_000.985085
iex> Calendar.DateTime.from_erl!({{2001,09,09},{03,46,40}}, "Europe/Copenhagen") |> Calendar.DateTime.Format.unix_micro
1_000_000_000.0