-
Notifications
You must be signed in to change notification settings - Fork 1
/
3_dates_and_times.clj
73 lines (61 loc) · 2.35 KB
/
3_dates_and_times.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
;; @@PLEAC@@_3.0 Introduction
;;------------------------------------
;; Use a calendar to compute year, month, day, hour, minute and second values.
(import '(java.util Calendar))
(import '(java.util GregorianCalendar))
(defn print-day-of-year []
(let [cal (GregorianCalendar.)]
(printf "Today is day %d of the current year.\n"
(.get cal Calendar/DAY_OF_YEAR))))
;; @@PLEAC@@_3.1 Finding Today's Date
;;------------------------------------
(import '(java.util Calendar))
(import '(java.util GregorianCalendar))
(defn todays-date []
(let [cal (GregorianCalendar.)
day (.get cal Calendar/DATE)
month (.get cal Calendar/MONTH)
year (.get cal Calendar/YEAR)]
[day month year]))
(defn print-todays-date []
(let [[day month year] (todays-date)]
(printf "The current date is %d %02d %02d\n" year (inc month) day)))
;; @@PLEAC@@_3.2 Converting DMYHMS to Epoch Seconds
;;------------------------------------
(import '(java.util Calendar))
(import '(java.util TimeZone))
(import '(java.util GregorianCalendar))
(defn epoch-seconds-of-dmyhms [tz day month year hour minute second]
(let [cal (GregorianCalendar.)
zone (TimeZone/getTimeZone tz)]
(do
(.setTimeZone cal zone)
(.set cal Calendar/DAY_OF_MONTH day)
(.set cal Calendar/MONTH month)
(.set cal Calendar/YEAR year)
(.set cal Calendar/HOUR_OF_DAY hour)
(.set cal Calendar/MINUTE minute)
(.set cal Calendar/SECOND second)
(int (/ (.getTime (.getTime cal)) 1000)))))
(epoch-seconds-of-dmyhms "UTC" 4 10 2011 12 30 55)
;; @@PLEAC@@_3.3 Converting Epoch Seconds to DMYHMS
;;------------------------------------
(import '(java.util Calendar))
(import '(java.util Date))
(import '(java.util GregorianCalendar))
(defn dmyhms-of-epoch-seconds [seconds]
(let [cal (GregorianCalendar.)
date (Date. (* seconds 1000))]
(do
(.setTime cal date)
[(.get cal Calendar/DAY_OF_MONTH)
(.get cal Calendar/MONTH)
(.get cal Calendar/YEAR)
(.get cal Calendar/HOUR_OF_DAY)
(.get cal Calendar/MINUTE)
(.get cal Calendar/SECOND)])))
(defn print-dmyhms-of-epoch-seconds [seconds]
(let [[day month year hour minute seconds] (dmyhms-of-epoch-seconds seconds)]
(printf "%02d-%02d-%d %02d:%02d:%02d\n"
day (inc month) year hour minute seconds)))
;; 3.4 Adding or Subtracting from a Date