cow_date.erl 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. %% Copyright (c) 2013-2014, 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. -module(cow_date).
  15. -export([parse_date/1]).
  16. -export([rfc2109/1]).
  17. -ifdef(TEST).
  18. -include_lib("triq/include/triq.hrl").
  19. -endif.
  20. %% @doc Parse the HTTP date (IMF-fixdate, rfc850, asctime).
  21. -define(DIGITS(A, B), ((A - $0) * 10 + (B - $0))).
  22. -define(DIGITS(A, B, C, D), ((A - $0) * 1000 + (B - $0) * 100 + (C - $0) * 10 + (D - $0))).
  23. parse_date(DateBin) ->
  24. Date = {{_, _, D}, {H, M, S}} = http_date(DateBin),
  25. true = D >= 0 andalso D =< 31,
  26. true = H >= 0 andalso H =< 23,
  27. true = M >= 0 andalso M =< 59,
  28. true = S >= 0 andalso S =< 60, %% Leap second.
  29. Date.
  30. http_date(<<"Mon, ", D1, D2, " ", R/bits >>) -> fixdate(R, ?DIGITS(D1, D2));
  31. http_date(<<"Tue, ", D1, D2, " ", R/bits >>) -> fixdate(R, ?DIGITS(D1, D2));
  32. http_date(<<"Wed, ", D1, D2, " ", R/bits >>) -> fixdate(R, ?DIGITS(D1, D2));
  33. http_date(<<"Thu, ", D1, D2, " ", R/bits >>) -> fixdate(R, ?DIGITS(D1, D2));
  34. http_date(<<"Fri, ", D1, D2, " ", R/bits >>) -> fixdate(R, ?DIGITS(D1, D2));
  35. http_date(<<"Sat, ", D1, D2, " ", R/bits >>) -> fixdate(R, ?DIGITS(D1, D2));
  36. http_date(<<"Sun, ", D1, D2, " ", R/bits >>) -> fixdate(R, ?DIGITS(D1, D2));
  37. http_date(<<"Monday, ", D1, D2, "-", R/bits >>) -> rfc850_date(R, ?DIGITS(D1, D2));
  38. http_date(<<"Tuesday, ", D1, D2, "-", R/bits >>) -> rfc850_date(R, ?DIGITS(D1, D2));
  39. http_date(<<"Wednesday, ", D1, D2, "-", R/bits >>) -> rfc850_date(R, ?DIGITS(D1, D2));
  40. http_date(<<"Thursday, ", D1, D2, "-", R/bits >>) -> rfc850_date(R, ?DIGITS(D1, D2));
  41. http_date(<<"Friday, ", D1, D2, "-", R/bits >>) -> rfc850_date(R, ?DIGITS(D1, D2));
  42. http_date(<<"Saturday, ", D1, D2, "-", R/bits >>) -> rfc850_date(R, ?DIGITS(D1, D2));
  43. http_date(<<"Sunday, ", D1, D2, "-", R/bits >>) -> rfc850_date(R, ?DIGITS(D1, D2));
  44. http_date(<<"Mon ", R/bits >>) -> asctime_date(R);
  45. http_date(<<"Tue ", R/bits >>) -> asctime_date(R);
  46. http_date(<<"Wed ", R/bits >>) -> asctime_date(R);
  47. http_date(<<"Thu ", R/bits >>) -> asctime_date(R);
  48. http_date(<<"Fri ", R/bits >>) -> asctime_date(R);
  49. http_date(<<"Sat ", R/bits >>) -> asctime_date(R);
  50. http_date(<<"Sun ", R/bits >>) -> asctime_date(R).
  51. fixdate(<<"Jan ", Y1, Y2, Y3, Y4, " ", H1, H2, ":", M1, M2, ":", S1, S2, " GMT">>, Day) ->
  52. {{?DIGITS(Y1, Y2, Y3, Y4), 1, Day}, {?DIGITS(H1, H2), ?DIGITS(M1, M2), ?DIGITS(S1, S2)}};
  53. fixdate(<<"Feb ", Y1, Y2, Y3, Y4, " ", H1, H2, ":", M1, M2, ":", S1, S2, " GMT">>, Day) ->
  54. {{?DIGITS(Y1, Y2, Y3, Y4), 2, Day}, {?DIGITS(H1, H2), ?DIGITS(M1, M2), ?DIGITS(S1, S2)}};
  55. fixdate(<<"Mar ", Y1, Y2, Y3, Y4, " ", H1, H2, ":", M1, M2, ":", S1, S2, " GMT">>, Day) ->
  56. {{?DIGITS(Y1, Y2, Y3, Y4), 3, Day}, {?DIGITS(H1, H2), ?DIGITS(M1, M2), ?DIGITS(S1, S2)}};
  57. fixdate(<<"Apr ", Y1, Y2, Y3, Y4, " ", H1, H2, ":", M1, M2, ":", S1, S2, " GMT">>, Day) ->
  58. {{?DIGITS(Y1, Y2, Y3, Y4), 4, Day}, {?DIGITS(H1, H2), ?DIGITS(M1, M2), ?DIGITS(S1, S2)}};
  59. fixdate(<<"May ", Y1, Y2, Y3, Y4, " ", H1, H2, ":", M1, M2, ":", S1, S2, " GMT">>, Day) ->
  60. {{?DIGITS(Y1, Y2, Y3, Y4), 5, Day}, {?DIGITS(H1, H2), ?DIGITS(M1, M2), ?DIGITS(S1, S2)}};
  61. fixdate(<<"Jun ", Y1, Y2, Y3, Y4, " ", H1, H2, ":", M1, M2, ":", S1, S2, " GMT">>, Day) ->
  62. {{?DIGITS(Y1, Y2, Y3, Y4), 6, Day}, {?DIGITS(H1, H2), ?DIGITS(M1, M2), ?DIGITS(S1, S2)}};
  63. fixdate(<<"Jul ", Y1, Y2, Y3, Y4, " ", H1, H2, ":", M1, M2, ":", S1, S2, " GMT">>, Day) ->
  64. {{?DIGITS(Y1, Y2, Y3, Y4), 7, Day}, {?DIGITS(H1, H2), ?DIGITS(M1, M2), ?DIGITS(S1, S2)}};
  65. fixdate(<<"Aug ", Y1, Y2, Y3, Y4, " ", H1, H2, ":", M1, M2, ":", S1, S2, " GMT">>, Day) ->
  66. {{?DIGITS(Y1, Y2, Y3, Y4), 8, Day}, {?DIGITS(H1, H2), ?DIGITS(M1, M2), ?DIGITS(S1, S2)}};
  67. fixdate(<<"Sep ", Y1, Y2, Y3, Y4, " ", H1, H2, ":", M1, M2, ":", S1, S2, " GMT">>, Day) ->
  68. {{?DIGITS(Y1, Y2, Y3, Y4), 9, Day}, {?DIGITS(H1, H2), ?DIGITS(M1, M2), ?DIGITS(S1, S2)}};
  69. fixdate(<<"Oct ", Y1, Y2, Y3, Y4, " ", H1, H2, ":", M1, M2, ":", S1, S2, " GMT">>, Day) ->
  70. {{?DIGITS(Y1, Y2, Y3, Y4), 10, Day}, {?DIGITS(H1, H2), ?DIGITS(M1, M2), ?DIGITS(S1, S2)}};
  71. fixdate(<<"Nov ", Y1, Y2, Y3, Y4, " ", H1, H2, ":", M1, M2, ":", S1, S2, " GMT">>, Day) ->
  72. {{?DIGITS(Y1, Y2, Y3, Y4), 11, Day}, {?DIGITS(H1, H2), ?DIGITS(M1, M2), ?DIGITS(S1, S2)}};
  73. fixdate(<<"Dec ", Y1, Y2, Y3, Y4, " ", H1, H2, ":", M1, M2, ":", S1, S2, " GMT">>, Day) ->
  74. {{?DIGITS(Y1, Y2, Y3, Y4), 12, Day}, {?DIGITS(H1, H2), ?DIGITS(M1, M2), ?DIGITS(S1, S2)}}.
  75. rfc850_date(<<"Jan-", Y1, Y2, " ", H1, H2, ":", M1, M2, ":", S1, S2, " GMT">>, Day) ->
  76. {{rfc850_year(?DIGITS(Y1, Y2)), 1, Day}, {?DIGITS(H1, H2), ?DIGITS(M1, M2), ?DIGITS(S1, S2)}};
  77. rfc850_date(<<"Feb-", Y1, Y2, " ", H1, H2, ":", M1, M2, ":", S1, S2, " GMT">>, Day) ->
  78. {{rfc850_year(?DIGITS(Y1, Y2)), 2, Day}, {?DIGITS(H1, H2), ?DIGITS(M1, M2), ?DIGITS(S1, S2)}};
  79. rfc850_date(<<"Mar-", Y1, Y2, " ", H1, H2, ":", M1, M2, ":", S1, S2, " GMT">>, Day) ->
  80. {{rfc850_year(?DIGITS(Y1, Y2)), 3, Day}, {?DIGITS(H1, H2), ?DIGITS(M1, M2), ?DIGITS(S1, S2)}};
  81. rfc850_date(<<"Apr-", Y1, Y2, " ", H1, H2, ":", M1, M2, ":", S1, S2, " GMT">>, Day) ->
  82. {{rfc850_year(?DIGITS(Y1, Y2)), 4, Day}, {?DIGITS(H1, H2), ?DIGITS(M1, M2), ?DIGITS(S1, S2)}};
  83. rfc850_date(<<"May-", Y1, Y2, " ", H1, H2, ":", M1, M2, ":", S1, S2, " GMT">>, Day) ->
  84. {{rfc850_year(?DIGITS(Y1, Y2)), 5, Day}, {?DIGITS(H1, H2), ?DIGITS(M1, M2), ?DIGITS(S1, S2)}};
  85. rfc850_date(<<"Jun-", Y1, Y2, " ", H1, H2, ":", M1, M2, ":", S1, S2, " GMT">>, Day) ->
  86. {{rfc850_year(?DIGITS(Y1, Y2)), 6, Day}, {?DIGITS(H1, H2), ?DIGITS(M1, M2), ?DIGITS(S1, S2)}};
  87. rfc850_date(<<"Jul-", Y1, Y2, " ", H1, H2, ":", M1, M2, ":", S1, S2, " GMT">>, Day) ->
  88. {{rfc850_year(?DIGITS(Y1, Y2)), 7, Day}, {?DIGITS(H1, H2), ?DIGITS(M1, M2), ?DIGITS(S1, S2)}};
  89. rfc850_date(<<"Aug-", Y1, Y2, " ", H1, H2, ":", M1, M2, ":", S1, S2, " GMT">>, Day) ->
  90. {{rfc850_year(?DIGITS(Y1, Y2)), 8, Day}, {?DIGITS(H1, H2), ?DIGITS(M1, M2), ?DIGITS(S1, S2)}};
  91. rfc850_date(<<"Sep-", Y1, Y2, " ", H1, H2, ":", M1, M2, ":", S1, S2, " GMT">>, Day) ->
  92. {{rfc850_year(?DIGITS(Y1, Y2)), 9, Day}, {?DIGITS(H1, H2), ?DIGITS(M1, M2), ?DIGITS(S1, S2)}};
  93. rfc850_date(<<"Oct-", Y1, Y2, " ", H1, H2, ":", M1, M2, ":", S1, S2, " GMT">>, Day) ->
  94. {{rfc850_year(?DIGITS(Y1, Y2)), 10, Day}, {?DIGITS(H1, H2), ?DIGITS(M1, M2), ?DIGITS(S1, S2)}};
  95. rfc850_date(<<"Nov-", Y1, Y2, " ", H1, H2, ":", M1, M2, ":", S1, S2, " GMT">>, Day) ->
  96. {{rfc850_year(?DIGITS(Y1, Y2)), 11, Day}, {?DIGITS(H1, H2), ?DIGITS(M1, M2), ?DIGITS(S1, S2)}};
  97. rfc850_date(<<"Dec-", Y1, Y2, " ", H1, H2, ":", M1, M2, ":", S1, S2, " GMT">>, Day) ->
  98. {{rfc850_year(?DIGITS(Y1, Y2)), 12, Day}, {?DIGITS(H1, H2), ?DIGITS(M1, M2), ?DIGITS(S1, S2)}}.
  99. rfc850_year(Y) when Y > 50 -> Y + 1900;
  100. rfc850_year(Y) -> Y + 2000.
  101. asctime_date(<<"Jan ", D1, D2, " ", H1, H2, ":", M1, M2, ":", S1, S2, " ", Y1, Y2, Y3, Y4 >>) ->
  102. {{?DIGITS(Y1, Y2, Y3, Y4), 1, asctime_day(D1, D2)}, {?DIGITS(H1, H2), ?DIGITS(M1, M2), ?DIGITS(S1, S2)}};
  103. asctime_date(<<"Feb ", D1, D2, " ", H1, H2, ":", M1, M2, ":", S1, S2, " ", Y1, Y2, Y3, Y4 >>) ->
  104. {{?DIGITS(Y1, Y2, Y3, Y4), 2, asctime_day(D1, D2)}, {?DIGITS(H1, H2), ?DIGITS(M1, M2), ?DIGITS(S1, S2)}};
  105. asctime_date(<<"Mar ", D1, D2, " ", H1, H2, ":", M1, M2, ":", S1, S2, " ", Y1, Y2, Y3, Y4 >>) ->
  106. {{?DIGITS(Y1, Y2, Y3, Y4), 3, asctime_day(D1, D2)}, {?DIGITS(H1, H2), ?DIGITS(M1, M2), ?DIGITS(S1, S2)}};
  107. asctime_date(<<"Apr ", D1, D2, " ", H1, H2, ":", M1, M2, ":", S1, S2, " ", Y1, Y2, Y3, Y4 >>) ->
  108. {{?DIGITS(Y1, Y2, Y3, Y4), 4, asctime_day(D1, D2)}, {?DIGITS(H1, H2), ?DIGITS(M1, M2), ?DIGITS(S1, S2)}};
  109. asctime_date(<<"May ", D1, D2, " ", H1, H2, ":", M1, M2, ":", S1, S2, " ", Y1, Y2, Y3, Y4 >>) ->
  110. {{?DIGITS(Y1, Y2, Y3, Y4), 5, asctime_day(D1, D2)}, {?DIGITS(H1, H2), ?DIGITS(M1, M2), ?DIGITS(S1, S2)}};
  111. asctime_date(<<"Jun ", D1, D2, " ", H1, H2, ":", M1, M2, ":", S1, S2, " ", Y1, Y2, Y3, Y4 >>) ->
  112. {{?DIGITS(Y1, Y2, Y3, Y4), 6, asctime_day(D1, D2)}, {?DIGITS(H1, H2), ?DIGITS(M1, M2), ?DIGITS(S1, S2)}};
  113. asctime_date(<<"Jul ", D1, D2, " ", H1, H2, ":", M1, M2, ":", S1, S2, " ", Y1, Y2, Y3, Y4 >>) ->
  114. {{?DIGITS(Y1, Y2, Y3, Y4), 7, asctime_day(D1, D2)}, {?DIGITS(H1, H2), ?DIGITS(M1, M2), ?DIGITS(S1, S2)}};
  115. asctime_date(<<"Aug ", D1, D2, " ", H1, H2, ":", M1, M2, ":", S1, S2, " ", Y1, Y2, Y3, Y4 >>) ->
  116. {{?DIGITS(Y1, Y2, Y3, Y4), 8, asctime_day(D1, D2)}, {?DIGITS(H1, H2), ?DIGITS(M1, M2), ?DIGITS(S1, S2)}};
  117. asctime_date(<<"Sep ", D1, D2, " ", H1, H2, ":", M1, M2, ":", S1, S2, " ", Y1, Y2, Y3, Y4 >>) ->
  118. {{?DIGITS(Y1, Y2, Y3, Y4), 9, asctime_day(D1, D2)}, {?DIGITS(H1, H2), ?DIGITS(M1, M2), ?DIGITS(S1, S2)}};
  119. asctime_date(<<"Oct ", D1, D2, " ", H1, H2, ":", M1, M2, ":", S1, S2, " ", Y1, Y2, Y3, Y4 >>) ->
  120. {{?DIGITS(Y1, Y2, Y3, Y4), 10, asctime_day(D1, D2)}, {?DIGITS(H1, H2), ?DIGITS(M1, M2), ?DIGITS(S1, S2)}};
  121. asctime_date(<<"Nov ", D1, D2, " ", H1, H2, ":", M1, M2, ":", S1, S2, " ", Y1, Y2, Y3, Y4 >>) ->
  122. {{?DIGITS(Y1, Y2, Y3, Y4), 11, asctime_day(D1, D2)}, {?DIGITS(H1, H2), ?DIGITS(M1, M2), ?DIGITS(S1, S2)}};
  123. asctime_date(<<"Dec ", D1, D2, " ", H1, H2, ":", M1, M2, ":", S1, S2, " ", Y1, Y2, Y3, Y4 >>) ->
  124. {{?DIGITS(Y1, Y2, Y3, Y4), 12, asctime_day(D1, D2)}, {?DIGITS(H1, H2), ?DIGITS(M1, M2), ?DIGITS(S1, S2)}}.
  125. asctime_day($\s, D2) -> (D2 - $0);
  126. asctime_day(D1, D2) -> (D1 - $0) * 10 + (D2 - $0).
  127. -ifdef(TEST).
  128. day_name() -> oneof(["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]).
  129. day_name_l() -> oneof(["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]).
  130. year() -> int(1951, 2050).
  131. month() -> int(1, 12).
  132. day() -> int(1, 31).
  133. hour() -> int(23).
  134. minute() -> int(59).
  135. second() -> int(60).
  136. fixdate_gen() ->
  137. ?LET({DayName, Y, Mo, D, H, Mi, S},
  138. {day_name(), year(), month(), day(), hour(), minute(), second()},
  139. {{{Y, Mo, D}, {H, Mi, S}},
  140. list_to_binary([DayName, ", ", pad_int(D), " ", month(Mo), " ", integer_to_binary(Y),
  141. " ", pad_int(H), ":", pad_int(Mi), ":", pad_int(S), " GMT"])}).
  142. rfc850_gen() ->
  143. ?LET({DayName, Y, Mo, D, H, Mi, S},
  144. {day_name_l(), year(), month(), day(), hour(), minute(), second()},
  145. {{{Y, Mo, D}, {H, Mi, S}},
  146. list_to_binary([DayName, ", ", pad_int(D), "-", month(Mo), "-", pad_int(Y rem 100),
  147. " ", pad_int(H), ":", pad_int(Mi), ":", pad_int(S), " GMT"])}).
  148. asctime_gen() ->
  149. ?LET({DayName, Y, Mo, D, H, Mi, S},
  150. {day_name(), year(), month(), day(), hour(), minute(), second()},
  151. {{{Y, Mo, D}, {H, Mi, S}},
  152. list_to_binary([DayName, " ", month(Mo), " ",
  153. if D < 10 -> << $\s, (D + $0) >>; true -> integer_to_binary(D) end,
  154. " ", pad_int(H), ":", pad_int(Mi), ":", pad_int(S), " ", integer_to_binary(Y)])}).
  155. prop_http_date() ->
  156. ?FORALL({Date, DateBin},
  157. oneof([fixdate_gen(), rfc850_gen(), asctime_gen()]),
  158. Date =:= parse_date(DateBin)).
  159. http_date_test_() ->
  160. Tests = [
  161. {<<"Sun, 06 Nov 1994 08:49:37 GMT">>, {{1994, 11, 6}, {8, 49, 37}}},
  162. {<<"Sunday, 06-Nov-94 08:49:37 GMT">>, {{1994, 11, 6}, {8, 49, 37}}},
  163. {<<"Sun Nov 6 08:49:37 1994">>, {{1994, 11, 6}, {8, 49, 37}}}
  164. ],
  165. [{V, fun() -> R = http_date(V) end} || {V, R} <- Tests].
  166. -endif.
  167. -ifdef(PERF).
  168. horse_http_date_fixdate() ->
  169. horse:repeat(200000,
  170. http_date(<<"Sun, 06 Nov 1994 08:49:37 GMT">>)
  171. ).
  172. horse_http_date_rfc850() ->
  173. horse:repeat(200000,
  174. http_date(<<"Sunday, 06-Nov-94 08:49:37 GMT">>)
  175. ).
  176. horse_http_date_asctime() ->
  177. horse:repeat(200000,
  178. http_date(<<"Sun Nov 6 08:49:37 1994">>)
  179. ).
  180. -endif.
  181. %% @doc Return the date formatted according to RFC2109.
  182. -spec rfc2109(calendar:datetime()) -> binary().
  183. rfc2109({Date = {Y, Mo, D}, {H, Mi, S}}) ->
  184. Wday = calendar:day_of_the_week(Date),
  185. << (weekday(Wday))/binary, ", ",
  186. (pad_int(D))/binary, "-",
  187. (month(Mo))/binary, "-",
  188. (year(Y))/binary, " ",
  189. (pad_int(H))/binary, ":",
  190. (pad_int(Mi))/binary, ":",
  191. (pad_int(S))/binary, " GMT" >>.
  192. -ifdef(TEST).
  193. rfc2109_test_() ->
  194. Tests = [
  195. {<<"Sat, 14-May-2011 14:25:33 GMT">>, {{2011, 5, 14}, {14, 25, 33}}},
  196. {<<"Sun, 01-Jan-2012 00:00:00 GMT">>, {{2012, 1, 1}, { 0, 0, 0}}}
  197. ],
  198. [{R, fun() -> R = rfc2109(D) end} || {R, D} <- Tests].
  199. -endif.
  200. -ifdef(PERF).
  201. horse_rfc2019_20130101_000000() ->
  202. horse:repeat(100000,
  203. rfc2109({{2013, 1, 1}, {0, 0, 0}})
  204. ).
  205. horse_rfc2019_20131231_235959() ->
  206. horse:repeat(100000,
  207. rfc2109({{2013, 12, 31}, {23, 59, 59}})
  208. ).
  209. horse_rfc2019_12340506_070809() ->
  210. horse:repeat(100000,
  211. rfc2109({{1234, 5, 6}, {7, 8, 9}})
  212. ).
  213. -endif.
  214. %% Internal.
  215. -spec pad_int(0..59) -> <<_:16>>.
  216. pad_int( 0) -> <<"00">>;
  217. pad_int( 1) -> <<"01">>;
  218. pad_int( 2) -> <<"02">>;
  219. pad_int( 3) -> <<"03">>;
  220. pad_int( 4) -> <<"04">>;
  221. pad_int( 5) -> <<"05">>;
  222. pad_int( 6) -> <<"06">>;
  223. pad_int( 7) -> <<"07">>;
  224. pad_int( 8) -> <<"08">>;
  225. pad_int( 9) -> <<"09">>;
  226. pad_int(10) -> <<"10">>;
  227. pad_int(11) -> <<"11">>;
  228. pad_int(12) -> <<"12">>;
  229. pad_int(13) -> <<"13">>;
  230. pad_int(14) -> <<"14">>;
  231. pad_int(15) -> <<"15">>;
  232. pad_int(16) -> <<"16">>;
  233. pad_int(17) -> <<"17">>;
  234. pad_int(18) -> <<"18">>;
  235. pad_int(19) -> <<"19">>;
  236. pad_int(20) -> <<"20">>;
  237. pad_int(21) -> <<"21">>;
  238. pad_int(22) -> <<"22">>;
  239. pad_int(23) -> <<"23">>;
  240. pad_int(24) -> <<"24">>;
  241. pad_int(25) -> <<"25">>;
  242. pad_int(26) -> <<"26">>;
  243. pad_int(27) -> <<"27">>;
  244. pad_int(28) -> <<"28">>;
  245. pad_int(29) -> <<"29">>;
  246. pad_int(30) -> <<"30">>;
  247. pad_int(31) -> <<"31">>;
  248. pad_int(32) -> <<"32">>;
  249. pad_int(33) -> <<"33">>;
  250. pad_int(34) -> <<"34">>;
  251. pad_int(35) -> <<"35">>;
  252. pad_int(36) -> <<"36">>;
  253. pad_int(37) -> <<"37">>;
  254. pad_int(38) -> <<"38">>;
  255. pad_int(39) -> <<"39">>;
  256. pad_int(40) -> <<"40">>;
  257. pad_int(41) -> <<"41">>;
  258. pad_int(42) -> <<"42">>;
  259. pad_int(43) -> <<"43">>;
  260. pad_int(44) -> <<"44">>;
  261. pad_int(45) -> <<"45">>;
  262. pad_int(46) -> <<"46">>;
  263. pad_int(47) -> <<"47">>;
  264. pad_int(48) -> <<"48">>;
  265. pad_int(49) -> <<"49">>;
  266. pad_int(50) -> <<"50">>;
  267. pad_int(51) -> <<"51">>;
  268. pad_int(52) -> <<"52">>;
  269. pad_int(53) -> <<"53">>;
  270. pad_int(54) -> <<"54">>;
  271. pad_int(55) -> <<"55">>;
  272. pad_int(56) -> <<"56">>;
  273. pad_int(57) -> <<"57">>;
  274. pad_int(58) -> <<"58">>;
  275. pad_int(59) -> <<"59">>;
  276. pad_int(60) -> <<"60">>;
  277. pad_int(Int) -> integer_to_binary(Int).
  278. -spec weekday(1..7) -> <<_:24>>.
  279. weekday(1) -> <<"Mon">>;
  280. weekday(2) -> <<"Tue">>;
  281. weekday(3) -> <<"Wed">>;
  282. weekday(4) -> <<"Thu">>;
  283. weekday(5) -> <<"Fri">>;
  284. weekday(6) -> <<"Sat">>;
  285. weekday(7) -> <<"Sun">>.
  286. -spec month(1..12) -> <<_:24>>.
  287. month( 1) -> <<"Jan">>;
  288. month( 2) -> <<"Feb">>;
  289. month( 3) -> <<"Mar">>;
  290. month( 4) -> <<"Apr">>;
  291. month( 5) -> <<"May">>;
  292. month( 6) -> <<"Jun">>;
  293. month( 7) -> <<"Jul">>;
  294. month( 8) -> <<"Aug">>;
  295. month( 9) -> <<"Sep">>;
  296. month(10) -> <<"Oct">>;
  297. month(11) -> <<"Nov">>;
  298. month(12) -> <<"Dec">>.
  299. -spec year(pos_integer()) -> <<_:32>>.
  300. year(1970) -> <<"1970">>;
  301. year(1971) -> <<"1971">>;
  302. year(1972) -> <<"1972">>;
  303. year(1973) -> <<"1973">>;
  304. year(1974) -> <<"1974">>;
  305. year(1975) -> <<"1975">>;
  306. year(1976) -> <<"1976">>;
  307. year(1977) -> <<"1977">>;
  308. year(1978) -> <<"1978">>;
  309. year(1979) -> <<"1979">>;
  310. year(1980) -> <<"1980">>;
  311. year(1981) -> <<"1981">>;
  312. year(1982) -> <<"1982">>;
  313. year(1983) -> <<"1983">>;
  314. year(1984) -> <<"1984">>;
  315. year(1985) -> <<"1985">>;
  316. year(1986) -> <<"1986">>;
  317. year(1987) -> <<"1987">>;
  318. year(1988) -> <<"1988">>;
  319. year(1989) -> <<"1989">>;
  320. year(1990) -> <<"1990">>;
  321. year(1991) -> <<"1991">>;
  322. year(1992) -> <<"1992">>;
  323. year(1993) -> <<"1993">>;
  324. year(1994) -> <<"1994">>;
  325. year(1995) -> <<"1995">>;
  326. year(1996) -> <<"1996">>;
  327. year(1997) -> <<"1997">>;
  328. year(1998) -> <<"1998">>;
  329. year(1999) -> <<"1999">>;
  330. year(2000) -> <<"2000">>;
  331. year(2001) -> <<"2001">>;
  332. year(2002) -> <<"2002">>;
  333. year(2003) -> <<"2003">>;
  334. year(2004) -> <<"2004">>;
  335. year(2005) -> <<"2005">>;
  336. year(2006) -> <<"2006">>;
  337. year(2007) -> <<"2007">>;
  338. year(2008) -> <<"2008">>;
  339. year(2009) -> <<"2009">>;
  340. year(2010) -> <<"2010">>;
  341. year(2011) -> <<"2011">>;
  342. year(2012) -> <<"2012">>;
  343. year(2013) -> <<"2013">>;
  344. year(2014) -> <<"2014">>;
  345. year(2015) -> <<"2015">>;
  346. year(2016) -> <<"2016">>;
  347. year(2017) -> <<"2017">>;
  348. year(2018) -> <<"2018">>;
  349. year(2019) -> <<"2019">>;
  350. year(2020) -> <<"2020">>;
  351. year(2021) -> <<"2021">>;
  352. year(2022) -> <<"2022">>;
  353. year(2023) -> <<"2023">>;
  354. year(2024) -> <<"2024">>;
  355. year(2025) -> <<"2025">>;
  356. year(2026) -> <<"2026">>;
  357. year(2027) -> <<"2027">>;
  358. year(2028) -> <<"2028">>;
  359. year(2029) -> <<"2029">>;
  360. year(Year) -> integer_to_binary(Year).