cowboy_clock.erl 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. %% Copyright (c) 2011-2013, Loïc Hoguin <essen@ninenines.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. %% @doc Date and time related functions.
  15. %%
  16. %% While a gen_server process runs in the background to update
  17. %% the cache of formatted dates every second, all API calls are
  18. %% local and directly read from the ETS cache table, providing
  19. %% fast time and date computations.
  20. -module(cowboy_clock).
  21. -behaviour(gen_server).
  22. %% API.
  23. -export([start_link/0]).
  24. -export([stop/0]).
  25. -export([rfc1123/0]).
  26. -export([rfc1123/1]).
  27. %% gen_server.
  28. -export([init/1]).
  29. -export([handle_call/3]).
  30. -export([handle_cast/2]).
  31. -export([handle_info/2]).
  32. -export([terminate/2]).
  33. -export([code_change/3]).
  34. -record(state, {
  35. universaltime = undefined :: undefined | calendar:datetime(),
  36. rfc1123 = <<>> :: binary(),
  37. tref = undefined :: undefined | timer:tref()
  38. }).
  39. -define(SERVER, ?MODULE).
  40. -define(TABLE, ?MODULE).
  41. %% API.
  42. %% @private
  43. -spec start_link() -> {ok, pid()}.
  44. start_link() ->
  45. gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).
  46. %% @private
  47. -spec stop() -> stopped.
  48. stop() ->
  49. gen_server:call(?SERVER, stop).
  50. %% @doc Return the current date and time formatted according to RFC-1123.
  51. -spec rfc1123() -> binary().
  52. rfc1123() ->
  53. ets:lookup_element(?TABLE, rfc1123, 2).
  54. %% @doc Return the given date and time formatted according to RFC-1123.
  55. -spec rfc1123(calendar:datetime()) -> binary().
  56. rfc1123(DateTime) ->
  57. update_rfc1123(<<>>, undefined, DateTime).
  58. %% gen_server.
  59. %% @private
  60. init([]) ->
  61. ?TABLE = ets:new(?TABLE, [set, protected,
  62. named_table, {read_concurrency, true}]),
  63. T = erlang:universaltime(),
  64. B = update_rfc1123(<<>>, undefined, T),
  65. {ok, TRef} = timer:send_interval(1000, update),
  66. ets:insert(?TABLE, {rfc1123, B}),
  67. {ok, #state{universaltime=T, rfc1123=B, tref=TRef}}.
  68. %% @private
  69. handle_call(stop, _From, State=#state{tref=TRef}) ->
  70. {ok, cancel} = timer:cancel(TRef),
  71. {stop, normal, stopped, State};
  72. handle_call(_Request, _From, State) ->
  73. {reply, ignored, State}.
  74. %% @private
  75. handle_cast(_Msg, State) ->
  76. {noreply, State}.
  77. %% @private
  78. handle_info(update, #state{universaltime=Prev, rfc1123=B1, tref=TRef}) ->
  79. T = erlang:universaltime(),
  80. B2 = update_rfc1123(B1, Prev, T),
  81. ets:insert(?TABLE, {rfc1123, B2}),
  82. {noreply, #state{universaltime=T, rfc1123=B2, tref=TRef}};
  83. handle_info(_Info, State) ->
  84. {noreply, State}.
  85. %% @private
  86. terminate(_Reason, _State) ->
  87. ok.
  88. %% @private
  89. code_change(_OldVsn, State, _Extra) ->
  90. {ok, State}.
  91. %% Internal.
  92. -spec update_rfc1123(binary(), undefined | calendar:datetime(),
  93. calendar:datetime()) -> binary().
  94. update_rfc1123(Bin, Now, Now) ->
  95. Bin;
  96. update_rfc1123(<< Keep:23/binary, _/bits >>,
  97. {Date, {H, M, _}}, {Date, {H, M, S}}) ->
  98. << Keep/binary, (pad_int(S))/binary, " GMT" >>;
  99. update_rfc1123(<< Keep:20/binary, _/bits >>,
  100. {Date, {H, _, _}}, {Date, {H, M, S}}) ->
  101. << Keep/binary, (pad_int(M))/binary, $:, (pad_int(S))/binary, " GMT" >>;
  102. update_rfc1123(<< Keep:17/binary, _/bits >>, {Date, _}, {Date, {H, M, S}}) ->
  103. << Keep/binary, (pad_int(H))/binary, $:, (pad_int(M))/binary,
  104. $:, (pad_int(S))/binary, " GMT" >>;
  105. update_rfc1123(<< _:7/binary, Keep:10/binary, _/bits >>,
  106. {{Y, Mo, _}, _}, {Date = {Y, Mo, D}, {H, M, S}}) ->
  107. Wday = calendar:day_of_the_week(Date),
  108. << (weekday(Wday))/binary, ", ", (pad_int(D))/binary, Keep/binary,
  109. (pad_int(H))/binary, $:, (pad_int(M))/binary,
  110. $:, (pad_int(S))/binary, " GMT" >>;
  111. update_rfc1123(<< _:11/binary, Keep:6/binary, _/bits >>,
  112. {{Y, _, _}, _}, {Date = {Y, Mo, D}, {H, M, S}}) ->
  113. Wday = calendar:day_of_the_week(Date),
  114. << (weekday(Wday))/binary, ", ", (pad_int(D))/binary, " ",
  115. (month(Mo))/binary, Keep/binary,
  116. (pad_int(H))/binary, $:, (pad_int(M))/binary,
  117. $:, (pad_int(S))/binary, " GMT" >>;
  118. update_rfc1123(_, _, {Date = {Y, Mo, D}, {H, M, S}}) ->
  119. Wday = calendar:day_of_the_week(Date),
  120. << (weekday(Wday))/binary, ", ", (pad_int(D))/binary, " ",
  121. (month(Mo))/binary, " ", (list_to_binary(integer_to_list(Y)))/binary,
  122. " ", (pad_int(H))/binary, $:, (pad_int(M))/binary,
  123. $:, (pad_int(S))/binary, " GMT" >>.
  124. %% Following suggestion by MononcQc on #erlounge.
  125. -spec pad_int(0..59) -> binary().
  126. pad_int(X) when X < 10 ->
  127. << $0, ($0 + X) >>;
  128. pad_int(X) ->
  129. list_to_binary(integer_to_list(X)).
  130. -spec weekday(1..7) -> <<_:24>>.
  131. weekday(1) -> <<"Mon">>;
  132. weekday(2) -> <<"Tue">>;
  133. weekday(3) -> <<"Wed">>;
  134. weekday(4) -> <<"Thu">>;
  135. weekday(5) -> <<"Fri">>;
  136. weekday(6) -> <<"Sat">>;
  137. weekday(7) -> <<"Sun">>.
  138. -spec month(1..12) -> <<_:24>>.
  139. month( 1) -> <<"Jan">>;
  140. month( 2) -> <<"Feb">>;
  141. month( 3) -> <<"Mar">>;
  142. month( 4) -> <<"Apr">>;
  143. month( 5) -> <<"May">>;
  144. month( 6) -> <<"Jun">>;
  145. month( 7) -> <<"Jul">>;
  146. month( 8) -> <<"Aug">>;
  147. month( 9) -> <<"Sep">>;
  148. month(10) -> <<"Oct">>;
  149. month(11) -> <<"Nov">>;
  150. month(12) -> <<"Dec">>.
  151. %% Tests.
  152. -ifdef(TEST).
  153. update_rfc1123_test_() ->
  154. Tests = [
  155. {<<"Sat, 14 May 2011 14:25:33 GMT">>, undefined,
  156. {{2011, 5, 14}, {14, 25, 33}}, <<>>},
  157. {<<"Sat, 14 May 2011 14:25:33 GMT">>, {{2011, 5, 14}, {14, 25, 33}},
  158. {{2011, 5, 14}, {14, 25, 33}}, <<"Sat, 14 May 2011 14:25:33 GMT">>},
  159. {<<"Sat, 14 May 2011 14:25:34 GMT">>, {{2011, 5, 14}, {14, 25, 33}},
  160. {{2011, 5, 14}, {14, 25, 34}}, <<"Sat, 14 May 2011 14:25:33 GMT">>},
  161. {<<"Sat, 14 May 2011 14:26:00 GMT">>, {{2011, 5, 14}, {14, 25, 59}},
  162. {{2011, 5, 14}, {14, 26, 0}}, <<"Sat, 14 May 2011 14:25:59 GMT">>},
  163. {<<"Sat, 14 May 2011 15:00:00 GMT">>, {{2011, 5, 14}, {14, 59, 59}},
  164. {{2011, 5, 14}, {15, 0, 0}}, <<"Sat, 14 May 2011 14:59:59 GMT">>},
  165. {<<"Sun, 15 May 2011 00:00:00 GMT">>, {{2011, 5, 14}, {23, 59, 59}},
  166. {{2011, 5, 15}, { 0, 0, 0}}, <<"Sat, 14 May 2011 23:59:59 GMT">>},
  167. {<<"Wed, 01 Jun 2011 00:00:00 GMT">>, {{2011, 5, 31}, {23, 59, 59}},
  168. {{2011, 6, 1}, { 0, 0, 0}}, <<"Tue, 31 May 2011 23:59:59 GMT">>},
  169. {<<"Sun, 01 Jan 2012 00:00:00 GMT">>, {{2011, 5, 31}, {23, 59, 59}},
  170. {{2012, 1, 1}, { 0, 0, 0}}, <<"Sat, 31 Dec 2011 23:59:59 GMT">>}
  171. ],
  172. [{R, fun() -> R = update_rfc1123(B, P, N) end} || {R, P, N, B} <- Tests].
  173. pad_int_test_() ->
  174. Tests = [
  175. { 0, <<"00">>}, { 1, <<"01">>}, { 2, <<"02">>}, { 3, <<"03">>},
  176. { 4, <<"04">>}, { 5, <<"05">>}, { 6, <<"06">>}, { 7, <<"07">>},
  177. { 8, <<"08">>}, { 9, <<"09">>}, {10, <<"10">>}, {11, <<"11">>},
  178. {12, <<"12">>}, {13, <<"13">>}, {14, <<"14">>}, {15, <<"15">>},
  179. {16, <<"16">>}, {17, <<"17">>}, {18, <<"18">>}, {19, <<"19">>},
  180. {20, <<"20">>}, {21, <<"21">>}, {22, <<"22">>}, {23, <<"23">>},
  181. {24, <<"24">>}, {25, <<"25">>}, {26, <<"26">>}, {27, <<"27">>},
  182. {28, <<"28">>}, {29, <<"29">>}, {30, <<"30">>}, {31, <<"31">>},
  183. {32, <<"32">>}, {33, <<"33">>}, {34, <<"34">>}, {35, <<"35">>},
  184. {36, <<"36">>}, {37, <<"37">>}, {38, <<"38">>}, {39, <<"39">>},
  185. {40, <<"40">>}, {41, <<"41">>}, {42, <<"42">>}, {43, <<"43">>},
  186. {44, <<"44">>}, {45, <<"45">>}, {46, <<"46">>}, {47, <<"47">>},
  187. {48, <<"48">>}, {49, <<"49">>}, {50, <<"50">>}, {51, <<"51">>},
  188. {52, <<"52">>}, {53, <<"53">>}, {54, <<"54">>}, {55, <<"55">>},
  189. {56, <<"56">>}, {57, <<"57">>}, {58, <<"58">>}, {59, <<"59">>}
  190. ],
  191. [{I, fun() -> O = pad_int(I) end} || {I, O} <- Tests].
  192. -endif.