Current section

Files

Jump to
gtempo src tempo@duration.erl
Raw

src/tempo@duration.erl

-module(tempo@duration).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([start_monotonic/0, stop_monotonic/1, format_as/3, format_as_many/3, format/1, since/1, years_imprecise/1, weeks/1, days/1, hours/1, minutes/1, seconds/1, milliseconds/1, microseconds/1, nanoseconds/1, new/2, increase/2, decrease/2, as_unit/2, as_unit_fractional/2, as_years_imprecise/1, as_years_fractional_imprecise/1, as_weeks/1, as_weeks_fractional/1, as_days/1, as_days_fractional/1, as_hours/1, as_hours_fractional/1, as_minutes/1, as_minutes_fractional/1, as_seconds/1, as_seconds_fractional/1, as_milliseconds/1, as_milliseconds_fractional/1, as_microseconds/1, as_microseconds_fractional/1, as_nanoseconds/1, as_nanoseconds_fractional/1, compare/2, is_less/2, is_less_or_equal/2, is_equal/2, is_greater/2, is_greater_or_equal/2, absolute/1, inverse/1, is_negative/1]).
-export_type([monotonic_clock/0, unit/0]).
-opaque monotonic_clock() :: {monotonic_clock, integer()}.
-type unit() :: year_imprecise |
week |
day |
hour |
minute |
second |
millisecond |
microsecond |
nanosecond.
-file("/home/john/Repos/tempo/src/tempo/duration.gleam", 47).
-spec start_monotonic() -> monotonic_clock().
start_monotonic() ->
_pipe = tempo_ffi:now_monotonic(),
{monotonic_clock, _pipe}.
-file("/home/john/Repos/tempo/src/tempo/duration.gleam", 61).
-spec stop_monotonic(monotonic_clock()) -> tempo:duration().
stop_monotonic(Start) ->
_pipe = tempo_ffi:now_monotonic() - erlang:element(2, Start),
tempo:duration(_pipe).
-file("/home/john/Repos/tempo/src/tempo/duration.gleam", 177).
-spec as_internal_unit(unit()) -> gtempo@internal:unit().
as_internal_unit(U) ->
case U of
year_imprecise ->
year;
week ->
week;
day ->
day;
hour ->
hour;
minute ->
minute;
second ->
second;
millisecond ->
millisecond;
microsecond ->
microsecond;
nanosecond ->
nanosecond
end.
-file("/home/john/Repos/tempo/src/tempo/duration.gleam", 93).
-spec format_as(tempo:duration(), unit(), integer()) -> binary().
format_as(Duration, Unit, Decimals) ->
_pipe = as_internal_unit(Unit),
gtempo@internal:format_as(
begin
_pipe@1 = Duration,
tempo:duration_get_ns(_pipe@1)
end,
_pipe,
Decimals
).
-file("/home/john/Repos/tempo/src/tempo/duration.gleam", 115).
-spec format_as_many(tempo:duration(), list(unit()), integer()) -> binary().
format_as_many(Duration, Units, Decimals) ->
_pipe = gleam@list:map(Units, fun as_internal_unit/1),
gtempo@internal:format_as_many(
begin
_pipe@1 = Duration,
tempo:duration_get_ns(_pipe@1)
end,
_pipe,
Decimals
).
-file("/home/john/Repos/tempo/src/tempo/duration.gleam", 139).
-spec format(tempo:duration()) -> binary().
format(Duration) ->
case begin
_pipe = Duration,
tempo:duration_get_ns(_pipe)
end of
N when N >= 31449600000000000 ->
format_as_many(
Duration,
[year_imprecise, week, day, hour, minute],
0
);
N@1 when N@1 >= 604800000000000 ->
format_as_many(Duration, [week, day, hour, minute], 0);
N@2 when N@2 >= 86400000000000 ->
format_as_many(Duration, [day, hour, minute], 0);
N@3 when N@3 >= 3600000000000 ->
format_as_many(Duration, [hour, minute, second], 2);
N@4 when N@4 >= 60000000000 ->
format_as_many(Duration, [minute, second], 3);
N@5 when N@5 >= 1000000000 ->
format_as(Duration, second, 3);
N@6 when N@6 >= 1000000 ->
format_as(Duration, millisecond, 0);
N@7 when N@7 >= 1000 ->
format_as(Duration, microsecond, 0);
_ ->
format_as(Duration, nanosecond, 0)
end.
-file("/home/john/Repos/tempo/src/tempo/duration.gleam", 79).
-spec since(monotonic_clock()) -> binary().
since(Start) ->
_pipe = stop_monotonic(Start),
format(_pipe).
-file("/home/john/Repos/tempo/src/tempo/duration.gleam", 224).
-spec years_imprecise(integer()) -> tempo:duration().
years_imprecise(Years) ->
_pipe = Years,
_pipe@1 = gtempo@internal:imprecise_years(_pipe),
tempo:duration(_pipe@1).
-file("/home/john/Repos/tempo/src/tempo/duration.gleam", 237).
-spec weeks(integer()) -> tempo:duration().
weeks(Weeks) ->
_pipe = Weeks,
_pipe@1 = gtempo@internal:imprecise_weeks(_pipe),
tempo:duration(_pipe@1).
-file("/home/john/Repos/tempo/src/tempo/duration.gleam", 250).
-spec days(integer()) -> tempo:duration().
days(Days) ->
tempo:duration_days(Days).
-file("/home/john/Repos/tempo/src/tempo/duration.gleam", 263).
-spec hours(integer()) -> tempo:duration().
hours(Hours) ->
_pipe = Hours,
_pipe@1 = gtempo@internal:hours(_pipe),
tempo:duration(_pipe@1).
-file("/home/john/Repos/tempo/src/tempo/duration.gleam", 276).
-spec minutes(integer()) -> tempo:duration().
minutes(Minutes) ->
_pipe = Minutes,
_pipe@1 = gtempo@internal:minutes(_pipe),
tempo:duration(_pipe@1).
-file("/home/john/Repos/tempo/src/tempo/duration.gleam", 290).
-spec seconds(integer()) -> tempo:duration().
seconds(Seconds) ->
_pipe = Seconds,
_pipe@1 = gtempo@internal:seconds(_pipe),
tempo:duration(_pipe@1).
-file("/home/john/Repos/tempo/src/tempo/duration.gleam", 304).
-spec milliseconds(integer()) -> tempo:duration().
milliseconds(Milliseconds) ->
_pipe = Milliseconds,
_pipe@1 = gtempo@internal:milliseconds(_pipe),
tempo:duration(_pipe@1).
-file("/home/john/Repos/tempo/src/tempo/duration.gleam", 318).
-spec microseconds(integer()) -> tempo:duration().
microseconds(Microseconds) ->
_pipe = Microseconds,
_pipe@1 = gtempo@internal:microseconds(_pipe),
tempo:duration(_pipe@1).
-file("/home/john/Repos/tempo/src/tempo/duration.gleam", 332).
-spec nanoseconds(integer()) -> tempo:duration().
nanoseconds(Nanoseconds) ->
_pipe = Nanoseconds,
tempo:duration(_pipe).
-file("/home/john/Repos/tempo/src/tempo/duration.gleam", 200).
-spec new(integer(), unit()) -> tempo:duration().
new(Duration, Unit) ->
case Unit of
year_imprecise ->
years_imprecise(Duration);
week ->
weeks(Duration);
day ->
days(Duration);
hour ->
hours(Duration);
minute ->
minutes(Duration);
second ->
seconds(Duration);
millisecond ->
milliseconds(Duration);
microsecond ->
microseconds(Duration);
nanosecond ->
nanoseconds(Duration)
end.
-file("/home/john/Repos/tempo/src/tempo/duration.gleam", 347).
-spec increase(tempo:duration(), tempo:duration()) -> tempo:duration().
increase(A, B) ->
tempo:duration_increase(A, B).
-file("/home/john/Repos/tempo/src/tempo/duration.gleam", 370).
-spec decrease(tempo:duration(), tempo:duration()) -> tempo:duration().
decrease(A, B) ->
tempo:duration_decrease(A, B).
-file("/home/john/Repos/tempo/src/tempo/duration.gleam", 383).
-spec as_unit(tempo:duration(), unit()) -> integer().
as_unit(Duration, Unit) ->
_pipe = as_internal_unit(Unit),
gtempo@internal:as_unit(
begin
_pipe@1 = Duration,
tempo:duration_get_ns(_pipe@1)
end,
_pipe
).
-file("/home/john/Repos/tempo/src/tempo/duration.gleam", 396).
-spec as_unit_fractional(tempo:duration(), unit()) -> float().
as_unit_fractional(Duration, Unit) ->
_pipe = as_internal_unit(Unit),
gtempo@internal:as_unit_fractional(
begin
_pipe@1 = Duration,
tempo:duration_get_ns(_pipe@1)
end,
_pipe
).
-file("/home/john/Repos/tempo/src/tempo/duration.gleam", 411).
-spec as_years_imprecise(tempo:duration()) -> integer().
as_years_imprecise(Duration) ->
_pipe = Duration,
_pipe@1 = tempo:duration_get_ns(_pipe),
gtempo@internal:as_years_imprecise(_pipe@1).
-file("/home/john/Repos/tempo/src/tempo/duration.gleam", 425).
-spec as_years_fractional_imprecise(tempo:duration()) -> float().
as_years_fractional_imprecise(Duration) ->
_pipe = Duration,
_pipe@1 = tempo:duration_get_ns(_pipe),
gtempo@internal:as_years_imprecise_fractional(_pipe@1).
-file("/home/john/Repos/tempo/src/tempo/duration.gleam", 430).
-spec as_weeks(tempo:duration()) -> integer().
as_weeks(Duration) ->
_pipe = Duration,
_pipe@1 = tempo:duration_get_ns(_pipe),
gtempo@internal:as_weeks_imprecise(_pipe@1).
-file("/home/john/Repos/tempo/src/tempo/duration.gleam", 435).
-spec as_weeks_fractional(tempo:duration()) -> float().
as_weeks_fractional(Duration) ->
_pipe = Duration,
_pipe@1 = tempo:duration_get_ns(_pipe),
gtempo@internal:as_weeks_imprecise_fractional(_pipe@1).
-file("/home/john/Repos/tempo/src/tempo/duration.gleam", 440).
-spec as_days(tempo:duration()) -> integer().
as_days(Duration) ->
tempo:duration_as_days(Duration).
-file("/home/john/Repos/tempo/src/tempo/duration.gleam", 445).
-spec as_days_fractional(tempo:duration()) -> float().
as_days_fractional(Duration) ->
_pipe = Duration,
_pipe@1 = tempo:duration_get_ns(_pipe),
gtempo@internal:as_days_fractional(_pipe@1).
-file("/home/john/Repos/tempo/src/tempo/duration.gleam", 450).
-spec as_hours(tempo:duration()) -> integer().
as_hours(Duration) ->
_pipe = Duration,
_pipe@1 = tempo:duration_get_ns(_pipe),
gtempo@internal:as_hours(_pipe@1).
-file("/home/john/Repos/tempo/src/tempo/duration.gleam", 455).
-spec as_hours_fractional(tempo:duration()) -> float().
as_hours_fractional(Duration) ->
_pipe = Duration,
_pipe@1 = tempo:duration_get_ns(_pipe),
gtempo@internal:as_hours_fractional(_pipe@1).
-file("/home/john/Repos/tempo/src/tempo/duration.gleam", 460).
-spec as_minutes(tempo:duration()) -> integer().
as_minutes(Duration) ->
_pipe = Duration,
_pipe@1 = tempo:duration_get_ns(_pipe),
gtempo@internal:as_minutes(_pipe@1).
-file("/home/john/Repos/tempo/src/tempo/duration.gleam", 465).
-spec as_minutes_fractional(tempo:duration()) -> float().
as_minutes_fractional(Duration) ->
_pipe = Duration,
_pipe@1 = tempo:duration_get_ns(_pipe),
gtempo@internal:as_minutes_fractional(_pipe@1).
-file("/home/john/Repos/tempo/src/tempo/duration.gleam", 470).
-spec as_seconds(tempo:duration()) -> integer().
as_seconds(Duration) ->
_pipe = Duration,
_pipe@1 = tempo:duration_get_ns(_pipe),
gtempo@internal:as_seconds(_pipe@1).
-file("/home/john/Repos/tempo/src/tempo/duration.gleam", 475).
-spec as_seconds_fractional(tempo:duration()) -> float().
as_seconds_fractional(Duration) ->
_pipe = Duration,
_pipe@1 = tempo:duration_get_ns(_pipe),
gtempo@internal:as_seconds_fractional(_pipe@1).
-file("/home/john/Repos/tempo/src/tempo/duration.gleam", 480).
-spec as_milliseconds(tempo:duration()) -> integer().
as_milliseconds(Duration) ->
_pipe = Duration,
_pipe@1 = tempo:duration_get_ns(_pipe),
gtempo@internal:as_milliseconds(_pipe@1).
-file("/home/john/Repos/tempo/src/tempo/duration.gleam", 485).
-spec as_milliseconds_fractional(tempo:duration()) -> float().
as_milliseconds_fractional(Duration) ->
_pipe = Duration,
_pipe@1 = tempo:duration_get_ns(_pipe),
gtempo@internal:as_milliseconds_fractional(_pipe@1).
-file("/home/john/Repos/tempo/src/tempo/duration.gleam", 490).
-spec as_microseconds(tempo:duration()) -> integer().
as_microseconds(Duration) ->
_pipe = Duration,
_pipe@1 = tempo:duration_get_ns(_pipe),
gtempo@internal:as_microseconds(_pipe@1).
-file("/home/john/Repos/tempo/src/tempo/duration.gleam", 495).
-spec as_microseconds_fractional(tempo:duration()) -> float().
as_microseconds_fractional(Duration) ->
_pipe = Duration,
_pipe@1 = tempo:duration_get_ns(_pipe),
gtempo@internal:as_microseconds_fractional(_pipe@1).
-file("/home/john/Repos/tempo/src/tempo/duration.gleam", 500).
-spec as_nanoseconds(tempo:duration()) -> integer().
as_nanoseconds(Duration) ->
tempo:duration_as_nanoseconds(Duration).
-file("/home/john/Repos/tempo/src/tempo/duration.gleam", 506).
-spec as_nanoseconds_fractional(tempo:duration()) -> float().
as_nanoseconds_fractional(Duration) ->
_pipe = tempo:duration_as_nanoseconds(Duration),
erlang:float(_pipe).
-file("/home/john/Repos/tempo/src/tempo/duration.gleam", 525).
-spec compare(tempo:duration(), tempo:duration()) -> gleam@order:order().
compare(A, B) ->
gleam@int:compare(tempo:duration_get_ns(A), tempo:duration_get_ns(B)).
-file("/home/john/Repos/tempo/src/tempo/duration.gleam", 544).
-spec is_less(tempo:duration(), tempo:duration()) -> boolean().
is_less(A, B) ->
compare(A, B) =:= lt.
-file("/home/john/Repos/tempo/src/tempo/duration.gleam", 563).
-spec is_less_or_equal(tempo:duration(), tempo:duration()) -> boolean().
is_less_or_equal(A, B) ->
(compare(A, B) =:= lt) orelse (compare(A, B) =:= eq).
-file("/home/john/Repos/tempo/src/tempo/duration.gleam", 582).
-spec is_equal(tempo:duration(), tempo:duration()) -> boolean().
is_equal(A, B) ->
compare(A, B) =:= eq.
-file("/home/john/Repos/tempo/src/tempo/duration.gleam", 601).
-spec is_greater(tempo:duration(), tempo:duration()) -> boolean().
is_greater(A, B) ->
compare(A, B) =:= gt.
-file("/home/john/Repos/tempo/src/tempo/duration.gleam", 620).
-spec is_greater_or_equal(tempo:duration(), tempo:duration()) -> boolean().
is_greater_or_equal(A, B) ->
(compare(A, B) =:= gt) orelse (compare(A, B) =:= eq).
-file("/home/john/Repos/tempo/src/tempo/duration.gleam", 635).
-spec absolute(tempo:duration()) -> tempo:duration().
absolute(Duration) ->
tempo:duration_absolute(Duration).
-file("/home/john/Repos/tempo/src/tempo/duration.gleam", 650).
-spec inverse(tempo:duration()) -> tempo:duration().
inverse(Duration) ->
_pipe@1 = - begin
_pipe = Duration,
tempo:duration_get_ns(_pipe)
end,
tempo:duration(_pipe@1).
-file("/home/john/Repos/tempo/src/tempo/duration.gleam", 674).
-spec is_negative(tempo:duration()) -> boolean().
is_negative(Duration) ->
begin
_pipe = Duration,
tempo:duration_get_ns(_pipe)
end
< 0.