-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtimestamp.lisp
More file actions
191 lines (160 loc) · 6.5 KB
/
timestamp.lisp
File metadata and controls
191 lines (160 loc) · 6.5 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
(in-package #:els-web)
(defvar *unix-epoch-difference* (encode-universal-time 0 0 0 1 1 1970 0))
(defclass timestamp ()
((year :initarg :year :accessor year)
(day :initarg :day :accessor day)
(month :initarg :month :accessor month)
(hour :initarg :hour :accessor hour)
(minute :initarg :minute :accessor minute)
(sec :initarg :sec :accessor sec)
(offset :initarg :offset :accessor offset))
(:default-initargs
:year 0 :month 1 :day 1 :hour 0 :minute 0 :sec 0 :offset 0))
(defmethod print-object ((timestamp timestamp) stream)
(format stream "@~a" (date-machine timestamp)))
(defmethod make-load-form ((timestamp timestamp) &optional env)
(declare (ignore env))
`(read-timestamp
(make-string-input-stream
,(date-machine timestamp))))
(defun timestamp->universal (timestamp)
(encode-universal-time (sec timestamp) (minute timestamp) (hour timestamp)
(day timestamp) (month timestamp) (year timestamp)
(/ (offset timestamp) -60)))
(defun timestamp->unix (timestamp)
(- (timestamp->universal timestamp) *unix-epoch-difference*))
(defun make-timestamp (&optional (y 0) (m 1) (d 1) (hh 0) (mm 0) (ss 0) (offset 0))
(make-instance 'timestamp :year y :month m :day d
:hour hh :minute mm :sec ss
:offset offset))
(defun universal->timestamp (universal &optional time-zone)
(multiple-value-bind (ss mm hh d m y dow dst zone) (decode-universal-time universal time-zone)
(declare (ignore dow dst))
(make-timestamp y m d hh mm ss (round (* -60 zone)))))
(defun unix->timestamp (unix &optional time-zone)
(universal->timestamp (+ unix *unix-epoch-difference*) time-zone))
(defun copy-timestamp (timestamp)
(make-timestamp (year timestamp) (month timestamp) (day timestamp)
(hour timestamp) (minute timestamp) (sec timestamp)
(offset timestamp)))
(defun timestamp->utc (timestamp)
(universal->timestamp (timestamp->universal timestamp) 0))
(defun adjust-timestamp (timestamp offset)
(universal->timestamp
(+ (timestamp->universal timestamp) offset)
(/ (offset timestamp) -60)))
(defmethod day-of-week ((timestamp timestamp))
(1+ (nth-value 6 (decode-universal-time (timestamp->universal timestamp)
(/ (offset timestamp) 60)))))
(defun date-machine (stamp)
(format NIL "~4,'0d-~2,'0d-~2,'0dT~2,'0d:~2,'0d:~2,'0d~:[~:[-~;+~]~2,'0d:~2,'0d~;Z~]"
(year stamp) (month stamp) (day stamp) (hour stamp) (minute stamp) (sec stamp)
(= 0 (offset stamp)) (< 0 (offset stamp)) (floor (abs (offset stamp)) 60) (mod (abs (offset stamp)) 60)))
(defun date-human (stamp)
(format NIL "~4,'0d.~2,'0d.~2,'0d"
(year stamp) (month stamp) (day stamp)))
(defun month-name (num)
(ecase num
(1 "January")
(2 "February")
(3 "March")
(4 "April")
(5 "May")
(6 "June")
(7 "July")
(8 "August")
(9 "September")
(10 "October")
(11 "November")
(12 "December")))
(defun day-of-week-name (num)
(ecase num
(1 "Monday")
(2 "Tuesday")
(3 "Wednesday")
(4 "Thursday")
(5 "Friday")
(6 "Saturday")
(7 "Sunday")))
(defun plural-suffix (n)
(case (mod n 10)
(0 "th")
(1 "st")
(2 "nd")
(3 "rd")
(T "th")))
(defun date-title (stamp)
(format NIL "~a ~d~a"
(month-name (month stamp))
(day stamp) (plural-suffix (day stamp))))
(defun date-fancy (stamp)
(format NIL "~a ~d~a of ~a ~d, ~a:~2,'0d ~:[(~:[-~;+~]~2,'0d:~2,'0d)~;~]"
(day-of-week-name (day-of-week stamp)) (day stamp) (plural-suffix (day stamp)) (month-name (month stamp)) (year stamp) (hour stamp) (minute stamp)
(= 0 (offset stamp)) (< 0 (offset stamp)) (floor (abs (offset stamp)) 60) (mod (abs (offset stamp)) 60)))
(defun date-clock (stamp)
(format NIL "~a:~2,'0d"
(hour stamp) (minute stamp)))
(lquery:define-lquery-function time (node stamp &optional (format :human))
(setf (plump:attribute node "datetime") (date-machine stamp))
(setf (plump:attribute node "title") (date-fancy stamp))
(setf (plump:children node) (plump:make-child-array))
(plump:make-text-node node (ecase format
(:machine (date-machine stamp))
(:human (date-human stamp))
(:fancy (date-fancy stamp))
(:title (date-title stamp))
(:clock (date-clock stamp))))
node)
(defun parse-timestring (timestring)
(flet ((maybe-parse (num &optional (default 0))
(if num (parse-integer num) default)))
(cl-ppcre:register-groups-bind (NIL y m d NIL hh mm ss NIL od ohh omm)
("((\\d{4})-(\\d{2})-(\\d{2}))?([Tt]?(\\d{2}):(\\d{2}):?(\\d{2})?)?(([+-Zz])(\\d{2})?:?(\\d{2})?)?" timestring)
(make-timestamp (maybe-parse y) (maybe-parse m) (maybe-parse d)
(maybe-parse hh) (maybe-parse mm) (maybe-parse ss)
(if od
(* (if (string= od "+") 1 -1)
(+ (* 60 (maybe-parse ohh))
(maybe-parse omm)))
0)))))
(defun read-timestring (stream)
(with-output-to-string (str)
(loop for c = (read-char stream nil)
while (and c (or (digit-char-p c) (member c '(#\: #\T #\t #\: #\- #\+ #\Z #\z #\.))))
do (princ c str)
finally (when c (unread-char c stream)))))
(defun read-timestamp (stream &optional char)
(declare (ignore char))
(parse-timestring (read-timestring stream)))
(named-readtables:defreadtable :els-web
(:merge :standard)
(:macro-char #\@ #'read-timestamp))
(defun merge-timestamp (time base)
(let ((new (copy-timestamp base)))
(flet ((maybe-copy (slot)
(unless (= 0 (slot-value time slot))
(setf (slot-value new slot) (slot-value time slot)))))
(maybe-copy 'year)
(maybe-copy 'month)
(maybe-copy 'day)
(maybe-copy 'hour)
(maybe-copy 'minute)
(maybe-copy 'sec)
(maybe-copy 'offset))
new))
(defun timestamp= (a b)
(and (= (year a) (year b))
(= (month a) (month b))
(= (day a) (day b))
(= (hour a) (hour b))
(= (minute a) (minute b))
(= (sec a) (sec b))
(= (offset a) (offset b))))
(defun timestamp< (a b)
(< (timestamp->universal a)
(timestamp->universal b)))
(defun timestamp> (a b)
(> (timestamp->universal a)
(timestamp->universal b)))
(defun now ()
(universal->timestamp (get-universal-time)))