Created
November 18, 2010 19:40
-
-
Save KirinDave/705478 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn- pre-and-unparse-date [pre formatter string] | |
"Expects pre to return a vector, [v, data]. Returns [(unparse formatter v), data]. | |
If nil is returned from pre, it is propagated. If unparse fails, also returns nil." | |
(when-let [[v data] (pre string)] | |
(try | |
[(parse formatter v) data] | |
(catch IllegalArgumentException e | |
nil)))) | |
(defn- split-out-timezone [str] | |
(rest (re-matches #"^(.*) ([A-Z]{3})$" str))) | |
(defonce date-parsing-strategies | |
(list | |
[split-out-timezone ; 1123 | |
(formatter "E, d MMM Y H:m:s")] | |
[split-out-timezone ; 1036 | |
(formatter "E, d-MMM-yy H:m:s")] | |
[(juxt identity (constantly "UTC")) | |
(formatter "E MMM d H:m:s yyyy")])) | |
(defn date-timezone-from-string | |
"Attempts to extract a date from a string, using the HTTP 1.1 mandated | |
formats in this order: RFC 822/1123, RFC 850/1036, Ansi CTime. | |
(examples from the http 1.1 spec: | |
Sun, 06 Nov 1994 08:49:37 GMT ; RFC 822, updated by RFC 1123 | |
Sunday, 06-Nov-94 08:49:37 GMT ; RFC 850, obsoleted by RFC 1036 | |
Sun Nov 6 08:49:37 1994 ; ANSI C's asctime() format)" | |
[string] | |
(first (keep | |
(fn [[pre fmt]] | |
(println pre " ::: " fmt) | |
(pre-and-unparse-date pre fmt string)) | |
date-parsing-strategies))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment