Calendar.Time
The Time module provides a struct to represent a simple time without specifying a date, nor a time zone.
Summary
Functions
Returns true if provided time is AM in the twelve hour clock system. Otherwise false
Difference in seconds between two times
Create a Time struct using an erlang style tuple and optionally a fractional second
Like from_erl, but will raise if the time is not valid
Create a Calendar.Time struct from an integer being the number of the second of the day
Takes a time and returns a new time with the next second. If the provided time is 23:59:59 it returns a Time for 00:00:00
Returns true if provided time is AM in the twelve hour clock system. Otherwise false
Takes a time and returns a new time with the previous second. If the provided time is 00:00:00 it returns a Time for 23:59:59
The number of the second in the day with 00:00:00 being second 1 and 23:59:59 being number 86400
Takes a Time struct and returns an erlang style time tuple
Takes a Time struct and returns an Ecto style time four-tuple with microseconds
Converts a Time to the 12 hour format
Functions
Returns true if provided time is AM in the twelve hour clock system. Otherwise false.
Examples
iex> {8, 10, 23} |> Time.am?
true
iex> {20, 10, 23} |> Time.am?
false
Difference in seconds between two times.
Takes two Time structs: first_time
and second_time
. Subtracts second_time
from first_time
.
iex> from_erl!({0, 0, 30}) |> diff(from_erl!({0, 0, 10}))
20
iex> from_erl!({0, 0, 10}) |> diff(from_erl!({0, 0, 30}))
-20
Create a Time struct using an erlang style tuple and optionally a fractional second.
iex> from_erl({20,14,15})
{:ok, %Calendar.Time{usec: nil, hour: 20, min: 14, sec: 15}}
iex> from_erl({20,14,15}, 123456)
{:ok, %Calendar.Time{usec: 123456, hour: 20, min: 14, sec: 15}}
iex> from_erl({24,14,15})
{:error, :invalid_time}
iex> from_erl({-1,0,0})
{:error, :invalid_time}
iex> from_erl({20,14,15}, 1_000_000)
{:error, :invalid_time}
Like from_erl, but will raise if the time is not valid.
iex> from_erl!({20,14,15})
%Calendar.Time{usec: nil, hour: 20, min: 14, sec: 15}
iex> from_erl!({20,14,15}, 123456)
%Calendar.Time{usec: 123456, hour: 20, min: 14, sec: 15}
Create a Calendar.Time struct from an integer being the number of the second of the day.
00:00:00 being second 0 and 23:59:59 being number 86399
Examples
iex> 0 |> from_second_in_day
%Calendar.Time{hour: 0, min: 0, sec: 0, usec: nil}
iex> 43200 |> from_second_in_day
%Calendar.Time{hour: 12, min: 0, sec: 0, usec: nil}
iex> 86399 |> from_second_in_day
%Calendar.Time{hour: 23, min: 59, sec: 59, usec: nil}
Takes a time and returns a new time with the next second. If the provided time is 23:59:59 it returns a Time for 00:00:00.
Examples
iex> {12, 0, 0} |> next_second
%Calendar.Time{hour: 12, min: 0, sec: 1, usec: nil}
# Preserves microseconds
iex> {12, 0, 0, 123456} |> next_second
%Calendar.Time{hour: 12, min: 0, sec: 1, usec: 123456}
# At the end of the day it goes to 00:00:00
iex> {23, 59, 59} |> next_second
%Calendar.Time{hour: 0, min: 0, sec: 0, usec: nil}
iex> {23, 59, 59, 300000} |> next_second
%Calendar.Time{hour: 0, min: 0, sec: 0, usec: 300000}
Returns true if provided time is AM in the twelve hour clock system. Otherwise false.
Examples
iex> {8, 10, 23} |> Time.pm?
false
iex> {20, 10, 23} |> Time.pm?
true
Takes a time and returns a new time with the previous second. If the provided time is 00:00:00 it returns a Time for 23:59:59.
Examples
iex> {12, 0, 0} |> prev_second
%Calendar.Time{hour: 11, min: 59, sec: 59, usec: nil}
# Preserves microseconds
iex> {12, 0, 0, 123456} |> prev_second
%Calendar.Time{hour: 11, min: 59, sec: 59, usec: 123456}
# At the beginning of the day it goes to 23:59:59
iex> {0, 0, 0} |> prev_second
%Calendar.Time{hour: 23, min: 59, sec: 59, usec: nil}
iex> {0, 0, 0, 200_000} |> prev_second
%Calendar.Time{hour: 23, min: 59, sec: 59, usec: 200_000}
The number of the second in the day with 00:00:00 being second 1 and 23:59:59 being number 86400
Examples
iex> {0, 0, 0} |> second_in_day
0
iex> {23, 59, 59} |> second_in_day
86399
Takes a Time struct and returns an erlang style time tuple.
Examples
iex> from_erl!({10, 20, 25}, 123456) |> to_erl
{10, 20, 25}
iex> {10, 20, 25} |> to_erl
{10, 20, 25}
Takes a Time struct and returns an Ecto style time four-tuple with microseconds.
If the Time struct has its usec field set to nil, 0 will be used for usec.
Examples
iex> from_erl!({10, 20, 25}, 123456) |> to_micro_erl
{10, 20, 25, 123456}
# If `usec` is nil, 0 is used instead as the last element in the tuple
iex> {10, 20, 25} |> from_erl! |> to_micro_erl
{10, 20, 25, 0}
iex> {10, 20, 25} |> to_micro_erl
{10, 20, 25, 0}