1

This date format is used in HTTP Cookie Expires field.

This is my code so far

(ns cookie-handler
  (:require[clj-time.format :as f])
  (:import (org.joda.time DateTimeZone)))

(def custom-formatter2 
  (f/formatter "EEE, dd-MMM-yyyy HH:mm:ss zzz" (DateTimeZone/forID "Etc/GMT")))

I invoke this in repl

(f/unparse custom-formatter2 (c/to-date-time  (.. (Calendar/getInstance) (getTime) )))

And this is what I get : "Thu, 23-Apr-2015 16:20:22 +00:00"

How do I initialize formatter so that it outputs a date string like "Thu, 23-Apr-2015 16:20:22 GMT"

user193116
  • 3,498
  • 6
  • 39
  • 58

2 Answers2

2

There is a java version of this issue at java date format - GMT 0700 (PDT)

If all you want is to have GMT at the end of the formatted date string, you can add those characters to the end of the format string

(def custom1 (f/formatter-local "EEE, dd-MMM-yyyy HH:mm:ss 'GMT'"))
(def formatted-date-1 (f/unparse custom1 (t/now)))

"Thu, 23-Apr-2015 19:12:58 GMT"

If you really need GMT followed by the offset, the same idea applies

(def custom2 (f/formatter-local "EEE, dd-MMM-yyyy HH:mm:ss 'GMT'z"))
(def today (t/to-time-zone (t/now) (t/time-zone-for-offset -6)))
(def formatted-date-2 (f/unparse custom-formatter today))

"Thu, 23-Apr-2015 13:12:58 GMT-06:00"

Community
  • 1
  • 1
GregA100k
  • 1,385
  • 1
  • 11
  • 16
0

I use the following in a library of mine that needs to output HTTP-spec compliant timestamps:

(def ^:private time-format (f/formatter "EEE, dd MMM yyyy HH:mm:ss"))

(defn- time->str
  [time]
  (str (f/unparse time-format time) " GMT"))

You can probably include GMT in the format string like G_A suggested, but I haven't tried that.

I do think that using f/formatter rather than f/formatter-local is important though, so that your local timestamp gets converted to UTC before it's converted to a string.

liwp
  • 6,746
  • 1
  • 27
  • 39