Current section

Files

Jump to
glubs src glubs@internal@timestamp.erl
Raw

src/glubs@internal@timestamp.erl

-module(glubs@internal@timestamp).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([parse/2, parse_range/2, to_string/2]).
-spec split_seconds(binary(), binary()) -> {ok, {integer(), integer()}} |
{error, nil}.
split_seconds(Input, Fraction_sep) ->
case gleam@string:split(Input, Fraction_sep) of
[_] ->
gleam@result:'try'(
gleam@int:parse(Input),
fun(S) -> {ok, {S, 0}} end
);
[S@1, Ms] ->
gleam@result:'try'(
gleam@int:parse(S@1),
fun(S@2) ->
gleam@result:'try'(
gleam@int:parse(Ms),
fun(Ms@1) -> {ok, {S@2, Ms@1}} end
)
end
);
_ ->
{error, nil}
end.
-spec parse(binary(), binary()) -> {ok, integer()} | {error, nil}.
parse(Input, Fraction_sep) ->
gleam@result:'try'((case gleam@string:split(Input, <<":"/utf8>>) of
[M, S_ms] ->
{ok, {<<"0"/utf8>>, M, S_ms}};
[H, M@1, S_ms@1] ->
{ok, {H, M@1, S_ms@1}};
_ ->
{error, nil}
end), fun(_use0) ->
{H@1, M@2, S_ms@2} = _use0,
gleam@result:'try'(
gleam@int:parse(H@1),
fun(H@2) ->
gleam@result:'try'(
gleam@int:parse(M@2),
fun(M@3) ->
gleam@result:'try'(
split_seconds(S_ms@2, Fraction_sep),
fun(_use0@1) ->
{S, Ms} = _use0@1,
{ok,
(((S + (M@3 * 60)) + ((H@2 * 60) * 60))
* 1000)
+ Ms}
end
)
end
)
end
)
end).
-spec parse_range(binary(), binary()) -> {ok, {integer(), integer(), binary()}} |
{error, binary()}.
parse_range(Line, Fraction_sep) ->
case gleam@string:split(Line, <<" --> "/utf8>>) of
[Start, End_with_rest] ->
gleam@result:'try'(
begin
_pipe = Start,
_pipe@1 = parse(_pipe, Fraction_sep),
gleam@result:replace_error(
_pipe@1,
<<"Invalid start timestamp"/utf8>>
)
end,
fun(Start@1) ->
{End, Rest} = case gleam@string:split_once(
End_with_rest,
<<" "/utf8>>
) of
{ok, Result} ->
Result;
{error, nil} ->
{End_with_rest, <<""/utf8>>}
end,
gleam@result:'try'(
begin
_pipe@2 = End,
_pipe@3 = parse(_pipe@2, Fraction_sep),
gleam@result:replace_error(
_pipe@3,
<<"Invalid end timestamp"/utf8>>
)
end,
fun(End@1) -> {ok, {Start@1, End@1, Rest}} end
)
end
);
_ ->
{error, <<"Invalid timestamp"/utf8>>}
end.
-spec pad(integer(), integer()) -> binary().
pad(Number, Count) ->
_pipe = Number,
_pipe@1 = gleam@int:to_string(_pipe),
gleam@string:pad_left(_pipe@1, Count, <<"0"/utf8>>).
-spec to_string(integer(), binary()) -> gleam@string_builder:string_builder().
to_string(Ms, Fraction_sep) ->
Hours = pad((Ms div 3600000), 2),
Minutes = pad(((Ms rem 3600000) div 60000), 2),
Seconds = pad((Ms rem 60000) div 1000, 2),
Ms@1 = pad(Ms rem 1000, 3),
gleam@string_builder:from_strings(
[Hours,
<<":"/utf8>>,
Minutes,
<<":"/utf8>>,
Seconds,
Fraction_sep,
Ms@1]
).