Current section
Files
Jump to
Current section
Files
src/tempo@month.erl
-module(tempo@month).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([to_short_string/1, to_long_string/1, from_short_string/1, from_long_string/1, from_string/1, to_int/1, from_int/1, days/2, next/1, prior/1]).
-spec to_short_string(tempo:month()) -> binary().
to_short_string(Month) ->
case Month of
jan ->
<<"Jan"/utf8>>;
feb ->
<<"Feb"/utf8>>;
mar ->
<<"Mar"/utf8>>;
apr ->
<<"Apr"/utf8>>;
may ->
<<"May"/utf8>>;
jun ->
<<"Jun"/utf8>>;
jul ->
<<"Jul"/utf8>>;
aug ->
<<"Aug"/utf8>>;
sep ->
<<"Sep"/utf8>>;
oct ->
<<"Oct"/utf8>>;
nov ->
<<"Nov"/utf8>>;
dec ->
<<"Dec"/utf8>>
end.
-spec to_long_string(tempo:month()) -> binary().
to_long_string(Month) ->
case Month of
jan ->
<<"January"/utf8>>;
feb ->
<<"February"/utf8>>;
mar ->
<<"March"/utf8>>;
apr ->
<<"April"/utf8>>;
may ->
<<"May"/utf8>>;
jun ->
<<"June"/utf8>>;
jul ->
<<"July"/utf8>>;
aug ->
<<"August"/utf8>>;
sep ->
<<"September"/utf8>>;
oct ->
<<"October"/utf8>>;
nov ->
<<"November"/utf8>>;
dec ->
<<"December"/utf8>>
end.
-spec from_short_string(binary()) -> {ok, tempo:month()} |
{error, tempo:error()}.
from_short_string(Month) ->
case Month of
<<"Jan"/utf8>> ->
{ok, jan};
<<"Feb"/utf8>> ->
{ok, feb};
<<"Mar"/utf8>> ->
{ok, mar};
<<"Apr"/utf8>> ->
{ok, apr};
<<"May"/utf8>> ->
{ok, may};
<<"Jun"/utf8>> ->
{ok, jun};
<<"Jul"/utf8>> ->
{ok, jul};
<<"Aug"/utf8>> ->
{ok, aug};
<<"Sep"/utf8>> ->
{ok, sep};
<<"Oct"/utf8>> ->
{ok, oct};
<<"Nov"/utf8>> ->
{ok, nov};
<<"Dec"/utf8>> ->
{ok, dec};
_ ->
{error, month_invalid_format}
end.
-spec from_long_string(binary()) -> {ok, tempo:month()} | {error, tempo:error()}.
from_long_string(Month) ->
case Month of
<<"January"/utf8>> ->
{ok, jan};
<<"February"/utf8>> ->
{ok, feb};
<<"March"/utf8>> ->
{ok, mar};
<<"April"/utf8>> ->
{ok, apr};
<<"May"/utf8>> ->
{ok, may};
<<"June"/utf8>> ->
{ok, jun};
<<"July"/utf8>> ->
{ok, jul};
<<"August"/utf8>> ->
{ok, aug};
<<"September"/utf8>> ->
{ok, sep};
<<"October"/utf8>> ->
{ok, oct};
<<"November"/utf8>> ->
{ok, nov};
<<"December"/utf8>> ->
{ok, dec};
_ ->
{error, month_invalid_format}
end.
-spec from_string(binary()) -> {ok, tempo:month()} | {error, tempo:error()}.
from_string(Month) ->
_pipe = from_short_string(Month),
gleam@result:try_recover(_pipe, fun(_) -> from_long_string(Month) end).
-spec to_int(tempo:month()) -> integer().
to_int(Month) ->
case Month of
jan ->
1;
feb ->
2;
mar ->
3;
apr ->
4;
may ->
5;
jun ->
6;
jul ->
7;
aug ->
8;
sep ->
9;
oct ->
10;
nov ->
11;
dec ->
12
end.
-spec from_int(integer()) -> {ok, tempo:month()} | {error, tempo:error()}.
from_int(Month) ->
case Month of
1 ->
{ok, jan};
2 ->
{ok, feb};
3 ->
{ok, mar};
4 ->
{ok, apr};
5 ->
{ok, may};
6 ->
{ok, jun};
7 ->
{ok, jul};
8 ->
{ok, aug};
9 ->
{ok, sep};
10 ->
{ok, oct};
11 ->
{ok, nov};
12 ->
{ok, dec};
_ ->
{error, month_out_of_bounds}
end.
-spec days(tempo:month(), integer()) -> integer().
days(Month, Year) ->
case Month of
jan ->
31;
mar ->
31;
may ->
31;
jul ->
31;
aug ->
31;
oct ->
31;
dec ->
31;
_ ->
case Month of
apr ->
30;
jun ->
30;
sep ->
30;
nov ->
30;
_ ->
case tempo@year:is_leap_year(Year) of
true ->
29;
false ->
28
end
end
end.
-spec next(tempo:month()) -> tempo:month().
next(Month) ->
case Month of
jan ->
feb;
feb ->
mar;
mar ->
apr;
apr ->
may;
may ->
jun;
jun ->
jul;
jul ->
aug;
aug ->
sep;
sep ->
oct;
oct ->
nov;
nov ->
dec;
dec ->
jan
end.
-spec prior(tempo:month()) -> tempo:month().
prior(Month) ->
case Month of
jan ->
dec;
feb ->
jan;
mar ->
feb;
apr ->
mar;
may ->
apr;
jun ->
may;
jul ->
jun;
aug ->
jul;
sep ->
aug;
oct ->
sep;
nov ->
oct;
dec ->
nov
end.