-module(birl@time). -compile(no_auto_import). -export([now/0, to_parts/1, from_parts/2, to_iso/1, from_iso/1, compare/2, difference/2, add/2, subtract/2, get_weekday/1]). -export_type([time/0, week_day/0]). -opaque time() :: {time, integer(), gleam@option:option(integer())}. -type week_day() :: monday | tuesday | wednesday | thursday | friday | saturday | sunday. -spec now() -> time(). now() -> Now = birl_ffi:now(), Monotonic_now = birl_ffi:monotonic_now(), {time, Now, {some, Monotonic_now}}. -spec to_parts(time()) -> {{integer(), integer(), integer()}, {integer(), integer(), integer()}}. to_parts(Value) -> case Value of {time, T, _@1} -> birl_ffi:to_parts(T) end. -spec from_parts( {integer(), integer(), integer()}, {integer(), integer(), integer()} ) -> time(). from_parts(Date, Time) -> String_date = case Date of {Year, Month, Day} -> _pipe = [gleam@int:to_string(Year), gleam@int:to_string(Month), gleam@int:to_string(Day)], _pipe@1 = gleam@list:map( _pipe, fun gleam@string_builder:from_string/1 ), gleam@string_builder:join(_pipe@1, <<"-"/utf8>>) end, String_time = case Time of {Hour, Minute, Second} -> _pipe@2 = [gleam@int:to_string(Hour), gleam@int:to_string(Minute), gleam@int:to_string(Second)], _pipe@3 = gleam@list:map( _pipe@2, fun gleam@string_builder:from_string/1 ), _pipe@4 = gleam@string_builder:join(_pipe@3, <<":"/utf8>>), gleam@string_builder:append(_pipe@4, <<".000Z"/utf8>>) end, {ok, Value@1} = case begin _pipe@5 = gleam@string_builder:join( [String_date, String_time], <<"T"/utf8>> ), _pipe@6 = gleam@string_builder:to_string(_pipe@5), from_iso(_pipe@6) end of {ok, Value} -> {ok, Value}; _try -> erlang:error(#{gleam_error => assert, message => <<"Assertion pattern match failed"/utf8>>, value => _try, module => <<"birl/time"/utf8>>, function => <<"from_parts"/utf8>>, line => 51}) end, Value@1. -spec to_iso(time()) -> binary(). to_iso(Value) -> case Value of {time, T, _@1} -> birl_ffi:to_iso(T) end. -spec from_iso(binary()) -> {ok, time()} | {error, nil}. from_iso(Value) -> {ok, Pattern@1} = case gleam@regex:from_string( <<"\\d{4}-\\d{1,2}-\\d{1,2}T\\d{1,2}:\\d{1,2}:\\d{1,2}.\\d{3}Z"/utf8>> ) of {ok, Pattern} -> {ok, Pattern}; _try -> erlang:error(#{gleam_error => assert, message => <<"Assertion pattern match failed"/utf8>>, value => _try, module => <<"birl/time"/utf8>>, function => <<"from_iso"/utf8>>, line => 68}) end, case gleam@regex:check(Pattern@1, Value) of true -> _pipe = Value, _pipe@1 = birl_ffi:from_iso(_pipe), _pipe@2 = {time, _pipe@1, none}, {ok, _pipe@2}; false -> {error, nil} end. -spec compare(time(), time()) -> gleam@order:order(). compare(A, B) -> {time, Wta, Mta} = A, {time, Wtb, Mtb} = B, {Ta@1, Tb@1} = case {Mta, Mtb} of {{some, Ta}, {some, Tb}} -> {Ta, Tb}; _@1 -> {Wta, Wtb} end, case Ta@1 =:= Tb@1 of true -> eq; false -> case Ta@1 < Tb@1 of true -> lt; false -> gt end end. -spec difference(time(), time()) -> birl@duration:duration(). difference(A, B) -> {time, Wta, Mta} = A, {time, Wtb, Mtb} = B, {Ta@1, Tb@1} = case {Mta, Mtb} of {{some, Ta}, {some, Tb}} -> {Ta, Tb}; _@1 -> {Wta, Wtb} end, {duration, Ta@1 - Tb@1}. -spec add(time(), birl@duration:duration()) -> time(). add(Value, Duration) -> {time, Wt, Mt} = Value, {duration, Duration@1} = Duration, case Mt of {some, Mt@1} -> {time, Wt + Duration@1, {some, Mt@1 + Duration@1}}; none -> {time, Wt + Duration@1, none} end. -spec subtract(time(), birl@duration:duration()) -> time(). subtract(Value, Duration) -> {time, Wt, Mt} = Value, {duration, Duration@1} = Duration, case Mt of {some, Mt@1} -> {time, Wt - Duration@1, {some, Mt@1 - Duration@1}}; none -> {time, Wt - Duration@1, none} end. -spec get_weekday(time()) -> week_day(). get_weekday(Value) -> case Value of {time, T, _@1} -> {ok, Weekday@1} = case gleam@list:at( [monday, tuesday, wednesday, thursday, friday, saturday, sunday], birl_ffi:get_weekday(T) ) of {ok, Weekday} -> {ok, Weekday}; _try -> erlang:error(#{gleam_error => assert, message => <<"Assertion pattern match failed"/utf8>>, value => _try, module => <<"birl/time"/utf8>>, function => <<"get_weekday"/utf8>>, line => 133}) end, Weekday@1 end.