-module(tempo@datetime). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]). -export([new/3, from_string/1, literal/1, to_string/1, parse/2, parse_any/1, describe_parse_error/1, from_unix_seconds/1, from_unix_milli/1, from_unix_micro/1, from_timestamp/1, to_unix_micro/1, to_timestamp/1, from_dynamic_string/1, from_dynamic_unix_utc/1, from_dynamic_unix_milli_utc/1, from_dynamic_unix_micro_utc/1, get_date/1, get_calendar_date/1, get_time/1, get_calendar_time_of_day/1, get_offset/1, drop_offset/1, drop_time/1, apply_offset/1, to_unix_seconds/1, to_unix_milli/1, to_utc/1, format/2, to_offset/2, accept_imprecision/1, error_on_imprecision/1, to_local/1, to_local_time/1, to_local_date/1, to_timezone/2, get_timezone_name/1, compare/2, is_earlier/2, is_earlier_or_equal/2, is_equal/2, is_later/2, is_later_or_equal/2, difference/2, as_period/2, add/2, subtract/2, time_left_in_day/1]). -export_type([uncertain_conversion/1]). -type uncertain_conversion(IDJ) :: {precise, IDJ} | {imprecise, IDJ}. -file("/home/john/Repos/tempo/src/tempo/datetime.gleam", 64). -spec new(tempo:date(), tempo:time(), tempo:offset()) -> tempo:date_time(). new(Date, Time, Offset) -> tempo:datetime(Date, Time, Offset). -file("/home/john/Repos/tempo/src/tempo/datetime.gleam", 160). -spec split_time_and_offset(binary()) -> {ok, {binary(), binary()}} | {error, nil}. split_time_and_offset(Time_with_offset) -> case gleam@string:slice(Time_with_offset, -1, 1) of <<"Z"/utf8>> -> _pipe = {gleam@string:drop_end(Time_with_offset, 1), <<"Z"/utf8>>}, {ok, _pipe}; <<"z"/utf8>> -> _pipe@1 = {gleam@string:drop_end(Time_with_offset, 1), <<"Z"/utf8>>}, {ok, _pipe@1}; _ -> case gleam@string:split(Time_with_offset, <<"-"/utf8>>) of [Time, Offset] -> _pipe@2 = {Time, <<"-"/utf8, Offset/binary>>}, {ok, _pipe@2}; _ -> case gleam@string:split(Time_with_offset, <<"+"/utf8>>) of [Time@1, Offset@1] -> _pipe@3 = {Time@1, <<"+"/utf8, Offset@1/binary>>}, {ok, _pipe@3}; _ -> {error, nil} end end end. -file("/home/john/Repos/tempo/src/tempo/datetime.gleam", 111). -spec from_string(binary()) -> {ok, tempo:date_time()} | {error, tempo@error:date_time_parse_error()}. from_string(Datetime) -> Split_dt = case gleam_stdlib:contains_string(Datetime, <<"T"/utf8>>) of true -> gleam@string:split(Datetime, <<"T"/utf8>>); false -> case gleam_stdlib:contains_string(Datetime, <<"t"/utf8>>) of true -> gleam@string:split(Datetime, <<"t"/utf8>>); false -> case gleam_stdlib:contains_string(Datetime, <<"_"/utf8>>) of true -> gleam@string:split(Datetime, <<"_"/utf8>>); false -> gleam@string:split(Datetime, <<" "/utf8>>) end end end, case Split_dt of [Date, Time] -> gleam@result:'try'( begin _pipe = tempo@date:from_string(Date), gleam@result:map_error( _pipe, fun(_capture) -> {date_time_date_parse_error, Datetime, _capture} end ) end, fun(Date@1) -> gleam@result:'try'( begin _pipe@1 = split_time_and_offset(Time), gleam@result:replace_error( _pipe@1, {date_time_invalid_format, Datetime} ) end, fun(_use0) -> {Time@1, Offset} = _use0, gleam@result:'try'( begin _pipe@2 = tempo@time:from_string(Time@1), gleam@result:map_error( _pipe@2, fun(_capture@1) -> {date_time_time_parse_error, Datetime, _capture@1} end ) end, fun(Time@2) -> gleam@result:map( begin _pipe@3 = tempo@offset:from_string( Offset ), gleam@result:map_error( _pipe@3, fun(_capture@2) -> {date_time_offset_parse_error, Datetime, _capture@2} end ) end, fun(Offset@1) -> new(Date@1, Time@2, Offset@1) end ) end ) end ) end ); [Date@2] -> _pipe@4 = tempo@date:from_string(Date@2), _pipe@5 = gleam@result:map( _pipe@4, fun(_capture@3) -> new(_capture@3, {time_of_day, 0}, {offset, 0}) end ), gleam@result:map_error( _pipe@5, fun(_capture@4) -> {date_time_date_parse_error, Datetime, _capture@4} end ); _ -> {error, {date_time_invalid_format, Datetime}} end. -file("/home/john/Repos/tempo/src/tempo/datetime.gleam", 86). -spec literal(binary()) -> tempo:date_time(). literal(Datetime) -> case from_string(Datetime) of {ok, Datetime@1} -> Datetime@1; {error, {date_time_invalid_format, _}} -> erlang:error(#{gleam_error => panic, message => <<"Invalid datetime literal format"/utf8>>, module => <<"tempo/datetime"/utf8>>, function => <<"literal"/utf8>>, line => 90}); {error, {date_time_date_parse_error, _, _}} -> erlang:error(#{gleam_error => panic, message => <<"Invalid date in datetime literal value"/utf8>>, module => <<"tempo/datetime"/utf8>>, function => <<"literal"/utf8>>, line => 92}); {error, {date_time_time_parse_error, _, _}} -> erlang:error(#{gleam_error => panic, message => <<"Invalid time in datetime literal value"/utf8>>, module => <<"tempo/datetime"/utf8>>, function => <<"literal"/utf8>>, line => 94}); {error, _} -> erlang:error(#{gleam_error => panic, message => <<"Invalid datetime literal"/utf8>>, module => <<"tempo/datetime"/utf8>>, function => <<"literal"/utf8>>, line => 95}) end. -file("/home/john/Repos/tempo/src/tempo/datetime.gleam", 187). -spec to_string(tempo:date_time()) -> binary(). to_string(Datetime) -> tempo:datetime_to_string(Datetime). -file("/home/john/Repos/tempo/src/tempo/datetime.gleam", 223). -spec parse(binary(), tempo:date_time_format()) -> {ok, tempo:date_time()} | {error, tempo@error:date_time_parse_error()}. parse(Str, Format) -> Format_str = tempo:get_datetime_format_str(Format), gleam@result:'try'( begin _pipe = tempo:consume_format(Str, Format_str), gleam@result:map_error( _pipe, fun(_capture) -> {date_time_invalid_format, _capture} end ) end, fun(_use0) -> {Parts, _} = _use0, gleam@result:'try'( begin _pipe@1 = tempo:find_date(Parts), gleam@result:map_error( _pipe@1, fun(_capture@1) -> {date_time_date_parse_error, Str, _capture@1} end ) end, fun(Date) -> gleam@result:'try'( begin _pipe@2 = tempo:find_time(Parts), gleam@result:map_error( _pipe@2, fun(_capture@2) -> {date_time_time_parse_error, Str, _capture@2} end ) end, fun(Time) -> gleam@result:'try'( begin _pipe@3 = tempo:find_offset(Parts), gleam@result:map_error( _pipe@3, fun(_capture@3) -> {date_time_offset_parse_error, Str, _capture@3} end ) end, fun(Offset) -> {ok, new(Date, Time, Offset)} end ) end ) end ) end ). -file("/home/john/Repos/tempo/src/tempo/datetime.gleam", 267). -spec parse_any(binary()) -> {ok, tempo:date_time()} | {error, tempo@error:date_time_parse_error()}. parse_any(Str) -> case tempo:parse_any(Str) of {{some, Date}, {some, Time}, {some, Offset}} -> {ok, new(Date, Time, Offset)}; {_, _, none} -> {error, {date_time_invalid_format, <<"Unable to find offset in "/utf8, Str/binary>>}}; {_, none, _} -> {error, {date_time_invalid_format, <<"Unable to find time in "/utf8, Str/binary>>}}; {none, _, _} -> {error, {date_time_invalid_format, <<"Unable to find date in "/utf8, Str/binary>>}} end. -file("/home/john/Repos/tempo/src/tempo/datetime.gleam", 291). -spec describe_parse_error(tempo@error:date_time_parse_error()) -> binary(). describe_parse_error(Error) -> tempo@error:describe_datetime_parse_error(Error). -file("/home/john/Repos/tempo/src/tempo/datetime.gleam", 358). -spec from_unix_seconds(integer()) -> tempo:date_time(). from_unix_seconds(Unix_ts) -> new( tempo@date:from_unix_seconds(Unix_ts), tempo@time:from_unix_seconds(Unix_ts), {offset, 0} ). -file("/home/john/Repos/tempo/src/tempo/datetime.gleam", 393). -spec from_unix_milli(integer()) -> tempo:date_time(). from_unix_milli(Unix_ts) -> new( tempo@date:from_unix_milli(Unix_ts), tempo@time:from_unix_milli(Unix_ts), {offset, 0} ). -file("/home/john/Repos/tempo/src/tempo/datetime.gleam", 423). -spec from_unix_micro(integer()) -> tempo:date_time(). from_unix_micro(Unix_ts) -> new( tempo@date:from_unix_micro(Unix_ts), tempo@time:from_unix_micro(Unix_ts), {offset, 0} ). -file("/home/john/Repos/tempo/src/tempo/datetime.gleam", 334). -spec from_timestamp(gleam@time@timestamp:timestamp()) -> tempo:date_time(). from_timestamp(Timestamp) -> {Seconds, Nanoseconds} = gleam@time@timestamp:to_unix_seconds_and_nanoseconds( Timestamp ), from_unix_micro((Seconds * 1000000) + (Nanoseconds div 1000)). -file("/home/john/Repos/tempo/src/tempo/datetime.gleam", 436). -spec to_unix_micro(tempo:date_time()) -> integer(). to_unix_micro(Datetime) -> tempo:datetime_to_unix_micro(Datetime). -file("/home/john/Repos/tempo/src/tempo/datetime.gleam", 342). -spec to_timestamp(tempo:date_time()) -> gleam@time@timestamp:timestamp(). to_timestamp(Datetime) -> Unix_us = to_unix_micro(Datetime), Seconds = Unix_us div 1000000, Nanoseconds = (Unix_us rem 1000000) * 1000, gleam@time@timestamp:from_unix_seconds_and_nanoseconds(Seconds, Nanoseconds). -file("/home/john/Repos/tempo/src/tempo/datetime.gleam", 462). -spec from_dynamic_string(gleam@dynamic:dynamic_()) -> {ok, tempo:date_time()} | {error, list(gleam@dynamic:decode_error())}. from_dynamic_string(Dynamic_string) -> gleam@result:'try'( begin _pipe = gleam@dynamic@decode:run( Dynamic_string, {decoder, fun gleam@dynamic@decode:decode_string/1} ), gleam@result:map_error( _pipe, fun(Errs) -> gleam@list:map( Errs, fun(Err) -> {decode_error, erlang:element(2, Err), erlang:element(3, Err), erlang:element(4, Err)} end ) end ) end, fun(Datetime) -> case from_string(Datetime) of {ok, Datetime@1} -> {ok, Datetime@1}; {error, Tempo_error} -> {error, [{decode_error, <<"tempo.DateTime"/utf8>>, case Tempo_error of {date_time_invalid_format, Msg} -> Msg; {date_time_time_parse_error, Msg@1, _} -> Msg@1; {date_time_date_parse_error, Msg@2, _} -> Msg@2; {date_time_offset_parse_error, Msg@3, _} -> Msg@3 end, []}]} end end ). -file("/home/john/Repos/tempo/src/tempo/datetime.gleam", 516). -spec from_dynamic_unix_utc(gleam@dynamic:dynamic_()) -> {ok, tempo:date_time()} | {error, list(gleam@dynamic:decode_error())}. from_dynamic_unix_utc(Dynamic_ts) -> gleam@result:map( begin _pipe = gleam@dynamic@decode:run( Dynamic_ts, {decoder, fun gleam@dynamic@decode:decode_int/1} ), gleam@result:map_error( _pipe, fun(Errs) -> gleam@list:map( Errs, fun(Err) -> {decode_error, erlang:element(2, Err), erlang:element(3, Err), erlang:element(4, Err)} end ) end ) end, fun(Unix_seconds) -> from_unix_seconds(Unix_seconds) end ). -file("/home/john/Repos/tempo/src/tempo/datetime.gleam", 555). -spec from_dynamic_unix_milli_utc(gleam@dynamic:dynamic_()) -> {ok, tempo:date_time()} | {error, list(gleam@dynamic:decode_error())}. from_dynamic_unix_milli_utc(Dynamic_ts) -> gleam@result:map( begin _pipe = gleam@dynamic@decode:run( Dynamic_ts, {decoder, fun gleam@dynamic@decode:decode_int/1} ), gleam@result:map_error( _pipe, fun(Errs) -> gleam@list:map( Errs, fun(Err) -> {decode_error, erlang:element(2, Err), erlang:element(3, Err), erlang:element(4, Err)} end ) end ) end, fun(Unix_milli) -> from_unix_milli(Unix_milli) end ). -file("/home/john/Repos/tempo/src/tempo/datetime.gleam", 594). -spec from_dynamic_unix_micro_utc(gleam@dynamic:dynamic_()) -> {ok, tempo:date_time()} | {error, list(gleam@dynamic:decode_error())}. from_dynamic_unix_micro_utc(Dynamic_ts) -> gleam@result:map( begin _pipe = gleam@dynamic@decode:run( Dynamic_ts, {decoder, fun gleam@dynamic@decode:decode_int/1} ), gleam@result:map_error( _pipe, fun(Errs) -> gleam@list:map( Errs, fun(Err) -> {decode_error, erlang:element(2, Err), erlang:element(3, Err), erlang:element(4, Err)} end ) end ) end, fun(Unix_micro) -> from_unix_micro(Unix_micro) end ). -file("/home/john/Repos/tempo/src/tempo/datetime.gleam", 620). -spec get_date(tempo:date_time()) -> tempo:date(). get_date(Datetime) -> erlang:element(2, Datetime). -file("/home/john/Repos/tempo/src/tempo/datetime.gleam", 633). -spec get_calendar_date(tempo:date_time()) -> gleam@time@calendar:date(). get_calendar_date(Datetime) -> _pipe = erlang:element(2, Datetime), tempo@date:to_calendar_date(_pipe). -file("/home/john/Repos/tempo/src/tempo/datetime.gleam", 646). -spec get_time(tempo:date_time()) -> tempo:time(). get_time(Datetime) -> erlang:element(3, Datetime). -file("/home/john/Repos/tempo/src/tempo/datetime.gleam", 659). -spec get_calendar_time_of_day(tempo:date_time()) -> gleam@time@calendar:time_of_day(). get_calendar_time_of_day(Datetime) -> _pipe = erlang:element(3, Datetime), tempo@time:to_calendar_time_of_day(_pipe). -file("/home/john/Repos/tempo/src/tempo/datetime.gleam", 672). -spec get_offset(tempo:date_time()) -> tempo:offset(). get_offset(Datetime) -> _pipe = Datetime, tempo:datetime_get_offset(_pipe). -file("/home/john/Repos/tempo/src/tempo/datetime.gleam", 685). -spec drop_offset(tempo:date_time()) -> tempo:naive_date_time(). drop_offset(Datetime) -> tempo:datetime_drop_offset(Datetime). -file("/home/john/Repos/tempo/src/tempo/datetime.gleam", 698). -spec drop_time(tempo:date_time()) -> tempo:date_time(). drop_time(Datetime) -> Naive = tempo@naive_datetime:drop_time( begin _pipe = Datetime, tempo:datetime_get_naive(_pipe) end ), tempo:datetime( erlang:element(2, Naive), erlang:element(3, Naive), begin _pipe@1 = Datetime, tempo:datetime_get_offset(_pipe@1) end ). -file("/home/john/Repos/tempo/src/tempo/datetime.gleam", 718). -spec apply_offset(tempo:date_time()) -> tempo:naive_date_time(). apply_offset(Datetime) -> tempo:datetime_apply_offset(Datetime). -file("/home/john/Repos/tempo/src/tempo/datetime.gleam", 375). -spec to_unix_seconds(tempo:date_time()) -> integer(). to_unix_seconds(Datetime) -> Utc_dt = begin _pipe = Datetime, apply_offset(_pipe) end, tempo@date:to_unix_seconds( begin _pipe@1 = Utc_dt, tempo:naive_datetime_get_date(_pipe@1) end ) + (tempo:time_to_microseconds( begin _pipe@2 = Utc_dt, tempo:naive_datetime_get_time(_pipe@2) end ) div 1000000). -file("/home/john/Repos/tempo/src/tempo/datetime.gleam", 406). -spec to_unix_milli(tempo:date_time()) -> integer(). to_unix_milli(Datetime) -> Utc_dt = begin _pipe = Datetime, apply_offset(_pipe) end, tempo@date:to_unix_milli( begin _pipe@1 = Utc_dt, tempo:naive_datetime_get_date(_pipe@1) end ) + (tempo:time_to_microseconds( begin _pipe@2 = Utc_dt, tempo:naive_datetime_get_time(_pipe@2) end ) div 1000). -file("/home/john/Repos/tempo/src/tempo/datetime.gleam", 731). -spec to_utc(tempo:date_time()) -> tempo:date_time(). to_utc(Datetime) -> tempo:datetime_to_utc(Datetime). -file("/home/john/Repos/tempo/src/tempo/datetime.gleam", 322). -spec format(tempo:date_time(), tempo:date_time_format()) -> binary(). format(Datetime, Format) -> _pipe = case Format of http -> to_utc(Datetime); _ -> Datetime end, tempo:datetime_format(_pipe, Format). -file("/home/john/Repos/tempo/src/tempo/datetime.gleam", 744). -spec to_offset(tempo:date_time(), tempo:offset()) -> tempo:date_time(). to_offset(Datetime, Offset) -> tempo:datetime_to_offset(Datetime, Offset). -file("/home/john/Repos/tempo/src/tempo/datetime.gleam", 782). -spec accept_imprecision(uncertain_conversion(IEE)) -> IEE. accept_imprecision(Conv) -> case Conv of {precise, A} -> A; {imprecise, A@1} -> A@1 end. -file("/home/john/Repos/tempo/src/tempo/datetime.gleam", 800). -spec error_on_imprecision(uncertain_conversion(IEG)) -> {ok, IEG} | {error, nil}. error_on_imprecision(Conv) -> case Conv of {precise, A} -> {ok, A}; {imprecise, _} -> {error, nil} end. -file("/home/john/Repos/tempo/src/tempo/datetime.gleam", 835). -spec to_local(tempo:date_time()) -> uncertain_conversion(tempo:date_time()). to_local(Datetime) -> gleam@bool:lazy_guard( begin _pipe = Datetime, tempo:datetime_get_offset(_pipe) end =:= tempo@offset:local(), fun() -> {precise, Datetime} end, fun() -> Local_dt = begin _pipe@1 = Datetime, to_offset(_pipe@1, tempo@offset:local()) end, case begin _pipe@2 = Local_dt, _pipe@3 = tempo:datetime_get_naive(_pipe@2), tempo:naive_datetime_get_date(_pipe@3) end =:= tempo@date:current_local() of true -> {precise, Local_dt}; false -> {imprecise, Local_dt} end end ). -file("/home/john/Repos/tempo/src/tempo/datetime.gleam", 880). -spec to_local_time(tempo:date_time()) -> uncertain_conversion(tempo:time()). to_local_time(Datetime) -> case to_local(Datetime) of {precise, Datetime@1} -> _pipe = Datetime@1, _pipe@1 = tempo:datetime_get_naive(_pipe), _pipe@2 = tempo:naive_datetime_get_time(_pipe@1), {precise, _pipe@2}; {imprecise, Datetime@2} -> _pipe@3 = Datetime@2, _pipe@4 = tempo:datetime_get_naive(_pipe@3), _pipe@5 = tempo:naive_datetime_get_time(_pipe@4), {imprecise, _pipe@5} end. -file("/home/john/Repos/tempo/src/tempo/datetime.gleam", 925). -spec to_local_date(tempo:date_time()) -> uncertain_conversion(tempo:date()). to_local_date(Datetime) -> case to_local(Datetime) of {precise, Datetime@1} -> _pipe = Datetime@1, _pipe@1 = tempo:datetime_get_naive(_pipe), _pipe@2 = tempo:naive_datetime_get_date(_pipe@1), {precise, _pipe@2}; {imprecise, Datetime@2} -> _pipe@3 = Datetime@2, _pipe@4 = tempo:datetime_get_naive(_pipe@3), _pipe@5 = tempo:naive_datetime_get_date(_pipe@4), {imprecise, _pipe@5} end. -file("/home/john/Repos/tempo/src/tempo/datetime.gleam", 966). -spec to_timezone(tempo:date_time(), tempo:time_zone_provider()) -> tempo:date_time(). to_timezone(Datetime, Tz) -> tempo:datetime_to_tz(Datetime, Tz). -file("/home/john/Repos/tempo/src/tempo/datetime.gleam", 990). -spec get_timezone_name(tempo:date_time()) -> gleam@option:option(binary()). get_timezone_name(Datetime) -> tempo:datetime_get_tz(Datetime). -file("/home/john/Repos/tempo/src/tempo/datetime.gleam", 1015). -spec compare(tempo:date_time(), tempo:date_time()) -> gleam@order:order(). compare(A, B) -> tempo:datetime_compare(A, B). -file("/home/john/Repos/tempo/src/tempo/datetime.gleam", 1038). -spec is_earlier(tempo:date_time(), tempo:date_time()) -> boolean(). is_earlier(A, B) -> tempo:datetime_is_earlier(A, B). -file("/home/john/Repos/tempo/src/tempo/datetime.gleam", 1060). -spec is_earlier_or_equal(tempo:date_time(), tempo:date_time()) -> boolean(). is_earlier_or_equal(A, B) -> tempo:datetime_is_earlier_or_equal(A, B). -file("/home/john/Repos/tempo/src/tempo/datetime.gleam", 1082). -spec is_equal(tempo:date_time(), tempo:date_time()) -> boolean(). is_equal(A, B) -> tempo:datetime_is_equal(A, B). -file("/home/john/Repos/tempo/src/tempo/datetime.gleam", 1105). -spec is_later(tempo:date_time(), tempo:date_time()) -> boolean(). is_later(A, B) -> tempo:datetime_is_later(A, B). -file("/home/john/Repos/tempo/src/tempo/datetime.gleam", 1127). -spec is_later_or_equal(tempo:date_time(), tempo:date_time()) -> boolean(). is_later_or_equal(A, B) -> tempo:datetime_is_later_or_equal(A, B). -file("/home/john/Repos/tempo/src/tempo/datetime.gleam", 1153). -spec difference(tempo:date_time(), tempo:date_time()) -> tempo:duration(). difference(A, B) -> tempo@naive_datetime:difference( tempo:datetime_apply_offset(A), tempo:datetime_apply_offset(B) ). -file("/home/john/Repos/tempo/src/tempo/datetime.gleam", 1187). -spec as_period(tempo:date_time(), tempo:date_time()) -> tempo:period(). as_period(Start, End) -> tempo:period_new(Start, End). -file("/home/john/Repos/tempo/src/tempo/datetime.gleam", 1203). -spec add(tempo:date_time(), tempo:duration()) -> tempo:date_time(). add(Datetime, Duration_to_add) -> tempo:datetime_add(Datetime, Duration_to_add). -file("/home/john/Repos/tempo/src/tempo/datetime.gleam", 1219). -spec subtract(tempo:date_time(), tempo:duration()) -> tempo:date_time(). subtract(Datetime, Duration_to_subtract) -> tempo:datetime_subtract(Datetime, Duration_to_subtract). -file("/home/john/Repos/tempo/src/tempo/datetime.gleam", 1243). -spec time_left_in_day(tempo:date_time()) -> tempo:time(). time_left_in_day(Datetime) -> _pipe = Datetime, _pipe@1 = tempo:datetime_get_naive(_pipe), _pipe@2 = tempo:naive_datetime_get_time(_pipe@1), tempo@time:left_in_day(_pipe@2).