Calendar.DateTime
DateTime provides a struct which represents a certain time and date in a certain time zone.
The functions in this module can be used to create and transform DateTime structs.
Summary
Functions
Takes a DateTime and an integer. Returns the date_time
advanced by the number of seconds found in the seconds
argument
Like advance
without exclamation points. Instead of returning a tuple with :ok and the result, the result is returned untagged. Will raise an error in case no correct result can be found based on the arguments
Takes a two DateTime
s and returns true if the first one is greater than the second. Otherwise false. Greater than means that it is later then the second datetime
Takes a two DateTime
s and returns true if the first one is less than the second. Otherwise false. Less than means that it is earlier then the second datetime
The difference between two DateTime structs. In seconds and microseconds
Create new DateTime struct based on a date and a time and a timezone string
Like from_date_and_time_and_zone
, but returns result untagged and raises in case of an error or ambiguous datetime
Takes an Erlang-style date-time tuple and additionally a timezone name. Returns a tuple with a tag and a DateTime struct
Like from_erl/2 without "!", but returns the result directly without a tag. Will raise if date is ambiguous or invalid! Only use this if you are sure the date is valid. Otherwise use "from_erl" without the "!"
Like from_erl, but also takes an argument with the total UTC offset. (Total offset is standard offset + UTC offset)
Like from_erl_total_off/4
but takes a 7 element datetime tuple with microseconds instead of a "normal" erlang style tuple
Takes an NaiveDateTime and a time zone identifier and returns a DateTime
Takes a DateTime and returns an integer of gregorian seconds starting with year 0. This is done via the Erlang calendar module
Takes a timezone name a returns a DateTime with the current time in that timezone. Timezone names must be in the TZ data format
Like DateTime.now!("Etc/UTC")
Takes a two DateTime
s and returns true if the first is at the same time as the second one
Takes a DateTime and the name of a new timezone. Returns a DateTime with the equivalent time in the new timezone
Like shift_zone without "!", but does not check that the time zone is valid and just returns a DateTime struct instead of a tuple with a tag
Takes a DateTime struct and returns a Date struct representing the date part of the provided DateTime
Returns a tuple with a Date struct and a Time struct
Takes a DateTime struct and returns an erlang style datetime tuple
Takes a DateTime struct and returns an Ecto style datetime tuple. This is like an erlang style tuple, but with microseconds added as an additional element in the time part of the tuple
Takes a DateTime and returns a NaiveDateTime
Takes a DateTime struct and returns a Time struct representing the time part of the provided DateTime
Functions
Takes a DateTime and an integer. Returns the date_time
advanced by the number of seconds found in the seconds
argument.
If seconds
is negative, the time is moved back.
The advancement is done in UTC. The datetime is converted to UTC, then advanced, then converted back.
NOTE: this ignores leap seconds. The calculation is based on the (wrong) assumption that there are no leap seconds.
Examples
# Advance 2 seconds
iex> from_erl!({{2014,10,2},{0,29,10}}, "America/New_York",123456) |> advance(2)
{:ok, %Calendar.DateTime{abbr: "EDT", day: 2, hour: 0, min: 29, month: 10,
sec: 12, std_off: 3600, timezone: "America/New_York", usec: 123456,
utc_off: -18000, year: 2014}}
# Advance 86400 seconds (one day)
iex> from_erl!({{2014,10,2},{0,29,10}}, "America/New_York",123456) |> advance(86400)
{:ok, %Calendar.DateTime{abbr: "EDT", day: 3, hour: 0, min: 29, month: 10,
sec: 10, std_off: 3600, timezone: "America/New_York", usec: 123456,
utc_off: -18000, year: 2014}}
# Go back 62 seconds
iex> from_erl!({{2014,10,2},{0,0,0}}, "America/New_York",123456) |> advance(-62)
{:ok, %Calendar.DateTime{abbr: "EDT", day: 1, hour: 23, min: 58, month: 10,
sec: 58, std_off: 3600, timezone: "America/New_York", usec: 123456, utc_off: -18000,
year: 2014}}
# Advance 10 seconds just before DST "spring forward" so we go from 1:59:59 to 3:00:09
iex> from_erl!({{2015,3,8},{1,59,59}}, "America/New_York",123456) |> advance(10)
{:ok, %Calendar.DateTime{abbr: "EDT", day: 8, hour: 3, min: 0, month: 3,
sec: 9, std_off: 3600, timezone: "America/New_York", usec: 123456,
utc_off: -18000, year: 2015}}
# Go back too far so that year would be before 0
iex> from_erl!({{2014,10,2},{0,0,0}}, "America/New_York",123456) |> advance(-999999999999)
{:error, :function_clause_error}
Like advance
without exclamation points. Instead of returning a tuple with :ok and the result, the result is returned untagged. Will raise an error in case no correct result can be found based on the arguments.
Takes a two DateTime
s and returns true if the first one is greater than the second. Otherwise false. Greater than means that it is later then the second datetime.
Examples
# The wall times of the two times are the same, but the one in Los Angeles
# happens after the one in UTC because Los Angeles is behind UTC
iex> from_erl!({{2014,1,1}, {11,11,11}}, "America/Los_Angeles") |> after?(from_erl!({{2014, 1, 1}, {11, 11, 11}}, "Etc/UTC"))
true
iex> from_erl!({{2014,1,1}, {10,10,10}}, "Etc/UTC") |> after?(from_erl!({{1999, 1, 1}, {11, 11, 11}}, "Etc/UTC"))
true
iex> from_erl!({{2014,1,1}, {10,10,10}}, "Etc/UTC") |> after?(from_erl!({{2020, 1, 1}, {11, 11, 11}}, "Etc/UTC"))
false
iex> from_erl!({{2014,1,1}, {10,10,10}}, "Etc/UTC") |> after?(from_erl!({{2014, 1, 1}, {10, 10, 10}}, "Etc/UTC"))
false
Takes a two DateTime
s and returns true if the first one is less than the second. Otherwise false. Less than means that it is earlier then the second datetime.
Examples
iex> from_erl!({{2014,1,1}, {10,10,10}}, "Etc/UTC") |> before?(from_erl!({{1999, 1, 1}, {11, 11, 11}}, "Etc/UTC"))
false
iex> from_erl!({{2014,1,1}, {10,10,10}}, "Etc/UTC") |> before?(from_erl!({{2020, 1, 1}, {11, 11, 11}}, "Etc/UTC"))
true
iex> from_erl!({{2014,1,1}, {10,10,10}}, "Etc/UTC") |> before?(from_erl!({{2014, 1, 1}, {10, 10, 10}}, "Etc/UTC"))
false
The difference between two DateTime structs. In seconds and microseconds.
Leap seconds are ignored.
Returns tuple with {:ok, seconds, microseconds, :before or :after or :same_time}
If the first argument is later (e.g. greater) the second, the result will be positive.
In case of a negative result the second element (seconds) will be negative. This is always the case if both of the arguments have the microseconds as nil or 0. But if the difference is less than a second and the result is negative, then the microseconds will be negative.
Examples
# March 30th 2014 02:00:00 in Central Europe the time changed from
# winter time to summer time. This means that clocks were set forward
# and an hour skipped. So between 01:00 and 4:00 there were 2 hours
# not 3. Two hours is 7200 seconds.
iex> diff(from_erl!({{2014,3,30},{4,0,0}}, "Europe/Stockholm"), from_erl!({{2014,3,30},{1,0,0}}, "Europe/Stockholm"))
{:ok, 7200, 0, :after}
# The first DateTime is 40 seconds after the second DateTime
iex> diff(from_erl!({{2014,10,2},{0,29,50}}, "Etc/UTC"), from_erl!({{2014,10,2},{0,29,10}}, "Etc/UTC"))
{:ok, 40, 0, :after}
# The first DateTime is 40 seconds before the second DateTime
iex> diff(from_erl!({{2014,10,2},{0,29,10}}, "Etc/UTC"), from_erl!({{2014,10,2},{0,29,50}}, "Etc/UTC"))
{:ok, -40, 0, :before}
# The first DateTime is 30 microseconds after the second DateTime
iex> diff(from_erl!({{2014,10,2},{0,29,0}}, "Etc/UTC", 31), from_erl!({{2014,10,2},{0,29,0}}, "Etc/UTC", 1))
{:ok, 0, 30, :after}
# The first DateTime is 2 microseconds after the second DateTime
iex> diff(from_erl!({{2014,10,2},{0,29,0}}, "Etc/UTC", 0), from_erl!({{2014,10,2},{0,29,0}}, "Etc/UTC", 2))
{:ok, 0, -2, :before}
# The first DateTime is 9.999998 seconds after the second DateTime
iex> diff(from_erl!({{2014,10,2},{0,29,10}}, "Etc/UTC", 0), from_erl!({{2014,10,2},{0,29,0}}, "Etc/UTC", 2))
{:ok, 9, 999998, :after}
# The first DateTime is 9.999998 seconds before the second DateTime
iex> diff(from_erl!({{2014,10,2},{0,29,0}}, "Etc/UTC", 2), from_erl!({{2014,10,2},{0,29,10}}, "Etc/UTC", 0))
{:ok, -9, 999998, :before}
iex> diff(from_erl!({{2014,10,2},{0,29,0}}, "Etc/UTC", 0), from_erl!({{2014,10,2},{0,29,10}}, "Etc/UTC", 2))
{:ok, -10, 2, :before}
iex> diff(from_erl!({{2014,10,2},{0,29,1}}, "Etc/UTC", 100), from_erl!({{2014,10,2},{0,29,0}}, "Etc/UTC", 200))
{:ok, 0, 999900, :after}
iex> diff(from_erl!({{2014,10,2},{0,29,0}}, "Etc/UTC", 10), from_erl!({{2014,10,2},{0,29,0}}, "Etc/UTC", 999999))
{:ok, 0, -999989, :before}
# 0:29:10.999999 and 0:29:11 should result in -1 microseconds
iex> diff(from_erl!({{2014,10,2},{0,29,10}}, "Etc/UTC", 999999), from_erl!({{2014,10,2},{0,29,11}}, "Etc/UTC"))
{:ok, 0, -1, :before}
iex> diff(from_erl!({{2014,10,2},{0,29,11}}, "Etc/UTC"), from_erl!({{2014,10,2},{0,29,10}}, "Etc/UTC", 999999))
{:ok, 0, 1, :after}
Create new DateTime struct based on a date and a time and a timezone string.
Examples
iex> from_date_and_time_and_zone({2016, 1, 8}, {14, 10, 55}, "Etc/UTC")
{:ok, %Calendar.DateTime{day: 8, usec: nil, hour: 14, min: 10, month: 1, sec: 55, year: 2016, abbr: "UTC", timezone: "Etc/UTC", usec: nil, utc_off: 0, std_off: 0}}
Like from_date_and_time_and_zone
, but returns result untagged and raises in case of an error or ambiguous datetime.
Examples
iex> from_date_and_time_and_zone!({2016, 1, 8}, {14, 10, 55}, "Etc/UTC")
%Calendar.DateTime{day: 8, usec: nil, hour: 14, min: 10, month: 1, sec: 55, year: 2016, abbr: "UTC", timezone: "Etc/UTC", usec: nil, utc_off: 0, std_off: 0}
Takes an Erlang-style date-time tuple and additionally a timezone name. Returns a tuple with a tag and a DateTime struct.
The tag can be :ok, :ambiguous or :error. :ok is for an unambigous time. :ambiguous is for a time that could have different UTC offsets and/or standard offsets. Usually when switching from summer to winter time.
An erlang style date-time tuple has the following format: {{year, month, day}, {hour, minute, second}}
Examples
Normal, non-ambigous time
iex> from_erl({{2014, 9, 26}, {17, 10, 20}}, "America/Montevideo")
{:ok, %Calendar.DateTime{day: 26, hour: 17, min: 10, month: 9, sec: 20,
year: 2014, timezone: "America/Montevideo",
abbr: "UYT",
utc_off: -10800, std_off: 0, usec: nil} }
Switching from summer to wintertime in the fall means an ambigous time.
iex> from_erl({{2014, 3, 9}, {1, 1, 1}}, "America/Montevideo")
{:ambiguous, %Calendar.AmbiguousDateTime{possible_date_times:
[%Calendar.DateTime{day: 9, hour: 1, min: 1, month: 3, sec: 1,
year: 2014, timezone: "America/Montevideo",
abbr: "UYST", utc_off: -10800, std_off: 3600},
%Calendar.DateTime{day: 9, hour: 1, min: 1, month: 3, sec: 1,
year: 2014, timezone: "America/Montevideo",
abbr: "UYT", utc_off: -10800, std_off: 0},
]}
}
iex> from_erl({{2014, 9, 26}, {17, 10, 20}}, "Non-existing timezone")
{:error, :timezone_not_found}
The time between 2:00 and 3:00 in the following example does not exist because of the one hour gap caused by switching to DST.
iex> from_erl({{2014, 3, 30}, {2, 20, 02}}, "Europe/Copenhagen")
{:error, :invalid_datetime_for_timezone}
Time with fractional seconds. This represents the time 17:10:20.987654321
iex> from_erl({{2014, 9, 26}, {17, 10, 20}}, "America/Montevideo", 987654)
{:ok, %Calendar.DateTime{day: 26, hour: 17, min: 10, month: 9, sec: 20,
year: 2014, timezone: "America/Montevideo",
abbr: "UYT",
utc_off: -10800, std_off: 0, usec: 987654} }
Like from_erl/2 without "!", but returns the result directly without a tag. Will raise if date is ambiguous or invalid! Only use this if you are sure the date is valid. Otherwise use "from_erl" without the "!".
Example:
iex> from_erl!({{2014, 9, 26}, {17, 10, 20}}, "America/Montevideo")
%Calendar.DateTime{day: 26, hour: 17, min: 10, month: 9, sec: 20, year: 2014, timezone: "America/Montevideo", abbr: "UYT", utc_off: -10800, std_off: 0}
Like from_erl, but also takes an argument with the total UTC offset. (Total offset is standard offset + UTC offset)
The result will be the same as from_erl, except if the datetime is ambiguous. When the datetime is ambiguous (for instance during change from DST to non-DST) the total_offset argument is use to try to disambiguise the result. If successful the matching result is returned tagged with :ok
. If the total_offset
argument does not match either, an error will be returned.
Examples:
iex> from_erl_total_off({{2014, 9, 26}, {17, 10, 20}}, "America/Montevideo", -10800, 2)
{:ok, %Calendar.DateTime{day: 26, hour: 17, min: 10, month: 9, sec: 20,
year: 2014, timezone: "America/Montevideo",
abbr: "UYT",
utc_off: -10800, std_off: 0, usec: 2} }
iex> from_erl_total_off({{2014, 3, 9}, {1, 1, 1}}, "America/Montevideo", -7200, 2)
{:ok, %Calendar.DateTime{day: 9, hour: 1, min: 1, month: 3, sec: 1,
year: 2014, timezone: "America/Montevideo", usec: 2,
abbr: "UYST", utc_off: -10800, std_off: 3600}
}
Like from_erl_total_off/4
but takes a 7 element datetime tuple with microseconds instead of a "normal" erlang style tuple.
Examples:
iex> from_micro_erl_total_off({{2014, 3, 9}, {1, 1, 1, 2}}, "America/Montevideo", -7200)
{:ok, %Calendar.DateTime{day: 9, hour: 1, min: 1, month: 3, sec: 1,
year: 2014, timezone: "America/Montevideo", usec: 2,
abbr: "UYST", utc_off: -10800, std_off: 3600}
}
Takes an NaiveDateTime and a time zone identifier and returns a DateTime
iex> Calendar.NaiveDateTime.from_erl!({{2014,10,15},{2,37,22}}) |> from_naive("Etc/UTC")
{:ok, %Calendar.DateTime{abbr: "UTC", day: 15, usec: nil, hour: 2, min: 37, month: 10, sec: 22, std_off: 0, timezone: "Etc/UTC", utc_off: 0, year: 2014}}
Takes a DateTime and returns an integer of gregorian seconds starting with year 0. This is done via the Erlang calendar module.
Examples
iex> from_erl!({{2014,9,26},{17,10,20}}, "UTC") |> gregorian_seconds
63578970620
Deprecated version of now!/1
with an exclamation point. Works the same way as now!/1
.
In the future now/1
will return a tuple with {:ok, [DateTime]}
Takes a timezone name a returns a DateTime with the current time in that timezone. Timezone names must be in the TZ data format.
Examples
iex > DateTime.now! "UTC"
%Calendar.DateTime{abbr: "UTC", day: 15, hour: 2,
min: 39, month: 10, sec: 53, std_off: 0, timezone: "UTC", utc_off: 0,
year: 2014}
iex > DateTime.now! "Europe/Copenhagen"
%Calendar.DateTime{abbr: "CEST", day: 15, hour: 4,
min: 41, month: 10, sec: 1, std_off: 3600, timezone: "Europe/Copenhagen",
utc_off: 3600, year: 2014}
Takes a two DateTime
s and returns true if the first is at the same time as the second one.
Examples
iex> from_erl!({{2014,1,1}, {10,10,10}}, "Etc/UTC") |> same_time?(from_erl!({{2014, 1, 1}, {10, 10, 10}}, "Etc/UTC"))
true
# 10:00 in London is the same time as 11:00 in Copenhagen
iex> from_erl!({{2014,1,1}, {10,10,10}}, "Europe/London") |> same_time?(from_erl!({{2014, 1, 1}, {11, 10, 10}}, "Europe/Copenhagen"))
true
iex> from_erl!({{2014,1,1}, {10,10,10}}, "America/Godthab") |> same_time?(from_erl!({{2014, 1, 1}, {10, 10, 10}}, "Etc/UTC"))
false
iex> from_erl!({{2014,1,1}, {10,10,10}}, "Etc/UTC") |> same_time?(from_erl!({{2020, 1, 1}, {11, 11, 11}}, "Etc/UTC"))
false
iex> from_erl!({{2014,1,1}, {10,10,10}}, "Europe/London") |> same_time?(from_erl!({{2014, 1, 1}, {10, 10, 10}}, "Etc/UTC"))
true
Takes a DateTime and the name of a new timezone. Returns a DateTime with the equivalent time in the new timezone.
Examples
iex> from_erl!({{2014,10,2},{0,29,10}}, "America/New_York",123456) |> shift_zone("Europe/Copenhagen")
{:ok, %Calendar.DateTime{abbr: "CEST", day: 2, hour: 6, min: 29, month: 10, sec: 10, timezone: "Europe/Copenhagen", utc_off: 3600, std_off: 3600, year: 2014, usec: 123456}}
iex> {:ok, nyc} = from_erl {{2014,10,2},{0,29,10}},"America/New_York"; shift_zone(nyc, "Invalid timezone")
{:invalid_time_zone, nil}
Like shift_zone without "!", but does not check that the time zone is valid and just returns a DateTime struct instead of a tuple with a tag.
Example
iex> from_erl!({{2014,10,2},{0,29,10}},"America/New_York") |> shift_zone!("Europe/Copenhagen")
%Calendar.DateTime{abbr: "CEST", day: 2, hour: 6, min: 29, month: 10, sec: 10,
timezone: "Europe/Copenhagen", utc_off: 3600, std_off: 3600, year: 2014}
Takes a DateTime struct and returns a Date struct representing the date part of the provided DateTime.
iex> from_erl!({{2014,10,15},{2,37,22}}, "UTC") |> Calendar.DateTime.to_date
%Calendar.Date{day: 15, month: 10, year: 2014}
Returns a tuple with a Date struct and a Time struct.
iex> from_erl!({{2014,10,15},{2,37,22}}, "UTC") |> Calendar.DateTime.to_date_and_time
{%Calendar.Date{day: 15, month: 10, year: 2014}, %Calendar.Time{usec: nil, hour: 2, min: 37, sec: 22}}
Takes a DateTime struct and returns an erlang style datetime tuple.
Examples
iex> from_erl!({{2014,10,15},{2,37,22}}, "Etc/UTC") |> Calendar.DateTime.to_erl
{{2014, 10, 15}, {2, 37, 22}}
Takes a DateTime struct and returns an Ecto style datetime tuple. This is like an erlang style tuple, but with microseconds added as an additional element in the time part of the tuple.
If the datetime has its usec field set to nil, 0 will be used for usec.
Examples
iex> from_erl!({{2014,10,15},{2,37,22}}, "Etc/UTC", 999999) |> Calendar.DateTime.to_micro_erl
{{2014, 10, 15}, {2, 37, 22, 999999}}
iex> from_erl!({{2014,10,15},{2,37,22}}, "Etc/UTC", nil) |> Calendar.DateTime.to_micro_erl
{{2014, 10, 15}, {2, 37, 22, 0}}
Takes a DateTime and returns a NaiveDateTime
iex> Calendar.DateTime.from_erl!({{2014,10,15},{2,37,22}}, "UTC", 0.55) |> to_naive
%Calendar.NaiveDateTime{day: 15, usec: 0.55, hour: 2, min: 37, month: 10, sec: 22, year: 2014}