Packages

A timezone data provider for Gleam!

Current section

Files

Jump to
gtz src gtz.erl
Raw

src/gtz.erl

-module(gtz).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([timezone/1, local_name/0]).
-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(" Functions to provide simple timezone support for other Gleam datetime libraries.\n").
-file("src/gtz.gleam", 22).
?DOC(
" Constructs a TimeZoneProvider type to be used with the Tempo package. \n"
" Returns an error if the timezone is not valid.\n"
" \n"
" ## Examples\n"
" \n"
" ```gleam\n"
" import tempo/datetime\n"
"\n"
" let assert Ok(tz) = gtz.timezone(\"America/New_York\")\n"
" datetime.literal(\"2024-06-21T06:30:02.334Z\")\n"
" |> datetime.to_timezone(tz)\n"
" |> datetime.to_string\n"
" // -> \"2024-01-03T02:30:02.334-04:00\"\n"
" ```\n"
).
-spec timezone(binary()) -> {ok, tempo:time_zone_provider()} | {error, nil}.
timezone(Name) ->
case 'Elixir.GTZ_FFI':is_valid_timezone(Name) of
true ->
{ok,
{time_zone_provider,
fun() -> Name end,
fun(Utc_naive_datetime) ->
{{Year, Month, Day}, {Hour, Minute, Second}} = tempo@naive_datetime:to_tuple(
Utc_naive_datetime
),
_assert_subject = begin
_pipe = 'Elixir.GTZ_FFI':calculate_offset(
Year,
Month,
Day,
Hour,
Minute,
Second,
Name
),
_pipe@1 = tempo@duration:minutes(_pipe),
tempo@offset:from_duration(_pipe@1)
end,
{ok, Offset} = 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 => <<"gtz"/utf8>>,
function => <<"timezone"/utf8>>,
line => 32})
end,
Offset
end}};
false ->
{error, nil}
end.
-file("src/gtz.gleam", 71).
?DOC(
" Returns the name of the host system's timezone.\n"
" \n"
" ## Examples\n"
" \n"
" ```gleam\n"
" gtz.local_name()\n"
" // -> \"Europe/London\"\n"
" ```\n"
).
-spec local_name() -> binary().
local_name() ->
'Elixir.Timex.Timezone.Local':lookup().