Erlang Central

RFC1123 Dates and Times

Revision as of 02:08, 26 August 2006 by Cyberlync (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Problem

You need to convert from an Erlang Date or Time tuple to RFC 1123 format Dates and Times.

Solution

The Erlang httpd_util module provides a solution:

1> CurrTime = erlang:universaltime().
{{2004,8,28},{8,15,38}}
2> httpd_util:rfc1123_date(CurrTime).
"Sat, 28 Aug 2004 08:15:38 GMT"

Most applications that use RFC 1123 or RFC 822 time formats require all output in GMT. However, Erlang does not provide support for RFC 822 formatted time, since it assumes all input times are GMT.

3> LocalTime = erlang:localtime().
{{2004,8,28},{1,19,37}}
4> httpd_util:rfc1123_date(LocalTime).
"Sat, 28 Aug 2004 01:19:37 GMT"

If you need to parse dates as input, you can use httpd_util:convert_request_date function:

5> DateString = "Wed, 28 Apr 2004 17:22:10 Z".
"Wed, 28 Apr 2004 17:22:10 Z"
6> httpd_util:convert_request_date(DateString).
{{2004,4,28},{17,22,10}}

RFC 1123 (and RFC 822) are used frequently as the basis for exchanging dates and times, particularly in Internet applications.