aboutsummaryrefslogtreecommitdiffstats
path: root/src/cowboy_clock.erl
diff options
context:
space:
mode:
Diffstat (limited to 'src/cowboy_clock.erl')
-rw-r--r--src/cowboy_clock.erl54
1 files changed, 19 insertions, 35 deletions
diff --git a/src/cowboy_clock.erl b/src/cowboy_clock.erl
index 5e2bf44..b439bb1 100644
--- a/src/cowboy_clock.erl
+++ b/src/cowboy_clock.erl
@@ -25,6 +25,7 @@
-export([start_link/0]).
-export([stop/0]).
-export([rfc1123/0]).
+-export([rfc1123/1]).
-export([rfc2109/1]).
%% gen_server.
@@ -61,50 +62,26 @@ stop() ->
gen_server:call(?SERVER, stop).
%% @doc Return the current date and time formatted according to RFC-1123.
-%%
-%% This format is used in the <em>date</em> header sent with HTTP responses.
-spec rfc1123() -> binary().
rfc1123() ->
ets:lookup_element(?TABLE, rfc1123, 2).
+%% @doc Return the given date and time formatted according to RFC-1123.
+-spec rfc1123(calendar:datetime()) -> binary().
+rfc1123(DateTime) ->
+ update_rfc1123(<<>>, undefined, DateTime).
+
%% @doc Return the current date and time formatted according to RFC-2109.
%%
%% This format is used in the <em>set-cookie</em> header sent with
%% HTTP responses.
-spec rfc2109(calendar:datetime()) -> binary().
-rfc2109(LocalTime) ->
- {{YYYY,MM,DD},{Hour,Min,Sec}} =
- case calendar:local_time_to_universal_time_dst(LocalTime) of
- [Gmt] -> Gmt;
- [_,Gmt] -> Gmt;
- [] ->
- %% The localtime generated by cowboy_cookies may fall within
- %% the hour that is skipped by daylight savings time. If this
- %% is such a localtime, increment the localtime with one hour
- %% and try again, if this succeeds, subtracting the max_age
- %% from the resulting universaltime and converting to a local
- %% time will yield the original localtime.
- {Date, {Hour1, Min1, Sec1}} = LocalTime,
- LocalTime2 = {Date, {Hour1 + 1, Min1, Sec1}},
- case calendar:local_time_to_universal_time_dst(LocalTime2) of
- [Gmt] -> Gmt;
- [_,Gmt] -> Gmt
- end
- end,
- Wday = calendar:day_of_the_week({YYYY,MM,DD}),
- DayBin = pad_int(DD),
- YearBin = list_to_binary(integer_to_list(YYYY)),
- HourBin = pad_int(Hour),
- MinBin = pad_int(Min),
- SecBin = pad_int(Sec),
- WeekDay = weekday(Wday),
- Month = month(MM),
- <<WeekDay/binary, ", ",
- DayBin/binary, " ", Month/binary, " ",
- YearBin/binary, " ",
- HourBin/binary, ":",
- MinBin/binary, ":",
- SecBin/binary, " GMT">>.
+rfc2109({Date = {Y, Mo, D}, {H, Mi, S}}) ->
+ Wday = calendar:day_of_the_week(Date),
+ << (weekday(Wday))/binary, ", ", (pad_int(D))/binary, "-",
+ (month(Mo))/binary, "-", (list_to_binary(integer_to_list(Y)))/binary,
+ " ", (pad_int(H))/binary, $:, (pad_int(Mi))/binary,
+ $:, (pad_int(S))/binary, " GMT" >>.
%% gen_server.
@@ -215,6 +192,13 @@ month(12) -> <<"Dec">>.
-ifdef(TEST).
+rfc2109_test_() ->
+ Tests = [
+ {<<"Sat, 14-May-2011 14:25:33 GMT">>, {{2011, 5, 14}, {14, 25, 33}}},
+ {<<"Sun, 01-Jan-2012 00:00:00 GMT">>, {{2012, 1, 1}, { 0, 0, 0}}}
+ ],
+ [{R, fun() -> R = rfc2109(D) end} || {R, D} <- Tests].
+
update_rfc1123_test_() ->
Tests = [
{<<"Sat, 14 May 2011 14:25:33 GMT">>, undefined,