From e20b180c21e9420f041321658b8195eaa5927736 Mon Sep 17 00:00:00 2001 From: Andrei Nesterov Date: Fri, 21 Oct 2016 13:01:18 +0300 Subject: Add support for IMF-fixdate formated date generation --- src/cow_date.erl | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) (limited to 'src/cow_date.erl') diff --git a/src/cow_date.erl b/src/cow_date.erl index b805aec..cdd62e4 100644 --- a/src/cow_date.erl +++ b/src/cow_date.erl @@ -15,7 +15,9 @@ -module(cow_date). -export([parse_date/1]). +-export([rfc1123/1]). -export([rfc2109/1]). +-export([rfc7231/1]). -ifdef(TEST). -include_lib("triq/include/triq.hrl"). @@ -236,6 +238,49 @@ horse_rfc2019_12340506_070809() -> ). -endif. +%% @doc Return the date formatted according to RFC1123. + +-spec rfc1123(calendar:datetime()) -> binary(). +rfc1123(DateTime) -> + rfc7231(DateTime). + +%% @doc Return the date formatted according to RFC7231. + +-spec rfc7231(calendar:datetime()) -> binary(). +rfc7231({Date = {Y, Mo, D}, {H, Mi, S}}) -> + Wday = calendar:day_of_the_week(Date), + << (weekday(Wday))/binary, ", ", + (pad_int(D))/binary, " ", + (month(Mo))/binary, " ", + (year(Y))/binary, " ", + (pad_int(H))/binary, ":", + (pad_int(Mi))/binary, ":", + (pad_int(S))/binary, " GMT" >>. + +-ifdef(TEST). +rfc7231_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 = rfc7231(D) end} || {R, D} <- Tests]. + +horse_rfc7231_20130101_000000() -> + horse:repeat(100000, + rfc7231({{2013, 1, 1}, {0, 0, 0}}) + ). + +horse_rfc7231_20131231_235959() -> + horse:repeat(100000, + rfc7231({{2013, 12, 31}, {23, 59, 59}}) + ). + +horse_rfc7231_12340506_070809() -> + horse:repeat(100000, + rfc7231({{1234, 5, 6}, {7, 8, 9}}) + ). +-endif. + %% Internal. -spec pad_int(0..59) -> <<_:16>>. -- cgit v1.2.3