2

I'm trying to create a HTTP 1/1 compliant date header using standard unix date(1) in order to post this to a RESTful server using curl or similar.

Any ideas what format to pass to date(1) to get this to be RFC 1123 compliant?

Many thanks

3 Answers 3

1

man date for whatever version of date your OS provides and use the correct switches to print (see man strftime), from from left to right, with a space between each, first the date:

Day (Three letter abbreviation Mon-Tue-Wed...) followed by a comma ,,
the month (Three letter abbreviation Jan Feb Mar ...)
the year (4 digit notation 1970, 1971 ...)
and then time HH:MM:SS.

And you might get something like Fri, 20 May 2016 20:22:33 GMT

1
  • went with DATE=$(date -u +%a,\ %d\ %b\ %Y\ %H:%M:%S\ GMT) in the end... not sure if these switches are right, but it seems to work.
    – Brad
    Commented May 20, 2016 at 20:55
5

env TZ=GMT; date '+%a, %d %b %Y %T %Z'

  1. %T is equivalent to %H:%M:%S.
  2. %Z is replaced by the time zone name.

The solution by @brad, DATE=$(date -u +%a,\ %d\ %b\ %Y\ %H:%M:%S\ GMT), would be incorrect if the time zone setting of your system is not GMT. For example:

env TZ=Asia/Taipei; date -u +%a,\ %d\ %b\ %Y\ %H:%M:%S\ GMT

%H:%M:%S would be GMT+8 while the output is GMT, but should be CST.

1
  • -u should always provide universal time regardless of the timezone. I can run TZ=Asia/Taipei date -u +%H:%M or TZ=Europe/Madrid date -u +%H:%M and I get the same result from both.
    – Walf
    Commented Jun 5 at 2:52
0

Either

LC_ALL=C date -u '+%a, %d %b %Y %T GMT'

or

LC_ALL=C TZ=GMT date '+%a, %d %b %Y %T GMT'

are correct.

Don't use semicolons, you only need to set those environment variables per-invocation. LC_ALL is necessary because %a and %b (among other format sequences) are locale-specific, e.g. in French the first one produces days like mer. You don't need to generate the timezone code because it's always the literal text GMT.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .