/
dates.clj
39 lines (32 loc) · 1.38 KB
/
dates.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
38
39
(ns clj-cctray.dates
"Functions for converting dates found in the CCTray XML."
(:require [java-time.format :as f]
[java-time.temporal :as t]
[clojure.string :refer [blank?]])
(:import (java.time ZoneId)))
(defn- formatter [format]
(.withZone (f/formatter format) (ZoneId/of "Z")))
(def iso-format
"Format string in the ISO 8601 format, yyyy-MM-dd'T'HH:mm:ss.SSSVV"
"yyyy-MM-dd'T'HH:mm:ss.SSSVV")
(def ^:private date-parser (formatter "yyyy-MM-dd'T'HH:mm:ss[.SSS][VV]"))
(defn parse-date [s]
(if-not (blank? s)
(try
(t/instant date-parser s)
(catch Exception _ nil))))
(defn print-date [format date]
(if (t/instant? date)
(f/format (formatter format) date)
(str date)))
(defn extract-dates
"Extracts the last and next build times from the CCTray XML into real DateTime objects."
[{:keys [last-build-time next-build-time]}]
{:last-build-time (parse-date last-build-time)
:next-build-time (parse-date next-build-time)})
(defn print-dates
"Prints the last and next build times using the given string format. They must be DateTime objects to be printed
correctly using the format string, if any other object is found it will just be converted to a string using `str`"
[format {:keys [last-build-time next-build-time]}]
{:last-build-time (print-date format last-build-time)
:next-build-time (print-date format next-build-time)})