Current section
Files
Jump to
Current section
Files
src/tempo@time.erl
-module(tempo@time).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([new/3, new_milli/4, new_micro/4, describe_out_of_bounds_error/1, test_literal/3, test_literal_milli/4, test_literal_micro/4, get_hour/1, get_minute/1, get_second/1, get_microsecond/1, to_string/1, from_string/1, literal/1, parse/2, parse_any/1, describe_parse_error/1, format/2, from_unix_seconds/1, from_unix_milli/1, from_unix_micro/1, to_tuple/1, from_tuple/1, to_tuple_microsecond/1, to_calendar_time_of_day/1, from_tuple_microsecond/1, from_calendar_time_of_day/1, to_duration/1, from_duration/1, compare/2, is_earlier/2, is_earlier_or_equal/2, is_equal/2, is_later/2, is_later_or_equal/2, is_between/3, is_outside/3, difference/2, difference_abs/2, add/2, subtract/2, left_in_day/1, until/2, since/2]).
-export_type([boundary/0]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
?MODULEDOC(
" Functions to use with the `Time` type in Tempo. The time values are wall\n"
" time values unless explicitly stated otherwise.\n"
" \n"
" ## Examples\n"
" \n"
" ```gleam\n"
" import tempo/time\n"
" \n"
" pub fn is_past_5pm() {\n"
" tempo.is_time_later(than: time.literal(\"17:00\"))\n"
" }\n"
" ```\n"
" \n"
" ```gleam\n"
" import tempo/time\n"
" \n"
" pub fn get_enthusiastic_time() {\n"
" time.literal(\"13:42\")\n"
" |> time.format(tempo.CustomTime(\n"
" \"[The hour is:] HH, [wow! And even better the minute is:] mm!\"\n"
" ))\n"
" // -> \"The hour is: 13, wow! And even better the minute is: 42!\"\n"
" }\n"
" ```\n"
).
-type boundary() :: {boundary, tempo:time(), boolean()}.
-file("src/tempo/time.gleam", 89).
?DOC(
" Creates a new time value with second precision.\n"
" \n"
" ## Example\n"
" \n"
" ```gleam\n"
" time.new(13, 42, 11)\n"
" // -> Ok(time.literal(\"13:42:11\"))\n"
" ```\n"
" \n"
" ```gleam\n"
" time.new(53, 42, 61)\n"
" // -> Error(tempo_error.TimeOutOfBounds)\n"
" ```\n"
).
-spec new(integer(), integer(), integer()) -> {ok, tempo:time()} |
{error, tempo@error:time_out_of_bounds_error()}.
new(Hour, Minute, Second) ->
tempo:new_time(Hour, Minute, Second).
-file("src/tempo/time.gleam", 115).
?DOC(
" Creates a new time value with millisecond precision.\n"
" \n"
" ## Example\n"
" \n"
" ```gleam\n"
" time.new_milli(13, 42, 11, 20)\n"
" // -> Ok(time.literal(\"13:42:11.020\"))\n"
" ```\n"
" \n"
" ```gleam\n"
" time.new_milli(13, 42, 11, 200)\n"
" // -> Ok(time.literal(\"13:42:11.200\"))\n"
" ```\n"
" \n"
" ```gleam\n"
" time.new_milli(13, 42, 11, 7_500)\n"
" // -> Error(tempo_error.TimeOutOfBounds)\n"
" ```\n"
).
-spec new_milli(integer(), integer(), integer(), integer()) -> {ok,
tempo:time()} |
{error, tempo@error:time_out_of_bounds_error()}.
new_milli(Hour, Minute, Second, Millisecond) ->
tempo:new_time_milli(Hour, Minute, Second, Millisecond).
-file("src/tempo/time.gleam", 143).
?DOC(
" Creates a new time value with microsecond precision.\n"
" \n"
" ## Example\n"
" \n"
" ```gleam\n"
" time.new_micro(13, 42, 11, 20)\n"
" // -> Ok(time.literal(\"13:42:11.000020\"))\n"
" ```\n"
" \n"
" ```gleam\n"
" time.new_micro(13, 42, 11, 200_000)\n"
" // -> Ok(time.litteral(\"13:42:11.200000\"))\n"
" ```\n"
" \n"
" ```gleam\n"
" time.new_micro(13, 42, 11, 7_500_000)\n"
" |> result.map_error(time.describe_out_of_bounds_error)\n"
" // -> Error(\"Subsecond value out of bounds in time: 13:42:11.7500000\")\n"
" ```\n"
).
-spec new_micro(integer(), integer(), integer(), integer()) -> {ok,
tempo:time()} |
{error, tempo@error:time_out_of_bounds_error()}.
new_micro(Hour, Minute, Second, Microsecond) ->
tempo:new_time_micro(Hour, Minute, Second, Microsecond).
-file("src/tempo/time.gleam", 160).
?DOC(
" Converts a time out of bounds error to a human readable error message.\n"
" \n"
" ## Example\n"
" \n"
" ```gleam\n"
" time.new(23, 59, 60)\n"
" |> snag.map_error(with: time.describe_out_of_bounds_error)\n"
" // -> snag.error(\"Second out of bounds in time: 60\")\n"
).
-spec describe_out_of_bounds_error(tempo@error:time_out_of_bounds_error()) -> binary().
describe_out_of_bounds_error(Error) ->
tempo@error:describe_time_out_of_bounds_error(Error).
-file("src/tempo/time.gleam", 204).
?DOC(false).
-spec test_literal(integer(), integer(), integer()) -> tempo:time().
test_literal(Hour, Minute, Second) ->
_assert_subject = tempo:validate_time(Hour, Minute, Second, 0),
{ok, Time} = case _assert_subject of
{ok, _} -> _assert_subject;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
value => _assert_fail,
module => <<"tempo/time"/utf8>>,
function => <<"test_literal"/utf8>>,
line => 205})
end,
Time.
-file("src/tempo/time.gleam", 210).
?DOC(false).
-spec test_literal_milli(integer(), integer(), integer(), integer()) -> tempo:time().
test_literal_milli(Hour, Minute, Second, Millisecond) ->
_assert_subject = tempo:validate_time(
Hour,
Minute,
Second,
Millisecond * 1000
),
{ok, Time} = case _assert_subject of
{ok, _} -> _assert_subject;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
value => _assert_fail,
module => <<"tempo/time"/utf8>>,
function => <<"test_literal_milli"/utf8>>,
line => 216})
end,
Time.
-file("src/tempo/time.gleam", 222).
?DOC(false).
-spec test_literal_micro(integer(), integer(), integer(), integer()) -> tempo:time().
test_literal_micro(Hour, Minute, Second, Microsecond) ->
_assert_subject = tempo:validate_time(Hour, Minute, Second, Microsecond),
{ok, Time} = case _assert_subject of
{ok, _} -> _assert_subject;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
value => _assert_fail,
module => <<"tempo/time"/utf8>>,
function => <<"test_literal_micro"/utf8>>,
line => 228})
end,
Time.
-file("src/tempo/time.gleam", 241).
?DOC(
" Gets the hour value of a time.\n"
" \n"
" ## Example\n"
" \n"
" ```gleam\n"
" time.literal(\"13:42:11\")\n"
" |> time.get_hour\n"
" // -> 13\n"
" ```\n"
).
-spec get_hour(tempo:time()) -> integer().
get_hour(Time) ->
_pipe = Time,
tempo:time_get_hour(_pipe).
-file("src/tempo/time.gleam", 254).
?DOC(
" Gets the minute value of a time.\n"
" \n"
" ## Example\n"
" \n"
" ```gleam\n"
" time.literal(\"13:42:11\")\n"
" |> time.get_minute\n"
" // -> 42\n"
" ```\n"
).
-spec get_minute(tempo:time()) -> integer().
get_minute(Time) ->
_pipe = Time,
tempo:time_get_minute(_pipe).
-file("src/tempo/time.gleam", 267).
?DOC(
" Gets the second value of a time.\n"
" \n"
" ## Example\n"
" \n"
" ```gleam\n"
" time.literal(\"13:42:11\")\n"
" |> time.get_second\n"
" // -> 11\n"
" ```\n"
).
-spec get_second(tempo:time()) -> integer().
get_second(Time) ->
_pipe = Time,
tempo:time_get_second(_pipe).
-file("src/tempo/time.gleam", 280).
?DOC(
" Gets the microsecond value of a time.\n"
" \n"
" ## Example\n"
" \n"
" ```gleam\n"
" time.literal(\"13:42:11.123\")\n"
" |> time.get_microsecond\n"
" // -> 123000\n"
" ```\n"
).
-spec get_microsecond(tempo:time()) -> integer().
get_microsecond(Time) ->
_pipe = Time,
tempo:time_get_micro(_pipe).
-file("src/tempo/time.gleam", 294).
?DOC(
" Converts a time value to a string in the format `hh:mm:ss.s` with \n"
" millisecond precision. If a different precision is needed, use the `format` \n"
" function.\n"
" \n"
" ## Example\n"
" \n"
" ```gleam\n"
" time.to_string(my_time)\n"
" // -> \"21:53:03.534\"\n"
" ```\n"
).
-spec to_string(tempo:time()) -> binary().
to_string(Time) ->
_pipe@9 = gleam_stdlib:identity(
[begin
_pipe = Time,
_pipe@1 = tempo:time_get_hour(_pipe),
_pipe@2 = erlang:integer_to_binary(_pipe@1),
gleam@string:pad_start(_pipe@2, 2, <<"0"/utf8>>)
end,
<<":"/utf8>>,
begin
_pipe@3 = Time,
_pipe@4 = tempo:time_get_minute(_pipe@3),
_pipe@5 = erlang:integer_to_binary(_pipe@4),
gleam@string:pad_start(_pipe@5, 2, <<"0"/utf8>>)
end,
<<":"/utf8>>,
begin
_pipe@6 = Time,
_pipe@7 = tempo:time_get_second(_pipe@6),
_pipe@8 = erlang:integer_to_binary(_pipe@7),
gleam@string:pad_start(_pipe@8, 2, <<"0"/utf8>>)
end]
),
_pipe@10 = gleam@string_tree:append(_pipe@9, <<"."/utf8>>),
_pipe@13 = gleam@string_tree:append(
_pipe@10,
begin
_pipe@11 = tempo:time_get_micro(Time),
_pipe@12 = erlang:integer_to_binary(_pipe@11),
gleam@string:pad_start(_pipe@12, 6, <<"0"/utf8>>)
end
),
unicode:characters_to_binary(_pipe@13).
-file("src/tempo/time.gleam", 334).
?DOC(
" Converts a string to a time value. Accepted formats are `hh:mm:ss.s`, \n"
" `hhmmss.s`, `hh:mm:ss`, `hhmmss`, `hh:mm`, or `hhmm`.\n"
" \n"
" ## Example\n"
" \n"
" ```gleam\n"
" time.from_string(\"00:00:00.000000300\")\n"
" // -> Ok(time.literal(\"00:00:00.000000300\"))\n"
" ```\n"
" \n"
" ```gleam\n"
" time.from_string(\"34:54:16\")\n"
" // -> Error(tempo_error.TimeOutOfBounds)\n"
" ```\n"
).
-spec from_string(binary()) -> {ok, tempo:time()} |
{error, tempo@error:time_parse_error()}.
from_string(Time_str) ->
gleam@result:'try'(
begin
_pipe = case gleam@string:split(Time_str, <<":"/utf8>>) of
[Hour, Minute, Second] ->
{ok, {Hour, Minute, Second}};
[Hour@1, Minute@1] ->
{ok, {Hour@1, Minute@1, <<"0"/utf8>>}};
_ ->
{error, nil}
end,
gleam@result:try_recover(
_pipe,
fun(_) ->
case {string:length(Time_str),
gleam_stdlib:contains_string(Time_str, <<"."/utf8>>)} of
{6, false} ->
{ok,
{gleam@string:slice(Time_str, 0, 2),
gleam@string:slice(Time_str, 2, 2),
gleam@string:slice(Time_str, 4, 2)}};
{4, false} ->
{ok,
{gleam@string:slice(Time_str, 0, 2),
gleam@string:slice(Time_str, 2, 2),
<<"0"/utf8>>}};
{L, true} when L >= 7 ->
{ok,
{gleam@string:slice(Time_str, 0, 2),
gleam@string:slice(Time_str, 2, 2),
gleam@string:slice(Time_str, 4, 12)}};
{_, _} ->
{error, {time_invalid_format, Time_str}}
end
end
)
end,
fun(_use0) ->
{Hour@2, Minute@2, Second@1} = _use0,
gleam@result:'try'(
case {gleam_stdlib:parse_int(Hour@2),
gleam_stdlib:parse_int(Minute@2),
gleam@string:split(Second@1, <<"."/utf8>>)} of
{{ok, Hour@3}, {ok, Minute@3}, [Second@2, Second_fraction]} ->
Second_fraction_length = string:length(Second_fraction),
case Second_fraction_length of
Len when Len =< 3 ->
case {gleam_stdlib:parse_int(Second@2),
gleam_stdlib:parse_int(
begin
_pipe@1 = Second_fraction,
gleam@string:pad_end(
_pipe@1,
3,
<<"0"/utf8>>
)
end
)} of
{{ok, Second@3}, {ok, Milli}} ->
{ok,
tempo:validate_time(
Hour@3,
Minute@3,
Second@3,
Milli * 1000
)};
{_, _} ->
{error, {time_invalid_format, Time_str}}
end;
Len@1 when Len@1 =< 6 ->
case {gleam_stdlib:parse_int(Second@2),
gleam_stdlib:parse_int(
begin
_pipe@2 = Second_fraction,
gleam@string:pad_end(
_pipe@2,
6,
<<"0"/utf8>>
)
end
)} of
{{ok, Second@4}, {ok, Micro}} ->
{ok,
tempo:validate_time(
Hour@3,
Minute@3,
Second@4,
Micro
)};
{_, _} ->
{error, {time_invalid_format, Time_str}}
end;
_ ->
{error, {time_invalid_format, Time_str}}
end;
{{ok, Hour@4}, {ok, Minute@4}, _} ->
case gleam_stdlib:parse_int(Second@1) of
{ok, Second@5} ->
{ok,
tempo:validate_time(
Hour@4,
Minute@4,
Second@5,
0
)};
_ ->
{error, {time_invalid_format, Time_str}}
end;
{_, _, _} ->
{error, {time_invalid_format, Time_str}}
end,
fun(Time) ->
gleam@result:map_error(
Time,
fun(_capture) ->
{time_out_of_bounds, Time_str, _capture}
end
)
end
)
end
).
-file("src/tempo/time.gleam", 182).
?DOC(
" Creates a new time value from a string literal, but will panic if\n"
" the string is invalid. Accepted formats are\n"
" `hh:mm:ss.s`, `hhmmss.s`, `hh:mm:ss`, `hhmmss`, `hh:mm`, or `hhmm`.\n"
" \n"
" Useful for declaring time literals that you know are valid within your \n"
" program.\n"
" \n"
" ## Example\n"
" \n"
" ```gleam\n"
" case \n"
" time.now_local() \n"
" |> time.is_later(than: time.literal(\"11:50:00\")) \n"
" { \n"
" True -> \"We are late!\"\n"
" False -> \"No rush :)\"\n"
" }\n"
" ```\n"
).
-spec literal(binary()) -> tempo:time().
literal(Time) ->
case from_string(Time) of
{ok, Time@1} ->
Time@1;
{error, {time_invalid_format, _}} ->
erlang:error(#{gleam_error => panic,
message => <<"Invalid time literal format"/utf8>>,
module => <<"tempo/time"/utf8>>,
function => <<"literal"/utf8>>,
line => 186});
{error, {time_out_of_bounds, _, {time_hour_out_of_bounds, _}}} ->
erlang:error(#{gleam_error => panic,
message => <<"Invalid time literal hour value"/utf8>>,
module => <<"tempo/time"/utf8>>,
function => <<"literal"/utf8>>,
line => 188});
{error, {time_out_of_bounds, _, {time_minute_out_of_bounds, _}}} ->
erlang:error(#{gleam_error => panic,
message => <<"Invalid time literal minute value"/utf8>>,
module => <<"tempo/time"/utf8>>,
function => <<"literal"/utf8>>,
line => 190});
{error, {time_out_of_bounds, _, {time_second_out_of_bounds, _}}} ->
erlang:error(#{gleam_error => panic,
message => <<"Invalid time literal second value"/utf8>>,
module => <<"tempo/time"/utf8>>,
function => <<"literal"/utf8>>,
line => 192});
{error, {time_out_of_bounds, _, {time_micro_second_out_of_bounds, _}}} ->
erlang:error(#{gleam_error => panic,
message => <<"Invalid time literal microsecond value"/utf8>>,
module => <<"tempo/time"/utf8>>,
function => <<"literal"/utf8>>,
line => 196})
end.
-file("src/tempo/time.gleam", 437).
?DOC(
" Parses a time string in the provided format. Always prefer using\n"
" this over `parse_any`. All parsed formats must have an hour and a second.\n"
" \n"
" Values can be escaped by putting brackets around them, like \"[Hello!] HH\".\n"
" \n"
" Available directives: H (hour), HH (two-digit hour), h (12-hour clock hour), hh \n"
" (two-digit 12-hour clock hour), m (minute), mm (two-digit minute),\n"
" s (second), ss (two-digit second), SSS (millisecond), SSSS (microsecond), \n"
" A (AM/PM), a (am/pm),\n"
" \n"
" ## Example\n"
" \n"
" ```gleam\n"
" time.parse(\"2024/06/08, 13:42:11\", \"YYYY/MM/DD\")\n"
" // -> Ok(time.literal(\"13:42:11\"))\n"
" ```\n"
" \n"
" ```gleam\n"
" time.parse(\"January 13, 2024\", \"MMMM DD, YYYY\")\n"
" |> result.map_error(time.describe_parse_error)\n"
" // -> Error(\"Invlid time format: January 13, 2024\")\n"
" ```\n"
" \n"
" ```gleam\n"
" time.parse(\"Hi! 12 2 am\", \"[Hi!] h m a\")\n"
" // -> Ok(time.literal(\"00:02:00\"))\n"
" ```\n"
).
-spec parse(binary(), tempo:time_format()) -> {ok, tempo:time()} |
{error, tempo@error:time_parse_error()}.
parse(Str, Format) ->
Format_str = tempo:get_time_format_str(Format),
gleam@result:'try'(
begin
_pipe = tempo:consume_format(Str, Format_str),
gleam@result:map_error(
_pipe,
fun(Field@0) -> {time_invalid_format, Field@0} end
)
end,
fun(_use0) ->
{Parts, _} = _use0,
tempo:find_time(Parts)
end
).
-file("src/tempo/time.gleam", 466).
?DOC(
" Tries to parse a given date string without a known format. It will not \n"
" parse two digit years and will assume the month always comes before the \n"
" day in a date. Will leave off any time offset values present.\n"
" \n"
" ## Example\n"
" \n"
" ```gleam\n"
" time.parse_any(\"2024.06.21 01:32 PM -04:00\")\n"
" // -> Ok(time.literal(\"13:32:00\"))\n"
" ```\n"
" \n"
" ```gleam\n"
" time.parse_any(\"2024.06.21\")\n"
" // -> Error(tempo.ParseMissingTime)\n"
" ```\n"
).
-spec parse_any(binary()) -> {ok, tempo:time()} |
{error, tempo@error:time_parse_error()}.
parse_any(Str) ->
case tempo:parse_any(Str) of
{_, {some, Time}, _} ->
{ok, Time};
{_, none, _} ->
{error,
{time_invalid_format,
<<"Unable to find time in "/utf8, Str/binary>>}}
end.
-file("src/tempo/time.gleam", 482).
?DOC(
" Converts a time parse error to a human readable error message.\n"
" \n"
" ## Example\n"
" \n"
" ```gleam\n"
" time.parse(\"13 42 11\", \"HH:mm:ss\")\n"
" |> snag.map_error(with: time.describe_parse_error)\n"
" // -> snag.error(\"Invalid time format: \"13 42 11\"\")\n"
).
-spec describe_parse_error(tempo@error:time_parse_error()) -> binary().
describe_parse_error(Error) ->
tempo@error:describe_time_parse_error(Error).
-file("src/tempo/time.gleam", 513).
?DOC(
" Formats a time value using the provided format.\n"
" \n"
" ## Example\n"
" \n"
" ```gleam\n"
" time.literal(\"13:42:11\")\n"
" |> time.format(tempo.ISO8601TimeMilli)\n"
" // -> \"13:42:11.000\"\n"
" ```\n"
" \n"
" ```gleam\n"
" time.literal(\"13:42:11.314\")\n"
" |> time.format(\"h:mm A\")\n"
" // -> \"1:42 PM\"\n"
" ```\n"
" \n"
" ```gleam \n"
" time.literal(\"09:02:01.014920202\")\n"
" |> time.format(\"HH:mm:ss SSS SSSS SSSSS\")\n"
" // -> \"09:02:01 014 014920 014920202\"\n"
" ```\n"
" \n"
" ```gleam\n"
" naive_datetime.literal(\"13:02:01\")\n"
" |> naive_datetime.format(\"H HH h hh m mm s ss a A [An ant]\")\n"
" // -------------------> \"13 13 1 01 2 02 1 01 pm PM An ant\"\n"
" ```\n"
).
-spec format(tempo:time(), tempo:time_format()) -> binary().
format(Time, Format) ->
Format_str = tempo:get_time_format_str(Format),
_assert_subject = gleam@regexp:from_string(
<<"\\[([^\\]]+)\\]|Y{1,4}|M{1,4}|D{1,2}|d{1,4}|H{1,2}|h{1,2}|a|A|m{1,2}|s{1,2}|Z{1,2}|z{1,2}|S{3,4}|GMT|."/utf8>>
),
{ok, Re} = case _assert_subject of
{ok, _} -> _assert_subject;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
value => _assert_fail,
module => <<"tempo/time"/utf8>>,
function => <<"format"/utf8>>,
line => 516})
end,
_pipe = gleam@regexp:scan(Re, Format_str),
_pipe@1 = lists:reverse(_pipe),
_pipe@2 = gleam@list:fold(_pipe@1, [], fun(Acc, Match) -> case Match of
{match, Content, []} ->
[tempo:time_replace_format(Content, Time) | Acc];
{match, _, [{some, Sub}]} ->
[Sub | Acc];
{match, Content@1, _} ->
[Content@1 | Acc]
end end),
gleam@string:join(_pipe@2, <<""/utf8>>).
-file("src/tempo/time.gleam", 553).
?DOC(false).
-spec from_unix_seconds(integer()) -> tempo:time().
from_unix_seconds(Unix_ts) ->
_pipe = (Unix_ts - (tempo@date:to_unix_seconds(
tempo@date:from_unix_seconds(Unix_ts)
)))
* 1000000,
tempo:time_from_microseconds(_pipe).
-file("src/tempo/time.gleam", 574).
?DOC(false).
-spec from_unix_milli(integer()) -> tempo:time().
from_unix_milli(Unix_ts) ->
_pipe = (Unix_ts - (tempo@date:to_unix_milli(
tempo@date:from_unix_milli(Unix_ts)
)))
* 1000,
tempo:time_from_microseconds(_pipe).
-file("src/tempo/time.gleam", 594).
?DOC(false).
-spec from_unix_micro(integer()) -> tempo:time().
from_unix_micro(Unix_ts) ->
tempo:time_from_unix_micro(Unix_ts).
-file("src/tempo/time.gleam", 608).
?DOC(
" Returns a time value as a tuple of hours, minutes, and seconds. Useful \n"
" for using with another time library.\n"
" \n"
" ## Example\n"
" \n"
" ```gleam\n"
" time.literal(\"13:42:11\")\n"
" |> time.to_tuple\n"
" // -> #(13, 42, 11)\n"
" ```\n"
).
-spec to_tuple(tempo:time()) -> {integer(), integer(), integer()}.
to_tuple(Time) ->
{begin
_pipe = Time,
tempo:time_get_hour(_pipe)
end,
begin
_pipe@1 = Time,
tempo:time_get_minute(_pipe@1)
end,
begin
_pipe@2 = Time,
tempo:time_get_second(_pipe@2)
end}.
-file("src/tempo/time.gleam", 640).
?DOC(
" Converts a tuple of hours, minutes, and seconds to a time value. Useful \n"
" for using with another time library.\n"
" \n"
" ## Example\n"
" \n"
" ```gleam\n"
" #(13, 42, 11)\n"
" |> time.from_tuple\n"
" // -> time.literal(\"13:42:11\")\n"
" ```\n"
).
-spec from_tuple({integer(), integer(), integer()}) -> {ok, tempo:time()} |
{error, tempo@error:time_out_of_bounds_error()}.
from_tuple(Time) ->
new(
erlang:element(1, Time),
erlang:element(2, Time),
erlang:element(3, Time)
).
-file("src/tempo/time.gleam", 656).
?DOC(
" Returns a time value as a tuple of hours, minutes, seconds, and microseconds. \n"
" Useful for using with another time library.\n"
" \n"
" ## Example\n"
" \n"
" ```gleam\n"
" time.literal(\"13:42:11.872\")\n"
" |> time.to_tuple_microsecond\n"
" // -> #(13, 42, 11, 872000)\n"
" ```\n"
).
-spec to_tuple_microsecond(tempo:time()) -> {integer(),
integer(),
integer(),
integer()}.
to_tuple_microsecond(Time) ->
{begin
_pipe = Time,
tempo:time_get_hour(_pipe)
end,
begin
_pipe@1 = Time,
tempo:time_get_minute(_pipe@1)
end,
begin
_pipe@2 = Time,
tempo:time_get_second(_pipe@2)
end,
begin
_pipe@3 = Time,
tempo:time_get_micro(_pipe@3)
end}.
-file("src/tempo/time.gleam", 617).
?DOC(" Converts a tempo time to a time of day type in the core gleam time package.\n").
-spec to_calendar_time_of_day(tempo:time()) -> gleam@time@calendar:time_of_day().
to_calendar_time_of_day(Time) ->
{Hour, Minute, Second, Microsecond} = to_tuple_microsecond(Time),
{time_of_day, Hour, Minute, Second, Microsecond * 1000}.
-file("src/tempo/time.gleam", 675).
?DOC(
" Converts a tuple of hours, minutes, seconds, and microseconds to a time \n"
" value. Useful for using with another time library.\n"
" \n"
" ## Example\n"
" \n"
" ```gleam\n"
" #(13, 42, 11, 872000)\n"
" |> time.from_tuple_microsecond\n"
" // -> time.literal(\"13:42:11.872\")\n"
" ```\n"
).
-spec from_tuple_microsecond({integer(), integer(), integer(), integer()}) -> {ok,
tempo:time()} |
{error, tempo@error:time_out_of_bounds_error()}.
from_tuple_microsecond(Time) ->
new_micro(
erlang:element(1, Time),
erlang:element(2, Time),
erlang:element(3, Time),
erlang:element(4, Time)
).
-file("src/tempo/time.gleam", 623).
?DOC(" Converts a core gleam time time of day to a tempo time.\n").
-spec from_calendar_time_of_day(gleam@time@calendar:time_of_day()) -> {ok,
tempo:time()} |
{error, tempo@error:time_out_of_bounds_error()}.
from_calendar_time_of_day(Time) ->
{time_of_day, Hour, Minute, Second, Nanosecond} = Time,
from_tuple_microsecond({Hour, Minute, Second, Nanosecond div 1000}).
-file("src/tempo/time.gleam", 698).
?DOC(
" Converts a time to duration, assuming the duration epoch is \"00:00:00\".\n"
" \n"
" ## Example\n"
" \n"
" ```gleam\n"
" time.literal(\"00:00:00.000300\")\n"
" |> time.to_duration\n"
" |> duration.as_microseconds\n"
" // -> 300\n"
" ```\n"
"\n"
" ```gleam\n"
" time.literal(\"00:03:06\")\n"
" |> time.to_duration \n"
" |> duration.as_milliseconds\n"
" // -> 186_000\n"
" ```\n"
).
-spec to_duration(tempo:time()) -> gleam@time@duration:duration().
to_duration(Time) ->
tempo:time_to_duration(Time).
-file("src/tempo/time.gleam", 736).
?DOC(
" Converts a duration to the equivalent time of day, assuming the \n"
" duration epoch is \"00:00:00\". Durations longer than 24 hours will be \n"
" wrapped to fit within a 24 hour representation.\n"
" \n"
" ## Example\n"
" \n"
" ```gleam\n"
" duration.seconds(58)\n"
" |> time.from_duration\n"
" |> time.to_second_precision\n"
" |> time.to_string\n"
" // -> \"00:00:58\"\n"
" ```\n"
" \n"
" ```gleam\n"
" duration.minutes(17)\n"
" |> time.from_duration\n"
" |> time.to_string\n"
" // -> \"00:17:00.000000000\"\n"
" ```\n"
" \n"
" ```gleam\n"
" duration.hours(25)\n"
" |> time.from_duration\n"
" |> time.to_string\n"
" // -> \"01:00:00.000000000\"\n"
" ```\n"
" \n"
" ```gleam\n"
" duration.microseconds(-3_000_000)\n"
" |> time.from_duration\n"
" |> time.to_string\n"
" // -> \"23:59:57.000000\"\n"
" ```\n"
).
-spec from_duration(gleam@time@duration:duration()) -> tempo:time().
from_duration(Duration) ->
_pipe = Duration,
_pipe@1 = tempo:duration_get_microseconds(_pipe),
tempo:time_from_microseconds(_pipe@1).
-file("src/tempo/time.gleam", 761).
?DOC(
" Compares two time values.\n"
" \n"
" ## Example\n"
" \n"
" ```gleam\n"
" time.literal(\"13:42:11\")\n"
" |> time.compare(to: time.literal(\"13:42:11\"))\n"
" // -> order.Eq\n"
" ```\n"
" \n"
" ```gleam\n"
" time.literal(\"15:32:01\")\n"
" |> time.compare(to: time.literal(\"13:42:11\"))\n"
" // -> order.Gt\n"
" ```\n"
" \n"
" ```gleam\n"
" time.literal(\"13:10:11\")\n"
" |> time.compare(to: time.literal(\"13:42:11\"))\n"
" // -> order.Lt\n"
" ```\n"
).
-spec compare(tempo:time(), tempo:time()) -> gleam@order:order().
compare(A, B) ->
tempo:time_compare(A, B).
-file("src/tempo/time.gleam", 786).
?DOC(
" Checks if the first time is earlier than the second time.\n"
"\n"
" ## Example\n"
"\n"
" ```gleam\n"
" time.literal(\"13:42:11\")\n"
" |> time.is_earlier(than: time.literal(\"13:42:12\"))\n"
" // -> True\n"
" ```\n"
"\n"
" ```gleam\n"
" time.literal(\"13:42:11\")\n"
" |> time.is_earlier(than: time.literal(\"13:42:11\"))\n"
" // -> False\n"
" ```\n"
"\n"
" ```gleam\n"
" time.literal(\"13:22:15\")\n"
" |> time.is_earlier(than: time.literal(\"07:42:11\"))\n"
" // -> False\n"
" ```\n"
).
-spec is_earlier(tempo:time(), tempo:time()) -> boolean().
is_earlier(A, B) ->
tempo:time_is_earlier(A, B).
-file("src/tempo/time.gleam", 810).
?DOC(
" Checks if the first time is earlier or equal to the second time.\n"
"\n"
" ## Example\n"
"\n"
" ```gleam\n"
" time.literal(\"13:42:11\")\n"
" |> time.is_earlier_or_equal(to: time.literal(\"13:42:12\"))\n"
" // -> True\n"
" ```\n"
"\n"
" ```gleam\n"
" time.literal(\"13:42:11\")\n"
" |> time.is_earlier_or_equal(to: time.literal(\"13:42:11.000\"))\n"
" // -> True\n"
" ```\n"
"\n"
" ```gleam\n"
" time.literal(\"13:22:15\")\n"
" |> time.is_earlier_or_equal(to: time.literal(\"07:42:12\"))\n"
" // -> False\n"
).
-spec is_earlier_or_equal(tempo:time(), tempo:time()) -> boolean().
is_earlier_or_equal(A, B) ->
tempo:time_is_earlier_or_equal(A, B).
-file("src/tempo/time.gleam", 829).
?DOC(
" Checks if the first time is equal to the second time.\n"
"\n"
" ## Example\n"
"\n"
" ```gleam\n"
" time.literal(\"13:42:11.000\")\n"
" |> time.is_equal(to: time.literal(\"13:42:11\"))\n"
" // -> True\n"
" ```\n"
"\n"
" ```gleam\n"
" time.literal(\"13:42:11.002\")\n"
" |> time.is_equal(to: time.literal(\"13:42:11\"))\n"
" // -> False\n"
" ```\n"
).
-spec is_equal(tempo:time(), tempo:time()) -> boolean().
is_equal(A, B) ->
tempo:time_is_equal(A, B).
-file("src/tempo/time.gleam", 854).
?DOC(
" Checks if the first time is later than the second time.\n"
"\n"
" ## Example\n"
"\n"
" ```gleam\n"
" time.literal(\"13:22:15\")\n"
" |> time.is_later(than: time.literal(\"07:42:11\"))\n"
" // -> True\n"
" ```\n"
" \n"
" ```gleam\n"
" time.literal(\"13:42:11\")\n"
" |> time.is_later(than: time.literal(\"13:42:12\"))\n"
" // -> False\n"
" ```\n"
"\n"
" ```gleam\n"
" time.literal(\"13:42:11\")\n"
" |> time.is_later(than: time.literal(\"13:42:11\"))\n"
" // -> False\n"
" ```\n"
).
-spec is_later(tempo:time(), tempo:time()) -> boolean().
is_later(A, B) ->
tempo:time_is_later(A, B).
-file("src/tempo/time.gleam", 878).
?DOC(
" Checks if the first time is earlier or equal to the second time.\n"
"\n"
" ## Example\n"
"\n"
" ```gleam\n"
" time.literal(\"13:22:15\")\n"
" |> time.is_later_or_equal(to: time.literal(\"07:42:12\"))\n"
" // -> True\n"
"\n"
" ```gleam\n"
" time.literal(\"13:42\")\n"
" |> time.is_later_or_equal(to: time.literal(\"13:42:00.000\"))\n"
" // -> True\n"
" ```\n"
" \n"
" ```gleam\n"
" time.literal(\"13:42:11\")\n"
" |> time.is_later_or_equal(to: time.literal(\"13:42:12\"))\n"
" // -> False\n"
" ```\n"
).
-spec is_later_or_equal(tempo:time(), tempo:time()) -> boolean().
is_later_or_equal(A, B) ->
tempo:time_is_later_or_equal(A, B).
-file("src/tempo/time.gleam", 898).
?DOC(
" Checks if a time is between two boundaries.\n"
" \n"
" ## Example\n"
"\n"
" ```gleam\n"
" time.literal(\"13:42:11\")\n"
" |> time.is_between(\n"
" Boundary(time.literal(\"05:00:00\"), inclusive: True), \n"
" and: Boundary(time.literal(\"15:00:00\"), inclusive: False),\n"
" )\n"
" // -> True\n"
" ```\n"
).
-spec is_between(tempo:time(), boundary(), boundary()) -> boolean().
is_between(Time, Start, End) ->
case erlang:element(3, Start) of
true ->
is_later_or_equal(Time, erlang:element(2, Start));
false ->
is_later(Time, erlang:element(2, Start))
end andalso case erlang:element(3, End) of
true ->
is_earlier_or_equal(Time, erlang:element(2, End));
false ->
is_earlier(Time, erlang:element(2, End))
end.
-file("src/tempo/time.gleam", 921).
?DOC(
" Checks if a time is outside of two boundaries.\n"
" \n"
" ## Example\n"
"\n"
" ```gleam\n"
" time.literal(\"13:42:11\")\n"
" |> time.is_outside(\n"
" time.Boundary(time.literal(\"05:00:00\"), inclusive: True), \n"
" and: time.Boundary(time.literal(\"15:00:00\"), inclusive: False),\n"
" )\n"
" // -> False\n"
" ```\n"
).
-spec is_outside(tempo:time(), boundary(), boundary()) -> boolean().
is_outside(Time, Start, End) ->
case erlang:element(3, Start) of
true ->
is_earlier_or_equal(Time, erlang:element(2, Start));
false ->
is_earlier(Time, erlang:element(2, Start))
end orelse case erlang:element(3, End) of
true ->
is_later_or_equal(Time, erlang:element(2, End));
false ->
is_later(Time, erlang:element(2, End))
end.
-file("src/tempo/time.gleam", 951).
?DOC(
" Gets the difference between two times as a duration. Always prefer using\n"
" `duration.start_monotonic` and `duration.stop_monotonic` to record live \n"
" time passing, as it is more precise.\n"
" \n"
" ## Example\n"
" \n"
" ```gleam\n"
" time.literal(\"23:42:11.435\")\n"
" |> time.difference(from: time.literal(\"23:42:09.743\"))\n"
" |> duration.as_milliseconds\n"
" // -> 1692\n"
" ```\n"
" \n"
" ```gleam\n"
" time.literal(\"13:30:11\")\n"
" |> time.difference(from: time.literal(\"13:55:13\"))\n"
" |> duration.as_minutes\n"
" // -> -25\n"
" ```\n"
).
-spec difference(tempo:time(), tempo:time()) -> gleam@time@duration:duration().
difference(A, B) ->
tempo:time_difference(A, B).
-file("src/tempo/time.gleam", 972).
?DOC(
" Gets the absolute difference between two times as a duration.\n"
" \n"
" ## Example\n"
" \n"
" ```gleam\n"
" time.literal(\"23:42:11.435\")\n"
" |> time.difference(from: time.literal(\"23:42:09.743\"))\n"
" |> duration.as_milliseconds\n"
" // -> 1692\n"
" ```\n"
" \n"
" ```gleam\n"
" time.literal(\"13:30:11\")\n"
" |> time.difference(from: time.literal(\"13:55:13\"))\n"
" |> duration.as_minutes\n"
" // -> 25\n"
" ```\n"
).
-spec difference_abs(tempo:time(), tempo:time()) -> gleam@time@duration:duration().
difference_abs(A, B) ->
case tempo:time_to_microseconds(A) - tempo:time_to_microseconds(B) of
Diff when Diff < 0 ->
_pipe = - Diff,
tempo:duration_microseconds(_pipe);
Diff@1 ->
_pipe@1 = Diff@1,
tempo:duration_microseconds(_pipe@1)
end.
-file("src/tempo/time.gleam", 988).
?DOC(
" Adds a duration to a time.\n"
" \n"
" ## Example\n"
" \n"
" ```gleam\n"
" time.literal(\"08:42:53\")\n"
" |> time.add(duration.mintues(36))\n"
" // -> time.literal(\"09:18:53\")\n"
" ```\n"
).
-spec add(tempo:time(), gleam@time@duration:duration()) -> tempo:time().
add(A, B) ->
tempo:time_add(A, B).
-file("src/tempo/time.gleam", 1001).
?DOC(
" Subtracts a duration from a time.\n"
" \n"
" ## Example\n"
" \n"
" ```gleam\n"
" time.literal(\"13:42:02\")\n"
" |> time.subtract(duration.hours(2))\n"
" // -> time.literal(\"11:42:02\")\n"
" ```\n"
).
-spec subtract(tempo:time(), gleam@time@duration:duration()) -> tempo:time().
subtract(A, B) ->
tempo:time_subtract(A, B).
-file("src/tempo/time.gleam", 1018).
?DOC(
" Converts a time to the equivalent time left in the day.\n"
"\n"
" ## Example\n"
"\n"
" ```gleam\n"
" time.literal(\"23:59:03\") |> time.left_in_day\n"
" // -> time.literal(\"00:00:57\")\n"
" ```\n"
"\n"
" ```gleam\n"
" time.literal(\"08:05:20\") |> time.left_in_day\n"
" // -> time.literal(\"15:54:40\")\n"
" ```\n"
).
-spec left_in_day(tempo:time()) -> tempo:time().
left_in_day(Time) ->
_pipe@1 = 86400000000 - begin
_pipe = Time,
tempo:time_to_microseconds(_pipe)
end,
tempo:time_from_microseconds(_pipe@1).
-file("src/tempo/time.gleam", 1041).
?DOC(
" Returns a duration representing the time left from the first time \n"
" until a given time. \n"
" \n"
" ## Example\n"
" \n"
" ```gleam\n"
" time.literal(\"23:54:00\")\n"
" |> time.until(time.literal(\"23:59:04\"))\n"
" |> duration.as_seconds\n"
" // -> 304\n"
" ```\n"
" \n"
" ```gleam\n"
" time.literal(\"23:59:03\")\n"
" |> time.until(time.literal(\"22:00:00\"))\n"
" |> duration.as_milliseconds\n"
" // -> 0\n"
" ```\n"
).
-spec until(tempo:time(), tempo:time()) -> gleam@time@duration:duration().
until(Time, Until) ->
Dur = begin
_pipe = Time,
_pipe@1 = difference(Until, _pipe),
tempo@duration:inverse(_pipe@1)
end,
case begin
_pipe@2 = Dur,
tempo@duration:is_negative(_pipe@2)
end of
true ->
tempo@duration:microseconds(0);
false ->
Dur
end.
-file("src/tempo/time.gleam", 1068).
?DOC(
" Returns a duration representing the time since the first time to the \n"
" given time.\n"
" \n"
" ## Example\n"
" \n"
" ```gleam\n"
" time.literal(\"23:54:00\")\n"
" |> time.since(time.literal(\"13:30:04\"))\n"
" |> duration.as_hours\n"
" // -> 10\n"
" ```\n"
" \n"
" ```gleam\n"
" time.literal(\"12:30:54\")\n"
" |> time.since(time.literal(\"22:00:00\"))\n"
" |> duration.as_milliseconds\n"
" // -> 0\n"
" ```\n"
).
-spec since(tempo:time(), tempo:time()) -> gleam@time@duration:duration().
since(Time, Since) ->
Dur = begin
_pipe = Time,
difference(Since, _pipe)
end,
case begin
_pipe@1 = Dur,
tempo@duration:is_negative(_pipe@1)
end of
true ->
tempo@duration:microseconds(0);
false ->
Dur
end.