Packages
mist
4.0.2
6.0.3
6.0.2
6.0.1
6.0.0
5.0.4
5.0.3
5.0.2
5.0.1
5.0.0
5.0.0-rc1
4.0.7
4.0.6
4.0.5
4.0.4
4.0.3
4.0.2
4.0.1
4.0.0
3.0.0
2.0.0
1.2.0
1.1.0
1.0.0
1.0.0-rc3
1.0.0-rc2
1.0.0-rc1
0.17.0
0.15.0
0.14.3
0.14.2
0.14.1
0.14.0
0.13.2
0.13.1
0.13.0
0.12.0
0.11.0
0.10.0
0.9.4
0.9.3
0.9.2
0.9.1
0.9.0
0.8.3
0.8.2
0.8.1
0.8.0
0.7.1
0.7.0
0.6.1
0.6.0
0.5.2
0.5.1
0.5.0
0.4.5
0.4.4
0.4.3
0.4.2
0.4.1
0.4.0
0.3.3
0.3.2
0.3.1
0.3.0
0.2.1
0.2.0
0.1.3
a misty Gleam web server
Current section
Files
Jump to
Current section
Files
src/mist@internal@clock.erl
-module(mist@internal@clock).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([stop/1, start/2, get_date/0]).
-export_type([clock_message/0, clock_table/0, table_key/0, ets_opts/0]).
-type clock_message() :: set_time.
-type clock_table() :: mist_clock.
-type table_key() :: date_header.
-type ets_opts() :: set |
protected |
named_table |
{read_concurrency, boolean()}.
-file("/Users/amanning/code/mist/src/mist/internal/clock.gleam", 56).
-spec stop(any()) -> gleam@erlang@atom:atom_().
stop(_) ->
erlang:binary_to_atom(<<"ok"/utf8>>).
-file("/Users/amanning/code/mist/src/mist/internal/clock.gleam", 89).
-spec weekday_to_short_string(integer()) -> binary().
weekday_to_short_string(Weekday) ->
case Weekday of
1 ->
<<"Mon"/utf8>>;
2 ->
<<"Tue"/utf8>>;
3 ->
<<"Wed"/utf8>>;
4 ->
<<"Thu"/utf8>>;
5 ->
<<"Fri"/utf8>>;
6 ->
<<"Sat"/utf8>>;
7 ->
<<"Sun"/utf8>>;
_ ->
erlang:error(#{gleam_error => panic,
message => <<"erlang weekday outside of 1-7 range"/utf8>>,
module => <<"mist/internal/clock"/utf8>>,
function => <<"weekday_to_short_string"/utf8>>,
line => 98})
end.
-file("/Users/amanning/code/mist/src/mist/internal/clock.gleam", 102).
-spec month_to_short_string(integer()) -> binary().
month_to_short_string(Month) ->
case Month of
1 ->
<<"Jan"/utf8>>;
2 ->
<<"Feb"/utf8>>;
3 ->
<<"Mar"/utf8>>;
4 ->
<<"Apr"/utf8>>;
5 ->
<<"May"/utf8>>;
6 ->
<<"Jun"/utf8>>;
7 ->
<<"Jul"/utf8>>;
8 ->
<<"Aug"/utf8>>;
9 ->
<<"Sep"/utf8>>;
10 ->
<<"Oct"/utf8>>;
11 ->
<<"Nov"/utf8>>;
12 ->
<<"Dec"/utf8>>;
_ ->
erlang:error(#{gleam_error => panic,
message => <<"erlang month outside of 1-12 range"/utf8>>,
module => <<"mist/internal/clock"/utf8>>,
function => <<"month_to_short_string"/utf8>>,
line => 116})
end.
-file("/Users/amanning/code/mist/src/mist/internal/clock.gleam", 73).
-spec date() -> binary().
date() ->
{Weekday, {Year, Month, Day}, {Hour, Minute, Second}} = mist_ffi:now(),
Weekday@1 = weekday_to_short_string(Weekday),
Year@1 = begin
_pipe = erlang:integer_to_binary(Year),
gleam@string:pad_start(_pipe, 4, <<"0"/utf8>>)
end,
Month@1 = month_to_short_string(Month),
Day@1 = begin
_pipe@1 = erlang:integer_to_binary(Day),
gleam@string:pad_start(_pipe@1, 2, <<"0"/utf8>>)
end,
Hour@1 = begin
_pipe@2 = erlang:integer_to_binary(Hour),
gleam@string:pad_start(_pipe@2, 2, <<"0"/utf8>>)
end,
Minute@1 = begin
_pipe@3 = erlang:integer_to_binary(Minute),
gleam@string:pad_start(_pipe@3, 2, <<"0"/utf8>>)
end,
Second@1 = begin
_pipe@4 = erlang:integer_to_binary(Second),
gleam@string:pad_start(_pipe@4, 2, <<"0"/utf8>>)
end,
<<<<((<<Weekday@1/binary, ", "/utf8>>))/binary,
((<<<<<<<<<<Day@1/binary, " "/utf8>>/binary, Month@1/binary>>/binary,
" "/utf8>>/binary,
Year@1/binary>>/binary,
" "/utf8>>))/binary>>/binary,
((<<<<<<<<<<Hour@1/binary, ":"/utf8>>/binary, Minute@1/binary>>/binary,
":"/utf8>>/binary,
Second@1/binary>>/binary,
" GMT"/utf8>>))/binary>>.
-file("/Users/amanning/code/mist/src/mist/internal/clock.gleam", 29).
-spec start(any(), any()) -> {ok, gleam@erlang@process:pid_()} |
{error, gleam@otp@actor:start_error()}.
start(_, _) ->
_pipe@1 = gleam@otp@actor:start_spec(
{spec,
fun() ->
Subj = gleam@erlang@process:new_subject(),
Selector = begin
_pipe = gleam_erlang_ffi:new_selector(),
gleam@erlang@process:selecting(
_pipe,
Subj,
fun gleam@function:identity/1
)
end,
ets:new(
mist_clock,
[set, protected, named_table, {read_concurrency, true}]
),
gleam@erlang@process:send(Subj, set_time),
{ready, Subj, Selector}
end,
500,
fun(Msg, State) -> case Msg of
set_time ->
ets:insert(mist_clock, {date_header, date()}),
gleam@erlang@process:send_after(State, 1000, set_time),
gleam@otp@actor:continue(State)
end end}
),
gleam@result:map(_pipe@1, fun gleam@erlang@process:subject_owner/1).
-file("/Users/amanning/code/mist/src/mist/internal/clock.gleam", 60).
-spec get_date() -> binary().
get_date() ->
case mist_ffi:ets_lookup_element(mist_clock, date_header, 2) of
{ok, Value} ->
Value;
_ ->
logging:log(
warning,
<<"Failed to lookup date, re-calculating"/utf8>>
),
date()
end.