Current section

Files

Jump to
gtempo src gtempo@internal.erl
Raw

src/gtempo@internal.erl

-module(gtempo@internal).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([unit_to_string/1, microseconds/1, as_microseconds/1, as_microseconds_fractional/1, floor_div/2, modulo_unwrap/2, div_with_remainder/2, imprecise_years/1, as_years_imprecise/1, as_years_imprecise_fractional/1, imprecise_months/1, as_months_imprecise/1, as_months_imprecise_fractional/1, imprecise_weeks/1, as_weeks_imprecise/1, as_weeks_imprecise_fractional/1, imprecise_days/1, as_days_imprecise/1, as_days_fractional/1, hours/1, as_hours/1, as_hours_fractional/1, minutes/1, as_minutes/1, as_minutes_fractional/1, seconds/1, as_seconds/1, as_seconds_fractional/1, in_microseconds/1, milliseconds/1, as_milliseconds/1, as_unit/2, as_milliseconds_fractional/1, as_unit_fractional/2, format_as/3, format_as_many/3, format/1]).
-export_type([unit/0]).
-type unit() :: year |
month |
week |
day |
hour |
{calculated_year, integer(), integer()} |
{calculated_month, integer(), integer()} |
minute |
second |
millisecond |
microsecond |
nothing.
-file("/home/john/Repos/tempo/src/gtempo/internal.gleam", 136).
-spec unit_to_string(unit()) -> binary().
unit_to_string(Unit) ->
case Unit of
year ->
<<"~year"/utf8>>;
{calculated_year, _, _} ->
<<"year"/utf8>>;
month ->
<<"~month"/utf8>>;
{calculated_month, _, _} ->
<<"month"/utf8>>;
week ->
<<"week"/utf8>>;
day ->
<<"day"/utf8>>;
hour ->
<<"hour"/utf8>>;
minute ->
<<"minute"/utf8>>;
second ->
<<"second"/utf8>>;
millisecond ->
<<"millisecond"/utf8>>;
microsecond ->
<<"microsecond"/utf8>>;
nothing ->
<<"no time"/utf8>>
end.
-file("/home/john/Repos/tempo/src/gtempo/internal.gleam", 236).
-spec microseconds(integer()) -> integer().
microseconds(Microseconds) ->
Microseconds.
-file("/home/john/Repos/tempo/src/gtempo/internal.gleam", 304).
-spec as_microseconds(integer()) -> integer().
as_microseconds(Microseconds) ->
Microseconds.
-file("/home/john/Repos/tempo/src/gtempo/internal.gleam", 308).
-spec as_microseconds_fractional(integer()) -> float().
as_microseconds_fractional(Microseconds) ->
erlang:float(Microseconds).
-file("/home/john/Repos/tempo/src/gtempo/internal.gleam", 314).
-spec floor_div(integer(), integer()) -> integer().
floor_div(Dividend, Divisor) ->
case (((Dividend > 0) andalso (Divisor < 0)) orelse ((Dividend < 0) andalso (Divisor
> 0)))
andalso ((case Divisor of
0 -> 0;
Gleam@denominator -> Dividend rem Gleam@denominator
end) /= 0) of
true ->
(case Divisor of
0 -> 0;
Gleam@denominator@1 -> Dividend div Gleam@denominator@1
end) - 1;
false ->
case Divisor of
0 -> 0;
Gleam@denominator@2 -> Dividend div Gleam@denominator@2
end
end.
-file("/home/john/Repos/tempo/src/gtempo/internal.gleam", 330).
-spec modulo_unwrap(integer(), integer()) -> integer().
modulo_unwrap(Dividend, Divisor) ->
Remainder = case Divisor of
0 -> 0;
Gleam@denominator -> Dividend rem Gleam@denominator
end,
case ((Remainder > 0) andalso (Divisor < 0)) orelse ((Remainder < 0) andalso (Divisor
> 0)) of
true ->
Remainder + Divisor;
false ->
Remainder
end.
-file("/home/john/Repos/tempo/src/gtempo/internal.gleam", 324).
-spec div_with_remainder(integer(), integer()) -> {integer(), integer()}.
div_with_remainder(A, B) ->
{floor_div(A, B), modulo_unwrap(A, B)}.
-file("/home/john/Repos/tempo/src/gtempo/internal.gleam", 204).
-spec imprecise_years(integer()) -> integer().
imprecise_years(Years) ->
Years * 31449600000000.
-file("/home/john/Repos/tempo/src/gtempo/internal.gleam", 240).
-spec as_years_imprecise(integer()) -> integer().
as_years_imprecise(Microseconds) ->
case 31449600000000 of
0 -> 0;
Gleam@denominator -> Microseconds div Gleam@denominator
end.
-file("/home/john/Repos/tempo/src/gtempo/internal.gleam", 244).
-spec as_years_imprecise_fractional(integer()) -> float().
as_years_imprecise_fractional(Microseconds) ->
case erlang:float(31449600000000) of
+0.0 -> +0.0;
-0.0 -> -0.0;
Gleam@denominator -> erlang:float(Microseconds) / Gleam@denominator
end.
-file("/home/john/Repos/tempo/src/gtempo/internal.gleam", 208).
-spec imprecise_months(integer()) -> integer().
imprecise_months(Months) ->
Months * 2592000000000.
-file("/home/john/Repos/tempo/src/gtempo/internal.gleam", 248).
-spec as_months_imprecise(integer()) -> integer().
as_months_imprecise(Microseconds) ->
case 2592000000000 of
0 -> 0;
Gleam@denominator -> Microseconds div Gleam@denominator
end.
-file("/home/john/Repos/tempo/src/gtempo/internal.gleam", 252).
-spec as_months_imprecise_fractional(integer()) -> float().
as_months_imprecise_fractional(Microseconds) ->
case erlang:float(2592000000000) of
+0.0 -> +0.0;
-0.0 -> -0.0;
Gleam@denominator -> erlang:float(Microseconds) / Gleam@denominator
end.
-file("/home/john/Repos/tempo/src/gtempo/internal.gleam", 212).
-spec imprecise_weeks(integer()) -> integer().
imprecise_weeks(Weeks) ->
Weeks * 604800000000.
-file("/home/john/Repos/tempo/src/gtempo/internal.gleam", 256).
-spec as_weeks_imprecise(integer()) -> integer().
as_weeks_imprecise(Microseconds) ->
case 604800000000 of
0 -> 0;
Gleam@denominator -> Microseconds div Gleam@denominator
end.
-file("/home/john/Repos/tempo/src/gtempo/internal.gleam", 260).
-spec as_weeks_imprecise_fractional(integer()) -> float().
as_weeks_imprecise_fractional(Microseconds) ->
case erlang:float(604800000000) of
+0.0 -> +0.0;
-0.0 -> -0.0;
Gleam@denominator -> erlang:float(Microseconds) / Gleam@denominator
end.
-file("/home/john/Repos/tempo/src/gtempo/internal.gleam", 216).
-spec imprecise_days(integer()) -> integer().
imprecise_days(Days) ->
Days * 86400000000.
-file("/home/john/Repos/tempo/src/gtempo/internal.gleam", 264).
-spec as_days_imprecise(integer()) -> integer().
as_days_imprecise(Microseconds) ->
case 86400000000 of
0 -> 0;
Gleam@denominator -> Microseconds div Gleam@denominator
end.
-file("/home/john/Repos/tempo/src/gtempo/internal.gleam", 268).
-spec as_days_fractional(integer()) -> float().
as_days_fractional(Microseconds) ->
case erlang:float(86400000000) of
+0.0 -> +0.0;
-0.0 -> -0.0;
Gleam@denominator -> erlang:float(Microseconds) / Gleam@denominator
end.
-file("/home/john/Repos/tempo/src/gtempo/internal.gleam", 220).
-spec hours(integer()) -> integer().
hours(Hours) ->
Hours * 3600000000.
-file("/home/john/Repos/tempo/src/gtempo/internal.gleam", 272).
-spec as_hours(integer()) -> integer().
as_hours(Microseconds) ->
case 3600000000 of
0 -> 0;
Gleam@denominator -> Microseconds div Gleam@denominator
end.
-file("/home/john/Repos/tempo/src/gtempo/internal.gleam", 276).
-spec as_hours_fractional(integer()) -> float().
as_hours_fractional(Microseconds) ->
case erlang:float(3600000000) of
+0.0 -> +0.0;
-0.0 -> -0.0;
Gleam@denominator -> erlang:float(Microseconds) / Gleam@denominator
end.
-file("/home/john/Repos/tempo/src/gtempo/internal.gleam", 224).
-spec minutes(integer()) -> integer().
minutes(Minutes) ->
Minutes * 60000000.
-file("/home/john/Repos/tempo/src/gtempo/internal.gleam", 280).
-spec as_minutes(integer()) -> integer().
as_minutes(Microseconds) ->
case 60000000 of
0 -> 0;
Gleam@denominator -> Microseconds div Gleam@denominator
end.
-file("/home/john/Repos/tempo/src/gtempo/internal.gleam", 284).
-spec as_minutes_fractional(integer()) -> float().
as_minutes_fractional(Microseconds) ->
case erlang:float(60000000) of
+0.0 -> +0.0;
-0.0 -> -0.0;
Gleam@denominator -> erlang:float(Microseconds) / Gleam@denominator
end.
-file("/home/john/Repos/tempo/src/gtempo/internal.gleam", 228).
-spec seconds(integer()) -> integer().
seconds(Seconds) ->
Seconds * 1000000.
-file("/home/john/Repos/tempo/src/gtempo/internal.gleam", 288).
-spec as_seconds(integer()) -> integer().
as_seconds(Microseconds) ->
case 1000000 of
0 -> 0;
Gleam@denominator -> Microseconds div Gleam@denominator
end.
-file("/home/john/Repos/tempo/src/gtempo/internal.gleam", 292).
-spec as_seconds_fractional(integer()) -> float().
as_seconds_fractional(Microseconds) ->
case erlang:float(1000000) of
+0.0 -> +0.0;
-0.0 -> -0.0;
Gleam@denominator -> erlang:float(Microseconds) / Gleam@denominator
end.
-file("/home/john/Repos/tempo/src/gtempo/internal.gleam", 153).
-spec in_microseconds(unit()) -> integer().
in_microseconds(Unit) ->
case Unit of
year ->
31449600000000;
month ->
2592000000000;
week ->
604800000000;
day ->
86400000000;
{calculated_year, _, Microseconds} ->
Microseconds;
{calculated_month, _, Microseconds@1} ->
Microseconds@1;
hour ->
3600000000;
minute ->
60000000;
second ->
1000000;
millisecond ->
1000;
microsecond ->
1;
nothing ->
0
end.
-file("/home/john/Repos/tempo/src/gtempo/internal.gleam", 232).
-spec milliseconds(integer()) -> integer().
milliseconds(Milliseconds) ->
Milliseconds * 1000.
-file("/home/john/Repos/tempo/src/gtempo/internal.gleam", 296).
-spec as_milliseconds(integer()) -> integer().
as_milliseconds(Microseconds) ->
case 1000 of
0 -> 0;
Gleam@denominator -> Microseconds div Gleam@denominator
end.
-file("/home/john/Repos/tempo/src/gtempo/internal.gleam", 170).
-spec as_unit(integer(), unit()) -> integer().
as_unit(Microseconds, Unit) ->
case Unit of
year ->
as_years_imprecise(Microseconds);
{calculated_year, Years, _} ->
Years;
month ->
as_months_imprecise(Microseconds);
{calculated_month, Months, _} ->
Months;
week ->
as_weeks_imprecise(Microseconds);
day ->
as_days_imprecise(Microseconds);
hour ->
as_hours(Microseconds);
minute ->
as_minutes(Microseconds);
second ->
as_seconds(Microseconds);
millisecond ->
as_milliseconds(Microseconds);
microsecond ->
as_microseconds(Microseconds);
nothing ->
0
end.
-file("/home/john/Repos/tempo/src/gtempo/internal.gleam", 300).
-spec as_milliseconds_fractional(integer()) -> float().
as_milliseconds_fractional(Microseconds) ->
case erlang:float(1000) of
+0.0 -> +0.0;
-0.0 -> -0.0;
Gleam@denominator -> erlang:float(Microseconds) / Gleam@denominator
end.
-file("/home/john/Repos/tempo/src/gtempo/internal.gleam", 187).
-spec as_unit_fractional(integer(), unit()) -> float().
as_unit_fractional(Microseconds, Unit) ->
case Unit of
year ->
as_years_imprecise_fractional(Microseconds);
{calculated_year, Years, _} ->
_pipe = Years,
erlang:float(_pipe);
month ->
as_months_imprecise_fractional(Microseconds);
{calculated_month, Months, _} ->
_pipe@1 = Months,
erlang:float(_pipe@1);
week ->
as_weeks_imprecise_fractional(Microseconds);
day ->
as_days_fractional(Microseconds);
hour ->
as_hours_fractional(Microseconds);
minute ->
as_minutes_fractional(Microseconds);
second ->
as_seconds_fractional(Microseconds);
millisecond ->
as_milliseconds_fractional(Microseconds);
microsecond ->
as_microseconds_fractional(Microseconds);
nothing ->
+0.0
end.
-file("/home/john/Repos/tempo/src/gtempo/internal.gleam", 59).
-spec format_as(integer(), unit(), integer()) -> binary().
format_as(Microseconds, Unit, Decimals) ->
In_unit = as_unit_fractional(Microseconds, Unit),
Decimal = begin
_pipe = erlang:trunc(In_unit),
_pipe@1 = erlang:float(_pipe),
gleam@float:subtract(In_unit, _pipe@1)
end,
Decimal_formatted = begin
_pipe@2 = Decimal,
_pipe@3 = gleam_stdlib:float_to_string(_pipe@2),
_pipe@4 = gleam@string:slice(_pipe@3, 2, Decimals),
gleam@string:pad_end(_pipe@4, Decimals, <<"0"/utf8>>)
end,
Whole = begin
_pipe@5 = erlang:trunc(In_unit),
erlang:integer_to_binary(_pipe@5)
end,
<<<<<<<<<<Whole/binary, (case Decimals > 0 of
true ->
<<"."/utf8>>;
false ->
<<""/utf8>>
end)/binary>>/binary, Decimal_formatted/binary>>/binary, " "/utf8>>/binary, (unit_to_string(
Unit
))/binary>>/binary, (case ((Whole =:= <<"1"/utf8>>) orelse (Whole
=:= <<"-1"/utf8>>))
andalso (Decimal =:= +0.0) of
true ->
<<""/utf8>>;
false ->
<<"s"/utf8>>
end)/binary>>.
-file("/home/john/Repos/tempo/src/gtempo/internal.gleam", 95).
-spec format_as_many(integer(), list(unit()), integer()) -> binary().
format_as_many(Microseconds, Units, Decimals) ->
erlang:element(
2,
gleam@list:index_fold(
Units,
{Microseconds, <<""/utf8>>},
fun(Accumulator, Unit, I) ->
case erlang:length(Units) =:= (I + 1) of
true ->
{0,
<<<<(erlang:element(2, Accumulator))/binary,
(case erlang:length(Units) /= 1 of
true ->
<<"and "/utf8>>;
false ->
<<""/utf8>>
end)/binary>>/binary,
(format_as(
erlang:element(1, Accumulator),
Unit,
Decimals
))/binary>>};
false ->
Remaining_duration = case in_microseconds(Unit) of
0 -> 0;
Gleam@denominator -> Microseconds rem Gleam@denominator
end,
Formated_current_unit = begin
_pipe = erlang:element(1, Accumulator) - Remaining_duration,
format_as(_pipe, Unit, 0)
end,
{Remaining_duration,
<<<<(erlang:element(2, Accumulator))/binary,
Formated_current_unit/binary>>/binary,
(case erlang:length(Units) > 2 of
true ->
<<", "/utf8>>;
false ->
<<" "/utf8>>
end)/binary>>}
end
end
)
).
-file("/home/john/Repos/tempo/src/gtempo/internal.gleam", 39).
-spec format(integer()) -> binary().
format(Microseconds) ->
case Microseconds of
N when N >= 31449600000000 ->
format_as_many(Microseconds, [year, week, day, hour, minute], 0);
N@1 when N@1 >= 604800000000 ->
format_as_many(Microseconds, [week, day, hour, minute], 0);
N@2 when N@2 >= 86400000000 ->
format_as_many(Microseconds, [day, hour, minute], 0);
N@3 when N@3 >= 3600000000 ->
format_as_many(Microseconds, [hour, minute, second], 2);
N@4 when N@4 >= 60000000 ->
format_as_many(Microseconds, [minute, second], 3);
N@5 when N@5 >= 1000000 ->
format_as(Microseconds, second, 3);
N@6 when N@6 >= 1000 ->
format_as(Microseconds, millisecond, 0);
_ ->
format_as(Microseconds, microsecond, 0)
end.