cowboy_clock.erl 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. %% Copyright (c) 2011, Loïc Hoguin <essen@dev-extend.eu>
  2. %%
  3. %% Permission to use, copy, modify, and/or distribute this software for any
  4. %% purpose with or without fee is hereby granted, provided that the above
  5. %% copyright notice and this permission notice appear in all copies.
  6. %%
  7. %% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. %% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. %% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  10. %% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. %% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  12. %% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  13. %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. -module(cowboy_clock).
  15. -behaviour(gen_server).
  16. -export([start_link/0, stop/0, rfc1123/0]). %% API.
  17. -export([init/1, handle_call/3, handle_cast/2,
  18. handle_info/2, terminate/2, code_change/3]). %% gen_server.
  19. %% @todo Use calendar types whenever they get exported.
  20. -type year() :: non_neg_integer().
  21. -type month() :: 1..12.
  22. -type day() :: 1..31.
  23. -type hour() :: 0..23.
  24. -type minute() :: 0..59.
  25. -type second() :: 0..59.
  26. -type daynum() :: 1..7.
  27. -type date() :: {year(), month(), day()}.
  28. -type time() :: {hour(), minute(), second()}.
  29. -type datetime() :: {date(), time()}.
  30. -record(state, {
  31. universaltime = undefined :: undefined | datetime(),
  32. rfc1123 = <<>> :: binary(),
  33. tref = undefined :: undefined | timer:tref()
  34. }).
  35. -define(SERVER, ?MODULE).
  36. -define(TABLE, ?MODULE).
  37. -include_lib("eunit/include/eunit.hrl").
  38. %% API.
  39. -spec start_link() -> {ok, Pid::pid()}.
  40. start_link() ->
  41. gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).
  42. -spec stop() -> stopped.
  43. stop() ->
  44. gen_server:call(?SERVER, stop).
  45. -spec rfc1123() -> binary().
  46. rfc1123() ->
  47. ets:lookup_element(?TABLE, rfc1123, 2).
  48. %% gen_server.
  49. init([]) ->
  50. ?TABLE = ets:new(?TABLE, [set, protected,
  51. named_table, {read_concurrency, true}]),
  52. T = erlang:universaltime(),
  53. B = update_rfc1123(undefined, T, <<>>),
  54. {ok, TRef} = timer:send_interval(1000, update),
  55. ets:insert(?TABLE, {rfc1123, B}),
  56. {ok, #state{universaltime=T, rfc1123=B, tref=TRef}}.
  57. handle_call(stop, _From, State=#state{tref=TRef}) ->
  58. {ok, cancel} = timer:cancel(TRef),
  59. {stop, normal, stopped, State};
  60. handle_call(_Request, _From, State) ->
  61. {reply, ignored, State}.
  62. handle_cast(_Msg, State) ->
  63. {noreply, State}.
  64. handle_info(update, #state{universaltime=Prev, rfc1123=B1, tref=TRef}) ->
  65. T = erlang:universaltime(),
  66. B2 = update_rfc1123(Prev, T, B1),
  67. ets:insert(?TABLE, {rfc1123, B2}),
  68. {noreply, #state{universaltime=T, rfc1123=B2, tref=TRef}};
  69. handle_info(_Info, State) ->
  70. {noreply, State}.
  71. terminate(_Reason, _State) ->
  72. ok.
  73. code_change(_OldVsn, State, _Extra) ->
  74. {ok, State}.
  75. %% Internal.
  76. -spec update_rfc1123(Prev::undefined | datetime(), Now::datetime(),
  77. Bin::binary()) -> binary().
  78. update_rfc1123(Now, Now, Bin) ->
  79. Bin;
  80. update_rfc1123({Date, {H, M, _}}, {Date, {H, M, S}},
  81. << Keep:23/binary, _/bits >>) ->
  82. << Keep/binary, (pad_int(S))/binary, " GMT" >>;
  83. update_rfc1123({Date, {H, _, _}}, {Date, {H, M, S}},
  84. << Keep:20/binary, _/bits >>) ->
  85. << Keep/binary, (pad_int(M))/binary, $:, (pad_int(S))/binary, " GMT" >>;
  86. update_rfc1123({Date, _}, {Date, {H, M, S}}, << Keep:17/binary, _/bits >>) ->
  87. << Keep/binary, (pad_int(H))/binary, $:, (pad_int(M))/binary,
  88. $:, (pad_int(S))/binary, " GMT" >>;
  89. update_rfc1123({{Y, Mo, _}, _}, {Date = {Y, Mo, D}, {H, M, S}},
  90. << _:7/binary, Keep:10/binary, _/bits >>) ->
  91. Wday = calendar:day_of_the_week(Date),
  92. << (weekday(Wday))/binary, ", ", (pad_int(D))/binary, Keep/binary,
  93. (pad_int(H))/binary, $:, (pad_int(M))/binary,
  94. $:, (pad_int(S))/binary, " GMT" >>;
  95. update_rfc1123({{Y, _, _}, _}, {Date = {Y, Mo, D}, {H, M, S}},
  96. << _:11/binary, Keep:6/binary, _/bits >>) ->
  97. Wday = calendar:day_of_the_week(Date),
  98. << (weekday(Wday))/binary, ", ", (pad_int(D))/binary, " ",
  99. (month(Mo))/binary, Keep/binary,
  100. (pad_int(H))/binary, $:, (pad_int(M))/binary,
  101. $:, (pad_int(S))/binary, " GMT" >>;
  102. update_rfc1123(_, {Date = {Y, Mo, D}, {H, M, S}}, _) ->
  103. Wday = calendar:day_of_the_week(Date),
  104. << (weekday(Wday))/binary, ", ", (pad_int(D))/binary, " ",
  105. (month(Mo))/binary, " ", (list_to_binary(integer_to_list(Y)))/binary,
  106. " ", (pad_int(H))/binary, $:, (pad_int(M))/binary,
  107. $:, (pad_int(S))/binary, " GMT" >>.
  108. %% Following suggestion by MononcQc on #erlounge.
  109. -spec pad_int(0..59) -> binary().
  110. pad_int(X) when X < 10 ->
  111. << $0, ($0 + X) >>;
  112. pad_int(X) ->
  113. list_to_binary(integer_to_list(X)).
  114. -spec weekday(daynum()) -> binary().
  115. weekday(1) -> <<"Mon">>;
  116. weekday(2) -> <<"Tue">>;
  117. weekday(3) -> <<"Wed">>;
  118. weekday(4) -> <<"Thu">>;
  119. weekday(5) -> <<"Fri">>;
  120. weekday(6) -> <<"Sat">>;
  121. weekday(7) -> <<"Sun">>.
  122. -spec month(month()) -> binary().
  123. month( 1) -> <<"Jan">>;
  124. month( 2) -> <<"Feb">>;
  125. month( 3) -> <<"Mar">>;
  126. month( 4) -> <<"Apr">>;
  127. month( 5) -> <<"May">>;
  128. month( 6) -> <<"Jun">>;
  129. month( 7) -> <<"Jul">>;
  130. month( 8) -> <<"Aug">>;
  131. month( 9) -> <<"Sep">>;
  132. month(10) -> <<"Oct">>;
  133. month(11) -> <<"Nov">>;
  134. month(12) -> <<"Dec">>.
  135. %% Tests.
  136. -ifdef(TEST).
  137. update_rfc1123_test_() ->
  138. Tests = [
  139. {<<"Sat, 14 May 2011 14:25:33 GMT">>, undefined,
  140. {{2011, 5, 14}, {14, 25, 33}}, <<>>},
  141. {<<"Sat, 14 May 2011 14:25:33 GMT">>, {{2011, 5, 14}, {14, 25, 33}},
  142. {{2011, 5, 14}, {14, 25, 33}}, <<"Sat, 14 May 2011 14:25:33 GMT">>},
  143. {<<"Sat, 14 May 2011 14:25:34 GMT">>, {{2011, 5, 14}, {14, 25, 33}},
  144. {{2011, 5, 14}, {14, 25, 34}}, <<"Sat, 14 May 2011 14:25:33 GMT">>},
  145. {<<"Sat, 14 May 2011 14:26:00 GMT">>, {{2011, 5, 14}, {14, 25, 59}},
  146. {{2011, 5, 14}, {14, 26, 0}}, <<"Sat, 14 May 2011 14:25:59 GMT">>},
  147. {<<"Sat, 14 May 2011 15:00:00 GMT">>, {{2011, 5, 14}, {14, 59, 59}},
  148. {{2011, 5, 14}, {15, 0, 0}}, <<"Sat, 14 May 2011 14:59:59 GMT">>},
  149. {<<"Sun, 15 May 2011 00:00:00 GMT">>, {{2011, 5, 14}, {23, 59, 59}},
  150. {{2011, 5, 15}, { 0, 0, 0}}, <<"Sat, 14 May 2011 23:59:59 GMT">>},
  151. {<<"Wed, 01 Jun 2011 00:00:00 GMT">>, {{2011, 5, 31}, {23, 59, 59}},
  152. {{2011, 6, 1}, { 0, 0, 0}}, <<"Tue, 31 May 2011 23:59:59 GMT">>},
  153. {<<"Sun, 01 Jan 2012 00:00:00 GMT">>, {{2011, 5, 31}, {23, 59, 59}},
  154. {{2012, 1, 1}, { 0, 0, 0}}, <<"Sat, 31 Dec 2011 23:59:59 GMT">>}
  155. ],
  156. [{R, fun() -> R = update_rfc1123(P, N, B) end} || {R, P, N, B} <- Tests].
  157. pad_int_test_() ->
  158. Tests = [
  159. { 0, <<"00">>}, { 1, <<"01">>}, { 2, <<"02">>}, { 3, <<"03">>},
  160. { 4, <<"04">>}, { 5, <<"05">>}, { 6, <<"06">>}, { 7, <<"07">>},
  161. { 8, <<"08">>}, { 9, <<"09">>}, {10, <<"10">>}, {11, <<"11">>},
  162. {12, <<"12">>}, {13, <<"13">>}, {14, <<"14">>}, {15, <<"15">>},
  163. {16, <<"16">>}, {17, <<"17">>}, {18, <<"18">>}, {19, <<"19">>},
  164. {20, <<"20">>}, {21, <<"21">>}, {22, <<"22">>}, {23, <<"23">>},
  165. {24, <<"24">>}, {25, <<"25">>}, {26, <<"26">>}, {27, <<"27">>},
  166. {28, <<"28">>}, {29, <<"29">>}, {30, <<"30">>}, {31, <<"31">>},
  167. {32, <<"32">>}, {33, <<"33">>}, {34, <<"34">>}, {35, <<"35">>},
  168. {36, <<"36">>}, {37, <<"37">>}, {38, <<"38">>}, {39, <<"39">>},
  169. {40, <<"40">>}, {41, <<"41">>}, {42, <<"42">>}, {43, <<"43">>},
  170. {44, <<"44">>}, {45, <<"45">>}, {46, <<"46">>}, {47, <<"47">>},
  171. {48, <<"48">>}, {49, <<"49">>}, {50, <<"50">>}, {51, <<"51">>},
  172. {52, <<"52">>}, {53, <<"53">>}, {54, <<"54">>}, {55, <<"55">>},
  173. {56, <<"56">>}, {57, <<"57">>}, {58, <<"58">>}, {59, <<"59">>}
  174. ],
  175. [{I, fun() -> O = pad_int(I) end} || {I, O} <- Tests].
  176. -endif.