Calendar.NaiveDateTime
NaiveDateTime can represents a "naive time". That is a point in time without a specified time zone.
Summary
advance!(ndt, seconds) | Like |
advance(ndt, seconds) | Takes a NaiveDateTime and an integer. Returns the |
from_erl!(erl_date_time, usec \\ nil) | Like from_erl/1 without "!", but returns the result directly without a tag. Will raise if date is invalid. Only use this if you are sure the date is valid |
from_erl(arg1, usec \\ nil) | Takes an Erlang-style date-time tuple. If the datetime is valid it returns a tuple with a tag and a naive DateTime. Naive in this context means that it does not have any timezone data |
gregorian_seconds(ndt) | Takes a NaiveDateTime and returns an integer of gregorian seconds starting with year 0. This is done via the Erlang calendar module |
strftime!(ndt, string, lang \\ :en) | DEPRICATED. Use |
to_date(ndt) | Takes a NaiveDateTime struct and returns a Date struct representing the date part of the provided NaiveDateTime |
to_date_time(ndt, timezone) | For turning NaiveDateTime structs to into a DateTime |
to_date_time_utc(ndt) | Promote to DateTime with UTC time zone. Should only be used if you are sure that the provided argument is in UTC |
to_erl(naivedatetime) | Takes a NaiveDateTime struct and returns an erlang style datetime tuple |
to_micro_erl(naivedatetime) | Takes a NaiveDateTime 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 |
to_time(ndt) | Takes a NaiveDateTime struct and returns a Time struct representing the time part of the provided NaiveDateTime |
with_offset_to_datetime_utc(ndt, total_utc_offset) | If you have a naive datetime and you know the offset, promote it to a UTC DateTime |
Functions
Takes a NaiveDateTime and an integer. Returns the naive_date_time
advanced by the number of seconds found in the seconds
argument.
If seconds
is negative, the time is moved back.
Examples
# Advance 2 seconds
iex> from_erl!({{2014,10,2},{0,29,10}}, 123456) |> advance(2)
{:ok, %Calendar.NaiveDateTime{day: 2, hour: 0, min: 29, month: 10,
sec: 12, usec: 123456,
year: 2014}}
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.
Examples
# Advance 2 seconds
iex> from_erl!({{2014,10,2},{0,29,10}}, 123456) |> advance!(2)
%Calendar.NaiveDateTime{day: 2, hour: 0, min: 29, month: 10,
sec: 12, usec: 123456,
year: 2014}
Takes an Erlang-style date-time tuple. If the datetime is valid it returns a tuple with a tag and a naive DateTime. Naive in this context means that it does not have any timezone data.
Examples
iex>from_erl({{2014, 9, 26}, {17, 10, 20}})
{:ok, %Calendar.NaiveDateTime{day: 26, hour: 17, min: 10, month: 9, sec: 20, year: 2014} }
iex>from_erl({{2014, 9, 26}, {17, 10, 20}}, 321321)
{:ok, %Calendar.NaiveDateTime{day: 26, hour: 17, min: 10, month: 9, sec: 20, year: 2014, usec: 321321} }
# Invalid date
iex>from_erl({{2014, 99, 99}, {17, 10, 20}})
{:error, :invalid_datetime}
# Invalid time
iex>from_erl({{2014, 9, 26}, {17, 70, 20}})
{:error, :invalid_datetime}
Like from_erl/1 without "!", but returns the result directly without a tag. Will raise if date is invalid. Only use this if you are sure the date is valid.
Examples
iex> from_erl!({{2014, 9, 26}, {17, 10, 20}})
%Calendar.NaiveDateTime{day: 26, hour: 17, min: 10, month: 9, sec: 20, year: 2014}
iex from_erl!({{2014, 99, 99}, {17, 10, 20}})
# this will throw a MatchError
Takes a NaiveDateTime 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}}) |> gregorian_seconds
63578970620
DEPRICATED. Use Calendar.Strftime.strftime!/3
instead - it works the same way.
Takes a NaiveDateTime struct and returns a Date struct representing the date part of the provided NaiveDateTime.
iex> from_erl!({{2014,10,15},{2,37,22}}) |> Calendar.NaiveDateTime.to_date
%Calendar.Date{day: 15, month: 10, year: 2014}
For turning NaiveDateTime structs to into a DateTime.
Takes a NaiveDateTime and a timezone name. If timezone is valid, returns a tuple with an :ok and DateTime.
iex> from_erl!({{2014,10,15},{2,37,22}}) |> Calendar.NaiveDateTime.to_date_time("UTC")
{:ok, %Calendar.DateTime{abbr: "UTC", day: 15, usec: nil, hour: 2, min: 37, month: 10, sec: 22, std_off: 0, timezone: "UTC", utc_off: 0, year: 2014}}
Promote to DateTime with UTC time zone. Should only be used if you are sure that the provided argument is in UTC.
Takes a NaiveDateTime. Returns a DateTime.
iex> from_erl!({{2014,10,15},{2,37,22}}) |> Calendar.NaiveDateTime.to_date_time_utc
%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 NaiveDateTime struct and returns an erlang style datetime tuple.
Examples
iex> from_erl!({{2014, 10, 15}, {2, 37, 22}}) |> to_erl
{{2014, 10, 15}, {2, 37, 22}}
Takes a NaiveDateTime 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}}, 999999) |> Calendar.NaiveDateTime.to_micro_erl
{{2014, 10, 15}, {2, 37, 22, 999999}}
iex> from_erl!({{2014,10,15},{2,37,22}}, nil) |> Calendar.NaiveDateTime.to_micro_erl
{{2014, 10, 15}, {2, 37, 22, 0}}
Takes a NaiveDateTime struct and returns a Time struct representing the time part of the provided NaiveDateTime.
iex> from_erl!({{2014,10,15},{2,37,22}}) |> Calendar.NaiveDateTime.to_time
%Calendar.Time{usec: nil, hour: 2, min: 37, sec: 22}
If you have a naive datetime and you know the offset, promote it to a UTC DateTime.
Examples
# A naive datetime at 2:37:22 with a 3600 second offset will return
# a UTC DateTime with the same date, but at 1:37:22
iex> with_offset_to_datetime_utc {{2014,10,15},{2,37,22}}, 3600
{:ok, %Calendar.DateTime{abbr: "UTC", day: 15, usec: nil, hour: 1, min: 37, month: 10, sec: 22, std_off: 0, timezone: "Etc/UTC", utc_off: 0, year: 2014} }
iex> with_offset_to_datetime_utc{{2014,10,15},{2,37,22}}, 999_999_999_999_999_999_999_999_999
{:error, nil}