Packages

Date formatter extension for birl

Current section

Files

Jump to
dateformat src dateformat@internal@util.erl
Raw

src/dateformat@internal@util.erl

-module(dateformat@internal@util).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([to_ordinal/1]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
?MODULEDOC(false).
-file("src/dateformat/internal/util.gleam", 3).
?DOC(false).
-spec to_ordinal(integer()) -> binary().
to_ordinal(Num) ->
case Num of
N when N > 100 ->
<<(erlang:integer_to_binary(Num div 100))/binary,
(to_ordinal(Num rem 100))/binary>>;
N@1 when (N@1 > 3) andalso (N@1 < 20) ->
<<(erlang:integer_to_binary(Num))/binary, "th"/utf8>>;
N@2 ->
<<(erlang:integer_to_binary(Num))/binary, (case N@2 rem 10 of
1 ->
<<"st"/utf8>>;
2 ->
<<"nd"/utf8>>;
3 ->
<<"rd"/utf8>>;
_ ->
<<"th"/utf8>>
end)/binary>>
end.