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, from_tuple_microsecond/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]).
-type boundary() :: {boundary, tempo:time(), boolean()}.
-file("/home/john/Repos/tempo/src/tempo/time.gleam", 87).
-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("/home/john/Repos/tempo/src/tempo/time.gleam", 113).
-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("/home/john/Repos/tempo/src/tempo/time.gleam", 141).
-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("/home/john/Repos/tempo/src/tempo/time.gleam", 158).
-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("/home/john/Repos/tempo/src/tempo/time.gleam", 233).
-spec validate(tempo:time()) -> {ok, tempo:time()} |
{error, tempo@error:time_out_of_bounds_error()}.
validate(Time) ->
tempo:validate_time(Time).
-file("/home/john/Repos/tempo/src/tempo/time.gleam", 202).
-spec test_literal(integer(), integer(), integer()) -> tempo:time().
test_literal(Hour, Minute, Second) ->
_assert_subject = begin
_pipe = tempo:time(Hour, Minute, Second, 0),
validate(_pipe)
end,
{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 => 203})
end,
Time.
-file("/home/john/Repos/tempo/src/tempo/time.gleam", 208).
-spec test_literal_milli(integer(), integer(), integer(), integer()) -> tempo:time().
test_literal_milli(Hour, Minute, Second, Millisecond) ->
_assert_subject = begin
_pipe = tempo:time(Hour, Minute, Second, Millisecond * 1000),
validate(_pipe)
end,
{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 => 214})
end,
Time.
-file("/home/john/Repos/tempo/src/tempo/time.gleam", 221).
-spec test_literal_micro(integer(), integer(), integer(), integer()) -> tempo:time().
test_literal_micro(Hour, Minute, Second, Microsecond) ->
_assert_subject = begin
_pipe = tempo:time(Hour, Minute, Second, Microsecond),
validate(_pipe)
end,
{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 => 227})
end,
Time.
-file("/home/john/Repos/tempo/src/tempo/time.gleam", 248).
-spec get_hour(tempo:time()) -> integer().
get_hour(Time) ->
_pipe = Time,
tempo:time_get_hour(_pipe).
-file("/home/john/Repos/tempo/src/tempo/time.gleam", 261).
-spec get_minute(tempo:time()) -> integer().
get_minute(Time) ->
_pipe = Time,
tempo:time_get_minute(_pipe).
-file("/home/john/Repos/tempo/src/tempo/time.gleam", 274).
-spec get_second(tempo:time()) -> integer().
get_second(Time) ->
_pipe = Time,
tempo:time_get_second(_pipe).
-file("/home/john/Repos/tempo/src/tempo/time.gleam", 287).
-spec get_microsecond(tempo:time()) -> integer().
get_microsecond(Time) ->
_pipe = Time,
tempo:time_get_micro(_pipe).
-file("/home/john/Repos/tempo/src/tempo/time.gleam", 301).
-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("/home/john/Repos/tempo/src/tempo/time.gleam", 341).
-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: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: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:time(Hour@4, Minute@4, Second@5, 0)};
_ ->
{error, {time_invalid_format, Time_str}}
end;
{_, _, _} ->
{error, {time_invalid_format, Time_str}}
end,
fun(Time) -> _pipe@3 = validate(Time),
gleam@result:map_error(
_pipe@3,
fun(_capture) ->
{time_out_of_bounds, Time_str, _capture}
end
) end
)
end
).
-file("/home/john/Repos/tempo/src/tempo/time.gleam", 180).
-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 => 184});
{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 => 186});
{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 => 188});
{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 => 190});
{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 => 194})
end.
-file("/home/john/Repos/tempo/src/tempo/time.gleam", 446).
-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(_capture) -> {time_invalid_format, _capture} end
)
end,
fun(_use0) ->
{Parts, _} = _use0,
tempo:find_time(Parts)
end
).
-file("/home/john/Repos/tempo/src/tempo/time.gleam", 475).
-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("/home/john/Repos/tempo/src/tempo/time.gleam", 491).
-spec describe_parse_error(tempo@error:time_parse_error()) -> binary().
describe_parse_error(Error) ->
tempo@error:describe_time_parse_error(Error).
-file("/home/john/Repos/tempo/src/tempo/time.gleam", 522).
-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|SSSSS|SSSS|SSS|."/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 => 525})
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("/home/john/Repos/tempo/src/tempo/time.gleam", 563).
-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("/home/john/Repos/tempo/src/tempo/time.gleam", 585).
-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("/home/john/Repos/tempo/src/tempo/time.gleam", 606).
-spec from_unix_micro(integer()) -> tempo:time().
from_unix_micro(Unix_ts) ->
tempo:time_from_unix_micro(Unix_ts).
-file("/home/john/Repos/tempo/src/tempo/time.gleam", 620).
-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("/home/john/Repos/tempo/src/tempo/time.gleam", 638).
-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("/home/john/Repos/tempo/src/tempo/time.gleam", 654).
-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("/home/john/Repos/tempo/src/tempo/time.gleam", 673).
-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("/home/john/Repos/tempo/src/tempo/time.gleam", 696).
-spec to_duration(tempo:time()) -> tempo:duration().
to_duration(Time) ->
tempo:time_to_duration(Time).
-file("/home/john/Repos/tempo/src/tempo/time.gleam", 734).
-spec from_duration(tempo:duration()) -> tempo:time().
from_duration(Duration) ->
_pipe = Duration,
_pipe@1 = tempo:duration_get_microseconds(_pipe),
tempo:time_from_microseconds(_pipe@1).
-file("/home/john/Repos/tempo/src/tempo/time.gleam", 759).
-spec compare(tempo:time(), tempo:time()) -> gleam@order:order().
compare(A, B) ->
tempo:time_compare(A, B).
-file("/home/john/Repos/tempo/src/tempo/time.gleam", 784).
-spec is_earlier(tempo:time(), tempo:time()) -> boolean().
is_earlier(A, B) ->
tempo:time_is_earlier(A, B).
-file("/home/john/Repos/tempo/src/tempo/time.gleam", 808).
-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("/home/john/Repos/tempo/src/tempo/time.gleam", 827).
-spec is_equal(tempo:time(), tempo:time()) -> boolean().
is_equal(A, B) ->
tempo:time_is_equal(A, B).
-file("/home/john/Repos/tempo/src/tempo/time.gleam", 852).
-spec is_later(tempo:time(), tempo:time()) -> boolean().
is_later(A, B) ->
tempo:time_is_later(A, B).
-file("/home/john/Repos/tempo/src/tempo/time.gleam", 876).
-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("/home/john/Repos/tempo/src/tempo/time.gleam", 896).
-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("/home/john/Repos/tempo/src/tempo/time.gleam", 919).
-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("/home/john/Repos/tempo/src/tempo/time.gleam", 949).
-spec difference(tempo:time(), tempo:time()) -> tempo:duration().
difference(A, B) ->
tempo:time_difference(A, B).
-file("/home/john/Repos/tempo/src/tempo/time.gleam", 970).
-spec difference_abs(tempo:time(), tempo:time()) -> tempo: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(_pipe);
Diff@1 ->
_pipe@1 = Diff@1,
tempo:duration(_pipe@1)
end.
-file("/home/john/Repos/tempo/src/tempo/time.gleam", 986).
-spec add(tempo:time(), tempo:duration()) -> tempo:time().
add(A, B) ->
tempo:time_add(A, B).
-file("/home/john/Repos/tempo/src/tempo/time.gleam", 999).
-spec subtract(tempo:time(), tempo:duration()) -> tempo:time().
subtract(A, B) ->
tempo:time_subtract(A, B).
-file("/home/john/Repos/tempo/src/tempo/time.gleam", 1016).
-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("/home/john/Repos/tempo/src/tempo/time.gleam", 1039).
-spec until(tempo:time(), tempo:time()) -> tempo: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("/home/john/Repos/tempo/src/tempo/time.gleam", 1066).
-spec since(tempo:time(), tempo:time()) -> tempo: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.