Calendar.NaiveDateTime
NaiveDateTime can represents a "naive time". That is a point in time without a specified time zone.
Summary↑
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 |
strftime!(ndt, string, lang \\ :en) | Like DateTime.Format.strftime! but for NaiveDateTime |
to_date(dt) | 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 |
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(dt) | Takes a NaiveDateTime struct and returns a Time struct representing the time part of the provided NaiveDateTime |
Functions
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} }
iex>from_erl({{2014, 99, 99}, {17, 10, 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
Like DateTime.Format.strftime! but for NaiveDateTime.
Refer to documentation for DateTime.Format.strftime!
iex> from_erl!({{2014,10,15},{2,37,22}}) |> strftime! "%Y %h %d"
"2014 Oct 15"
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.
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: "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}