-
Notifications
You must be signed in to change notification settings - Fork 517
/
time.clj
37 lines (31 loc) · 1.07 KB
/
time.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
(ns ring.util.time
"Functions for dealing with time and dates in HTTP requests."
(:require [clojure.string :as str])
(:import [java.text ParseException SimpleDateFormat]
[java.util Locale TimeZone]))
(def ^:no-doc http-date-formats
{:rfc1123 "EEE, dd MMM yyyy HH:mm:ss zzz"
:rfc1036 "EEEE, dd-MMM-yy HH:mm:ss zzz"
:asctime "EEE MMM d HH:mm:ss yyyy"})
(defn- ^SimpleDateFormat formatter [format]
(doto (SimpleDateFormat. ^String (http-date-formats format) Locale/US)
(.setTimeZone (TimeZone/getTimeZone "GMT"))))
(defn- attempt-parse [date format]
(try
(.parse (formatter format) date)
(catch ParseException _ nil)))
(defn- trim-quotes [s]
(str/replace s #"^'|'$" ""))
(defn parse-date
"Attempt to parse a HTTP date. Returns nil if unsuccessful."
{:added "1.2"}
[http-date]
(->> (keys http-date-formats)
(map (partial attempt-parse (trim-quotes http-date)))
(remove nil?)
(first)))
(defn format-date
"Format a date as RFC1123 format."
{:added "1.2"}
[^java.util.Date date]
(.format (formatter :rfc1123) date))