Current section

Files

Jump to
gtempo src tempo@date.erl
Raw

src/tempo@date.erl

-module(tempo@date).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([new/3, get_year/1, get_month/1, get_day/1, get_month_year/1, to_string/1, parse/2, parse_any/1, describe_parse_error/1, format/2, from_tuple/1, from_string/1, literal/1, describe_out_of_bounds_error/1, to_tuple/1, from_dynamic_string/1, from_unix_seconds/1, current_local/0, current_utc/0, to_unix_seconds/1, from_unix_milli/1, to_unix_milli/1, from_unix_micro/1, to_unix_micro/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, to_day_of_week_number/1, to_day_of_week/1, day_of_week_to_short_string/1, day_of_week_to_long_string/1, next_day_of_week/2, prior_day_of_week/2, is_weekend/1, first_of_month/1, last_of_month/1]).
-export_type([day_of_week/0]).
-type day_of_week() :: sun | mon | tue | wed | thu | fri | sat.
-file("/home/john/Repos/tempo/src/tempo/date.gleam", 72).
-spec new(integer(), integer(), integer()) -> {ok, tempo:date()} |
{error, tempo@error:date_out_of_bounds_error()}.
new(Year, Month, Day) ->
tempo:new_date(Year, Month, Day).
-file("/home/john/Repos/tempo/src/tempo/date.gleam", 157).
-spec get_year(tempo:date()) -> integer().
get_year(Date) ->
_pipe = Date,
tempo:date_get_year(_pipe).
-file("/home/john/Repos/tempo/src/tempo/date.gleam", 170).
-spec get_month(tempo:date()) -> tempo:month().
get_month(Date) ->
_pipe = Date,
tempo:date_get_month(_pipe).
-file("/home/john/Repos/tempo/src/tempo/date.gleam", 183).
-spec get_day(tempo:date()) -> integer().
get_day(Date) ->
_pipe = Date,
tempo:date_get_day(_pipe).
-file("/home/john/Repos/tempo/src/tempo/date.gleam", 196).
-spec get_month_year(tempo:date()) -> tempo:month_year().
get_month_year(Date) ->
_pipe = Date,
tempo:date_get_month_year(_pipe).
-file("/home/john/Repos/tempo/src/tempo/date.gleam", 245).
-spec split_int_tuple(binary(), binary()) -> {ok,
{integer(), integer(), integer()}} |
{error, tempo@error:date_parse_error()}.
split_int_tuple(Date, Delim) ->
_pipe = gleam@string:split(Date, Delim),
_pipe@1 = gleam@list:map(_pipe, fun gleam_stdlib:parse_int/1),
_pipe@2 = gleam@result:all(_pipe@1),
_pipe@3 = gleam@result:'try'(_pipe@2, fun(Date@1) -> case Date@1 of
[Year, Month, Day] ->
{ok, {Year, Month, Day}};
_ ->
{error, nil}
end end),
gleam@result:replace_error(_pipe@3, {date_invalid_format, Date}).
-file("/home/john/Repos/tempo/src/tempo/date.gleam", 270).
-spec to_string(tempo:date()) -> binary().
to_string(Date) ->
tempo:date_to_string(Date).
-file("/home/john/Repos/tempo/src/tempo/date.gleam", 299).
-spec parse(binary(), tempo:date_format()) -> {ok, tempo:date()} |
{error, tempo@error:date_parse_error()}.
parse(Str, Format) ->
Format_str = tempo:get_date_format_str(Format),
gleam@result:'try'(
begin
_pipe = tempo:consume_format(Str, Format_str),
gleam@result:map_error(
_pipe,
fun(_capture) -> {date_invalid_format, _capture} end
)
end,
fun(_use0) ->
{Parts, _} = _use0,
tempo:find_date(Parts)
end
).
-file("/home/john/Repos/tempo/src/tempo/date.gleam", 328).
-spec parse_any(binary()) -> {ok, tempo:date()} |
{error, tempo@error:date_parse_error()}.
parse_any(Str) ->
case tempo:parse_any(Str) of
{{some, Date}, _, _} ->
{ok, Date};
{none, _, _} ->
{error,
{date_invalid_format,
<<"Unable to find date in "/utf8, Str/binary>>}}
end.
-file("/home/john/Repos/tempo/src/tempo/date.gleam", 345).
-spec describe_parse_error(tempo@error:date_parse_error()) -> binary().
describe_parse_error(Error) ->
tempo@error:describe_date_parse_error(Error).
-file("/home/john/Repos/tempo/src/tempo/date.gleam", 382).
-spec format(tempo:date(), tempo:date_format()) -> binary().
format(Date, Format) ->
Format_str = tempo:get_date_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|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/date"/utf8>>,
function => <<"format"/utf8>>,
line => 385})
end,
_pipe = gleam@regexp:scan(Re, Format_str),
_pipe@1 = lists:reverse(_pipe),
_pipe@2 = gleam@list:fold(_pipe@1, [], fun(Acc, Match) -> case Match of
{match, Content, []} ->
[tempo:date_replace_format(Content, Date) | Acc];
{match, _, [{some, Sub}]} ->
[Sub | Acc];
{match, Content@1, _} ->
[Content@1 | Acc]
end end),
gleam@string:join(_pipe@2, <<""/utf8>>).
-file("/home/john/Repos/tempo/src/tempo/date.gleam", 427).
-spec from_tuple({integer(), integer(), integer()}) -> {ok, tempo:date()} |
{error, tempo@error:date_out_of_bounds_error()}.
from_tuple(Date) ->
tempo:date_from_tuple(Date).
-file("/home/john/Repos/tempo/src/tempo/date.gleam", 220).
-spec from_string(binary()) -> {ok, tempo:date()} |
{error, tempo@error:date_parse_error()}.
from_string(Date) ->
gleam@result:'try'(
begin
_pipe = split_int_tuple(Date, <<"-"/utf8>>),
_pipe@1 = gleam@result:try_recover(
_pipe,
fun(_) -> split_int_tuple(Date, <<"/"/utf8>>) end
),
_pipe@2 = gleam@result:try_recover(
_pipe@1,
fun(_) -> split_int_tuple(Date, <<"."/utf8>>) end
),
_pipe@3 = gleam@result:try_recover(
_pipe@2,
fun(_) -> split_int_tuple(Date, <<"_"/utf8>>) end
),
_pipe@4 = gleam@result:try_recover(
_pipe@3,
fun(_) -> split_int_tuple(Date, <<" "/utf8>>) end
),
gleam@result:try_recover(
_pipe@4,
fun(_) ->
Year = begin
_pipe@5 = gleam@string:slice(Date, 0, 4),
gleam_stdlib:parse_int(_pipe@5)
end,
Month = begin
_pipe@6 = gleam@string:slice(Date, 4, 2),
gleam_stdlib:parse_int(_pipe@6)
end,
Day = begin
_pipe@7 = gleam@string:slice(Date, 6, 2),
gleam_stdlib:parse_int(_pipe@7)
end,
case {Year, Month, Day} of
{{ok, Year@1}, {ok, Month@1}, {ok, Day@1}} ->
{ok, {Year@1, Month@1, Day@1}};
{_, _, _} ->
{error, {date_invalid_format, Date}}
end
end
)
end,
fun(Parts) -> _pipe@8 = from_tuple(Parts),
gleam@result:map_error(
_pipe@8,
fun(_capture) -> {date_out_of_bounds, Date, _capture} end
) end
).
-file("/home/john/Repos/tempo/src/tempo/date.gleam", 106).
-spec literal(binary()) -> tempo:date().
literal(Date) ->
case from_string(Date) of
{ok, Date@1} ->
Date@1;
{error, {date_invalid_format, _}} ->
erlang:error(#{gleam_error => panic,
message => <<"Invalid date literal format"/utf8>>,
module => <<"tempo/date"/utf8>>,
function => <<"literal"/utf8>>,
line => 110});
{error, {date_out_of_bounds, _, {date_day_out_of_bounds, _}}} ->
erlang:error(#{gleam_error => panic,
message => <<"Invalid date literal day value"/utf8>>,
module => <<"tempo/date"/utf8>>,
function => <<"literal"/utf8>>,
line => 112});
{error, {date_out_of_bounds, _, {date_month_out_of_bounds, _}}} ->
erlang:error(#{gleam_error => panic,
message => <<"Invalid date literal month value"/utf8>>,
module => <<"tempo/date"/utf8>>,
function => <<"literal"/utf8>>,
line => 114});
{error, {date_out_of_bounds, _, {date_year_out_of_bounds, _}}} ->
erlang:error(#{gleam_error => panic,
message => <<"Invalid date literal year value"/utf8>>,
module => <<"tempo/date"/utf8>>,
function => <<"literal"/utf8>>,
line => 116})
end.
-file("/home/john/Repos/tempo/src/tempo/date.gleam", 442).
-spec describe_out_of_bounds_error(tempo@error:date_out_of_bounds_error()) -> binary().
describe_out_of_bounds_error(Error) ->
tempo@error:describe_date_out_of_bounds_error(Error).
-file("/home/john/Repos/tempo/src/tempo/date.gleam", 456).
-spec to_tuple(tempo:date()) -> {integer(), integer(), integer()}.
to_tuple(Date) ->
{begin
_pipe = Date,
tempo:date_get_year(_pipe)
end,
tempo@month:to_int(
begin
_pipe@1 = Date,
tempo:date_get_month(_pipe@1)
end
),
begin
_pipe@2 = Date,
tempo:date_get_day(_pipe@2)
end}.
-file("/home/john/Repos/tempo/src/tempo/date.gleam", 486).
-spec from_dynamic_string(gleam@dynamic:dynamic_()) -> {ok, tempo:date()} |
{error, list(gleam@dynamic:decode_error())}.
from_dynamic_string(Dynamic_string) ->
gleam@result:'try'(
gleam@dynamic:string(Dynamic_string),
fun(Dt) -> case from_string(Dt) of
{ok, Date} ->
{ok, Date};
{error, Tempo_error} ->
{error,
[{decode_error,
<<"tempo.Date"/utf8>>,
case Tempo_error of
{date_invalid_format, Msg} ->
Msg;
{date_out_of_bounds, Msg@1, _} ->
Msg@1
end,
[]}]}
end end
).
-file("/home/john/Repos/tempo/src/tempo/date.gleam", 524).
-spec from_unix_seconds(integer()) -> tempo:date().
from_unix_seconds(Unix_ts) ->
tempo:date_from_unix_seconds(Unix_ts).
-file("/home/john/Repos/tempo/src/tempo/date.gleam", 129).
-spec current_local() -> tempo:date().
current_local() ->
_pipe = (tempo_ffi:now() + tempo:offset_local_micro()) div 1000000,
from_unix_seconds(_pipe).
-file("/home/john/Repos/tempo/src/tempo/date.gleam", 143).
-spec current_utc() -> tempo:date().
current_utc() ->
_pipe = tempo_ffi:now() div 1000000,
from_unix_seconds(_pipe).
-file("/home/john/Repos/tempo/src/tempo/date.gleam", 544).
-spec to_unix_seconds(tempo:date()) -> integer().
to_unix_seconds(Date) ->
tempo:date_to_unix_seconds(Date).
-file("/home/john/Repos/tempo/src/tempo/date.gleam", 565).
-spec from_unix_milli(integer()) -> tempo:date().
from_unix_milli(Unix_ts) ->
from_unix_seconds(Unix_ts div 1000).
-file("/home/john/Repos/tempo/src/tempo/date.gleam", 585).
-spec to_unix_milli(tempo:date()) -> integer().
to_unix_milli(Date) ->
to_unix_seconds(Date) * 1000.
-file("/home/john/Repos/tempo/src/tempo/date.gleam", 606).
-spec from_unix_micro(integer()) -> tempo:date().
from_unix_micro(Unix_ts) ->
tempo:date_from_unix_micro(Unix_ts).
-file("/home/john/Repos/tempo/src/tempo/date.gleam", 626).
-spec to_unix_micro(tempo:date()) -> integer().
to_unix_micro(Date) ->
tempo:date_to_unix_micro(Date).
-file("/home/john/Repos/tempo/src/tempo/date.gleam", 651).
-spec compare(tempo:date(), tempo:date()) -> gleam@order:order().
compare(A, B) ->
tempo:date_compare(A, B).
-file("/home/john/Repos/tempo/src/tempo/date.gleam", 670).
-spec is_earlier(tempo:date(), tempo:date()) -> boolean().
is_earlier(A, B) ->
tempo:date_is_earlier(A, B).
-file("/home/john/Repos/tempo/src/tempo/date.gleam", 688).
-spec is_earlier_or_equal(tempo:date(), tempo:date()) -> boolean().
is_earlier_or_equal(A, B) ->
tempo:date_is_earlier_or_equal(A, B).
-file("/home/john/Repos/tempo/src/tempo/date.gleam", 701).
-spec is_equal(tempo:date(), tempo:date()) -> boolean().
is_equal(A, B) ->
tempo:date_is_equal(A, B).
-file("/home/john/Repos/tempo/src/tempo/date.gleam", 720).
-spec is_later(tempo:date(), tempo:date()) -> boolean().
is_later(A, B) ->
tempo:date_is_later(A, B).
-file("/home/john/Repos/tempo/src/tempo/date.gleam", 739).
-spec is_later_or_equal(tempo:date(), tempo:date()) -> boolean().
is_later_or_equal(A, B) ->
tempo:date_is_later_or_equal(A, B).
-file("/home/john/Repos/tempo/src/tempo/date.gleam", 758).
-spec difference(tempo:date(), tempo:date()) -> integer().
difference(A, B) ->
tempo:date_days_apart(A, B).
-file("/home/john/Repos/tempo/src/tempo/date.gleam", 780).
-spec as_period(tempo:date(), tempo:date()) -> tempo:period().
as_period(Start, End) ->
tempo:period_new_date(Start, End).
-file("/home/john/Repos/tempo/src/tempo/date.gleam", 799).
-spec add(tempo:date(), integer()) -> tempo:date().
add(Date, Days) ->
tempo:date_add(Date, Days).
-file("/home/john/Repos/tempo/src/tempo/date.gleam", 818).
-spec subtract(tempo:date(), integer()) -> tempo:date().
subtract(Date, Days) ->
tempo:date_subtract(Date, Days).
-file("/home/john/Repos/tempo/src/tempo/date.gleam", 832).
-spec to_day_of_week_number(tempo:date()) -> integer().
to_day_of_week_number(Date) ->
tempo:date_to_day_of_week_number(Date).
-file("/home/john/Repos/tempo/src/tempo/date.gleam", 846).
-spec to_day_of_week(tempo:date()) -> day_of_week().
to_day_of_week(Date) ->
case to_day_of_week_number(Date) of
0 ->
sun;
1 ->
mon;
2 ->
tue;
3 ->
wed;
4 ->
thu;
5 ->
fri;
6 ->
sat;
_ ->
erlang:error(#{gleam_error => panic,
message => <<"Invalid day of week found after modulo by 7"/utf8>>,
module => <<"tempo/date"/utf8>>,
function => <<"to_day_of_week"/utf8>>,
line => 855})
end.
-file("/home/john/Repos/tempo/src/tempo/date.gleam", 867).
-spec day_of_week_to_short_string(day_of_week()) -> binary().
day_of_week_to_short_string(Day_of_week) ->
case Day_of_week of
sun ->
<<"Sun"/utf8>>;
mon ->
<<"Mon"/utf8>>;
tue ->
<<"Tue"/utf8>>;
wed ->
<<"Wed"/utf8>>;
thu ->
<<"Thu"/utf8>>;
fri ->
<<"Fri"/utf8>>;
sat ->
<<"Sat"/utf8>>
end.
-file("/home/john/Repos/tempo/src/tempo/date.gleam", 887).
-spec day_of_week_to_long_string(day_of_week()) -> binary().
day_of_week_to_long_string(Day_of_week) ->
case Day_of_week of
sun ->
<<"Sunday"/utf8>>;
mon ->
<<"Monday"/utf8>>;
tue ->
<<"Tuesday"/utf8>>;
wed ->
<<"Wednesday"/utf8>>;
thu ->
<<"Thursday"/utf8>>;
fri ->
<<"Friday"/utf8>>;
sat ->
<<"Saturday"/utf8>>
end.
-file("/home/john/Repos/tempo/src/tempo/date.gleam", 915).
-spec next_day_of_week(tempo:date(), day_of_week()) -> tempo:date().
next_day_of_week(Date, Dow) ->
Next = begin
_pipe = Date,
add(_pipe, 1)
end,
case begin
_pipe@1 = Next,
to_day_of_week(_pipe@1)
end
=:= Dow of
true ->
Next;
false ->
next_day_of_week(Next, Dow)
end.
-file("/home/john/Repos/tempo/src/tempo/date.gleam", 943).
-spec prior_day_of_week(tempo:date(), day_of_week()) -> tempo:date().
prior_day_of_week(Date, Dow) ->
Prior = begin
_pipe = Date,
subtract(_pipe, 1)
end,
case begin
_pipe@1 = Prior,
to_day_of_week(_pipe@1)
end
=:= Dow of
true ->
Prior;
false ->
prior_day_of_week(Prior, Dow)
end.
-file("/home/john/Repos/tempo/src/tempo/date.gleam", 964).
-spec is_weekend(tempo:date()) -> boolean().
is_weekend(Date) ->
case to_day_of_week(Date) of
sat ->
true;
sun ->
true;
_ ->
false
end.
-file("/home/john/Repos/tempo/src/tempo/date.gleam", 980).
-spec first_of_month(tempo:date()) -> tempo:date().
first_of_month(Date) ->
tempo:date(
begin
_pipe = Date,
tempo:date_get_year(_pipe)
end,
begin
_pipe@1 = Date,
tempo:date_get_month(_pipe@1)
end,
1
).
-file("/home/john/Repos/tempo/src/tempo/date.gleam", 993).
-spec last_of_month(tempo:date()) -> tempo:date().
last_of_month(Date) ->
tempo:date(
begin
_pipe = Date,
tempo:date_get_year(_pipe)
end,
begin
_pipe@1 = Date,
tempo:date_get_month(_pipe@1)
end,
tempo@month:days(
begin
_pipe@2 = Date,
tempo:date_get_month(_pipe@2)
end,
begin
_pipe@3 = Date,
tempo:date_get_year(_pipe@3)
end
)
).