Current section
Files
Jump to
Current section
Files
src/tempo@naive_datetime.erl
-module(tempo@naive_datetime).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([new/2, from_string/1, literal/1, to_string/1, to_tuple/1, parse/2, parse_any/1, describe_parse_error/1, get_date/1, get_time/1, format/2, drop_time/1, set_offset/2, as_utc/1, as_local/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]).
-file("/home/john/Repos/tempo/src/tempo/naive_datetime.gleam", 42).
-spec new(tempo:date(), tempo:time()) -> tempo:naive_date_time().
new(Date, Time) ->
tempo:naive_datetime(Date, Time).
-file("/home/john/Repos/tempo/src/tempo/naive_datetime.gleam", 128).
-spec from_string(binary()) -> {ok, tempo:naive_date_time()} |
{error, tempo@error:naive_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 ->
gleam@string:split(Datetime, <<" "/utf8>>)
end,
case Split_dt of
[Date, Time] ->
gleam@result:'try'(
begin
_pipe = tempo@date:from_string(Date),
gleam@result:map_error(
_pipe,
fun(_capture) ->
{naive_date_time_date_parse_error,
<<"Unable to parse date in input: "/utf8,
Datetime/binary>>,
_capture}
end
)
end,
fun(Date@1) ->
gleam@result:map(
begin
_pipe@1 = tempo@time:from_string(Time),
gleam@result:map_error(
_pipe@1,
fun(_capture@1) ->
{naive_date_time_time_parse_error,
<<"Unable to parse time in input: "/utf8,
Datetime/binary>>,
_capture@1}
end
)
end,
fun(Time@1) -> tempo:naive_datetime(Date@1, Time@1) end
)
end
);
[Date@2] ->
gleam@result:map(
begin
_pipe@2 = tempo@date:from_string(Date@2),
gleam@result:map_error(
_pipe@2,
fun(_capture@2) ->
{naive_date_time_date_parse_error,
<<"Unable to parse date in input: "/utf8,
Datetime/binary>>,
_capture@2}
end
)
end,
fun(Date@3) ->
tempo:naive_datetime(Date@3, {time_of_day, 0})
end
);
_ ->
{error,
{naive_date_time_invalid_format,
<<"Unable to determine date and time delimiter in input: "/utf8,
Datetime/binary>>}}
end.
-file("/home/john/Repos/tempo/src/tempo/naive_datetime.gleam", 64).
-spec literal(binary()) -> tempo:naive_date_time().
literal(Naive_datetime) ->
case from_string(Naive_datetime) of
{ok, Naive_datetime@1} ->
Naive_datetime@1;
{error, {naive_date_time_invalid_format, _}} ->
erlang:error(#{gleam_error => panic,
message => <<"Invalid naive datetime literal format"/utf8>>,
module => <<"tempo/naive_datetime"/utf8>>,
function => <<"literal"/utf8>>,
line => 68});
{error, {naive_date_time_time_parse_error, _, {time_invalid_format, _}}} ->
erlang:error(#{gleam_error => panic,
message => <<"Invalid time format in naive datetime literal"/utf8>>,
module => <<"tempo/naive_datetime"/utf8>>,
function => <<"literal"/utf8>>,
line => 72});
{error,
{naive_date_time_time_parse_error,
_,
{time_out_of_bounds, _, {time_hour_out_of_bounds, _}}}} ->
erlang:error(#{gleam_error => panic,
message => <<"Invalid hour value in naive datetime literal"/utf8>>,
module => <<"tempo/naive_datetime"/utf8>>,
function => <<"literal"/utf8>>,
line => 76});
{error,
{naive_date_time_time_parse_error,
_,
{time_out_of_bounds, _, {time_minute_out_of_bounds, _}}}} ->
erlang:error(#{gleam_error => panic,
message => <<"Invalid minute value in naive datetime literal"/utf8>>,
module => <<"tempo/naive_datetime"/utf8>>,
function => <<"literal"/utf8>>,
line => 80});
{error,
{naive_date_time_time_parse_error,
_,
{time_out_of_bounds, _, {time_second_out_of_bounds, _}}}} ->
erlang:error(#{gleam_error => panic,
message => <<"Invalid second value in naive datetime literal"/utf8>>,
module => <<"tempo/naive_datetime"/utf8>>,
function => <<"literal"/utf8>>,
line => 84});
{error,
{naive_date_time_time_parse_error,
_,
{time_out_of_bounds, _, {time_micro_second_out_of_bounds, _}}}} ->
erlang:error(#{gleam_error => panic,
message => <<"Invalid subsecond value in naive datetime literal"/utf8>>,
module => <<"tempo/naive_datetime"/utf8>>,
function => <<"literal"/utf8>>,
line => 88});
{error, {naive_date_time_date_parse_error, _, {date_invalid_format, _}}} ->
erlang:error(#{gleam_error => panic,
message => <<"Invalid date format in naive datetime literal"/utf8>>,
module => <<"tempo/naive_datetime"/utf8>>,
function => <<"literal"/utf8>>,
line => 92});
{error,
{naive_date_time_date_parse_error,
_,
{date_out_of_bounds, _, {date_day_out_of_bounds, _}}}} ->
erlang:error(#{gleam_error => panic,
message => <<"Invalid date day in naive datetime literal"/utf8>>,
module => <<"tempo/naive_datetime"/utf8>>,
function => <<"literal"/utf8>>,
line => 96});
{error,
{naive_date_time_date_parse_error,
_,
{date_out_of_bounds, _, {date_month_out_of_bounds, _}}}} ->
erlang:error(#{gleam_error => panic,
message => <<"Invalid date month in naive datetime literal"/utf8>>,
module => <<"tempo/naive_datetime"/utf8>>,
function => <<"literal"/utf8>>,
line => 100});
{error,
{naive_date_time_date_parse_error,
_,
{date_out_of_bounds, _, {date_year_out_of_bounds, _}}}} ->
erlang:error(#{gleam_error => panic,
message => <<"Invalid date year in naive datetime literal"/utf8>>,
module => <<"tempo/naive_datetime"/utf8>>,
function => <<"literal"/utf8>>,
line => 104})
end.
-file("/home/john/Repos/tempo/src/tempo/naive_datetime.gleam", 182).
-spec to_string(tempo:naive_date_time()) -> binary().
to_string(Datetime) ->
tempo:naive_datetime_to_string(Datetime).
-file("/home/john/Repos/tempo/src/tempo/naive_datetime.gleam", 195).
-spec to_tuple(tempo:naive_date_time()) -> {{integer(), integer(), integer()},
{integer(), integer(), integer()}}.
to_tuple(Naive_datetime) ->
{{begin
_pipe = Naive_datetime,
_pipe@1 = tempo:naive_datetime_get_date(_pipe),
tempo:date_get_year(_pipe@1)
end,
tempo@month:to_int(
begin
_pipe@2 = Naive_datetime,
_pipe@3 = tempo:naive_datetime_get_date(_pipe@2),
tempo:date_get_month(_pipe@3)
end
),
begin
_pipe@4 = Naive_datetime,
_pipe@5 = tempo:naive_datetime_get_date(_pipe@4),
tempo:date_get_day(_pipe@5)
end},
{begin
_pipe@6 = Naive_datetime,
_pipe@7 = tempo:naive_datetime_get_time(_pipe@6),
tempo:time_get_hour(_pipe@7)
end,
begin
_pipe@8 = Naive_datetime,
_pipe@9 = tempo:naive_datetime_get_time(_pipe@8),
tempo:time_get_minute(_pipe@9)
end,
begin
_pipe@10 = Naive_datetime,
_pipe@11 = tempo:naive_datetime_get_time(_pipe@10),
tempo:time_get_second(_pipe@11)
end}}.
-file("/home/john/Repos/tempo/src/tempo/naive_datetime.gleam", 245).
-spec parse(binary(), tempo:naive_date_time_format()) -> {ok,
tempo:naive_date_time()} |
{error, tempo@error:naive_date_time_parse_error()}.
parse(Str, Format) ->
Format_str = tempo:get_naive_datetime_format_str(Format),
gleam@result:'try'(
begin
_pipe = tempo:consume_format(Str, Format_str),
gleam@result:map_error(
_pipe,
fun(_capture) -> {naive_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) ->
{naive_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) ->
{naive_date_time_time_parse_error,
Str,
_capture@2}
end
)
end,
fun(Time) -> {ok, new(Date, Time)} end
)
end
)
end
).
-file("/home/john/Repos/tempo/src/tempo/naive_datetime.gleam", 284).
-spec parse_any(binary()) -> {ok, tempo:naive_date_time()} |
{error, tempo@error:naive_date_time_parse_error()}.
parse_any(Str) ->
case tempo:parse_any(Str) of
{{some, Date}, {some, Time}, _} ->
{ok, new(Date, Time)};
{_, none, _} ->
{error,
{naive_date_time_invalid_format,
<<"Unable to find date in "/utf8, Str/binary>>}};
{none, _, _} ->
{error,
{naive_date_time_invalid_format,
<<"Unable to find time in "/utf8, Str/binary>>}}
end.
-file("/home/john/Repos/tempo/src/tempo/naive_datetime.gleam", 309).
-spec describe_parse_error(tempo@error:naive_date_time_parse_error()) -> binary().
describe_parse_error(Error) ->
tempo@error:describe_naive_datetime_parse_error(Error).
-file("/home/john/Repos/tempo/src/tempo/naive_datetime.gleam", 425).
-spec get_date(tempo:naive_date_time()) -> tempo:date().
get_date(Datetime) ->
tempo:naive_datetime_get_date(Datetime).
-file("/home/john/Repos/tempo/src/tempo/naive_datetime.gleam", 438).
-spec get_time(tempo:naive_date_time()) -> tempo:time().
get_time(Datetime) ->
tempo:naive_datetime_get_time(Datetime).
-file("/home/john/Repos/tempo/src/tempo/naive_datetime.gleam", 355).
-spec format(tempo:naive_date_time(), tempo:naive_date_time_format()) -> binary().
format(Naive_datetime, Format) ->
Format_str = tempo:get_naive_datetime_format_str(Format),
_assert_subject = gleam@regexp:from_string(
<<"\\[([^\\]]+)\\]|Y{1,4}|M{1,4}|D{1,2}|d{1,4}|H{1,2}|h{1,2}|a|A|m{1,2}|s{1,2}|Z{1,2}|z{1,2}|SSSSS|SSSS|SSS|."/utf8>>
),
{ok, Re} = case _assert_subject of
{ok, _} -> _assert_subject;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
value => _assert_fail,
module => <<"tempo/naive_datetime"/utf8>>,
function => <<"format"/utf8>>,
line => 361})
end,
_pipe = gleam@regexp:scan(Re, Format_str),
_pipe@1 = lists:reverse(_pipe),
_pipe@6 = gleam@list:fold(_pipe@1, [], fun(Acc, Match) -> case Match of
{match, Content, []} ->
[begin
_pipe@2 = Content,
_pipe@4 = tempo:date_replace_format(
_pipe@2,
begin
_pipe@3 = Naive_datetime,
get_date(_pipe@3)
end
),
tempo:time_replace_format(
_pipe@4,
begin
_pipe@5 = Naive_datetime,
get_time(_pipe@5)
end
)
end |
Acc];
{match, _, [{some, Sub}]} ->
[Sub | Acc];
{match, Content@1, _} ->
[Content@1 | Acc]
end end),
gleam@string:join(_pipe@6, <<""/utf8>>).
-file("/home/john/Repos/tempo/src/tempo/naive_datetime.gleam", 451).
-spec drop_time(tempo:naive_date_time()) -> tempo:naive_date_time().
drop_time(Datetime) ->
_pipe = tempo:naive_datetime_get_date(Datetime),
tempo:naive_datetime(_pipe, {time_of_day, 0}).
-file("/home/john/Repos/tempo/src/tempo/naive_datetime.gleam", 466).
-spec set_offset(tempo:naive_date_time(), tempo:offset()) -> tempo:date_time().
set_offset(Datetime, Offset) ->
tempo:naive_datetime_set_offset(Datetime, Offset).
-file("/home/john/Repos/tempo/src/tempo/naive_datetime.gleam", 397).
-spec as_utc(tempo:naive_date_time()) -> tempo:date_time().
as_utc(Datetime) ->
set_offset(Datetime, {offset, 0}).
-file("/home/john/Repos/tempo/src/tempo/naive_datetime.gleam", 412).
-spec as_local(tempo:naive_date_time()) -> tempo:date_time().
as_local(Datetime) ->
set_offset(Datetime, tempo@offset:local()).
-file("/home/john/Repos/tempo/src/tempo/naive_datetime.gleam", 488).
-spec compare(tempo:naive_date_time(), tempo:naive_date_time()) -> gleam@order:order().
compare(A, B) ->
tempo:naive_datetime_compare(A, B).
-file("/home/john/Repos/tempo/src/tempo/naive_datetime.gleam", 512).
-spec is_earlier(tempo:naive_date_time(), tempo:naive_date_time()) -> boolean().
is_earlier(A, B) ->
tempo:naive_datetime_is_earlier(A, B).
-file("/home/john/Repos/tempo/src/tempo/naive_datetime.gleam", 536).
-spec is_earlier_or_equal(tempo:naive_date_time(), tempo:naive_date_time()) -> boolean().
is_earlier_or_equal(A, B) ->
tempo:naive_datetime_is_earlier_or_equal(A, B).
-file("/home/john/Repos/tempo/src/tempo/naive_datetime.gleam", 562).
-spec is_equal(tempo:naive_date_time(), tempo:naive_date_time()) -> boolean().
is_equal(A, B) ->
compare(A, B) =:= eq.
-file("/home/john/Repos/tempo/src/tempo/naive_datetime.gleam", 585).
-spec is_later(tempo:naive_date_time(), tempo:naive_date_time()) -> boolean().
is_later(A, B) ->
compare(A, B) =:= gt.
-file("/home/john/Repos/tempo/src/tempo/naive_datetime.gleam", 609).
-spec is_later_or_equal(tempo:naive_date_time(), tempo:naive_date_time()) -> boolean().
is_later_or_equal(A, B) ->
tempo:naive_datetime_is_later_or_equal(A, B).
-file("/home/john/Repos/tempo/src/tempo/naive_datetime.gleam", 637).
-spec difference(tempo:naive_date_time(), tempo:naive_date_time()) -> gleam@time@duration:duration().
difference(A, B) ->
tempo:naive_datetime_difference(A, B).
-file("/home/john/Repos/tempo/src/tempo/naive_datetime.gleam", 667).
-spec as_period(tempo:naive_date_time(), tempo:naive_date_time()) -> tempo:period().
as_period(Start, End) ->
tempo:period_new_naive(Start, End).
-file("/home/john/Repos/tempo/src/tempo/naive_datetime.gleam", 683).
-spec add(tempo:naive_date_time(), gleam@time@duration:duration()) -> tempo:naive_date_time().
add(Datetime, Duration_to_add) ->
tempo:naive_datetime_add(Datetime, Duration_to_add).
-file("/home/john/Repos/tempo/src/tempo/naive_datetime.gleam", 699).
-spec subtract(tempo:naive_date_time(), gleam@time@duration:duration()) -> tempo:naive_date_time().
subtract(Datetime, Duration_to_subtract) ->
tempo:naive_datetime_subtract(Datetime, Duration_to_subtract).
-file("/home/john/Repos/tempo/src/tempo/naive_datetime.gleam", 723).
-spec time_left_in_day(tempo:naive_date_time()) -> tempo:time().
time_left_in_day(Naive_datetime) ->
_pipe = Naive_datetime,
_pipe@1 = tempo:naive_datetime_get_time(_pipe),
tempo@time:left_in_day(_pipe@1).