/
time-interval.lisp
201 lines (181 loc) · 8.29 KB
/
time-interval.lisp
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
192
193
194
195
196
197
198
199
200
201
;;; file: time-interval.lisp
;;;
;;; Copyright (c) 2008-2012 Cyrus Harmon (ch-lisp@bobobeach.com)
;;; All rights reserved.
;;;
;;; Redistribution and use in source and binary forms, with or without
;;; modification, are permitted provided that the following conditions
;;; are met:
;;;
;;; * Redistributions of source code must retain the above copyright
;;; notice, this list of conditions and the following disclaimer.
;;;
;;; * Redistributions in binary form must reproduce the above
;;; copyright notice, this list of conditions and the following
;;; disclaimer in the documentation and/or other materials
;;; provided with the distribution.
;;;
;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED
;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
;;;
(in-package :time-interval)
(defclass time-interval ()
((years :accessor interval-years :initarg :years :initform 0)
(months :accessor interval-months :initarg :months :initform 0)
(weeks :accessor interval-weeks :initarg :weeks :initform 0)
(days :accessor interval-days :initarg :days :initform 0)
(hours :accessor interval-hours :initarg :hours :initform 0)
(minutes :accessor interval-minutes :initarg :minutes :initform 0)
(seconds :accessor interval-seconds :initarg :seconds :initform 0)
(nanoseconds :accessor interval-nanoseconds :initarg :nanoseconds :initform 0)))
(defmethod print-object ((object time-interval) stream)
(print-unreadable-object (object stream :type t :identity t)
(format stream "~DY ~DMO ~DW ~DD ~DH ~DMI ~DS ~DNS"
(interval-years object)
(interval-months object)
(interval-weeks object)
(interval-days object)
(interval-hours object)
(interval-minutes object)
(interval-seconds object)
(interval-nanoseconds object))))
(defun copy-time-interval (orig)
(make-instance 'time-interval
:years (interval-years orig)
:months (interval-months orig)
:weeks (interval-weeks orig)
:days (interval-days orig)
:hours (interval-hours orig)
:minutes (interval-minutes orig)
:seconds (interval-seconds orig)
:nanoseconds (interval-nanoseconds orig)))
(defmacro or-zero (value)
(let ((result (gensym)))
`(let ((,result ,value))
(if ,result ,result 0))))
(defun time-interval (&rest args)
(apply #'make-instance 'time-interval args))
(defparameter *unit-hash* (make-hash-table :test 'equal))
(mapcar (lambda (unit)
(destructuring-bind (abbrev keyword)
unit
(setf (gethash abbrev *unit-hash*)
keyword)))
'(("Y" :years)
("MO" :months)
("W" :weeks)
("D" :days)
("H" :hours)
("MI" :minutes)
("S" :seconds)
("NS" :nanoseconds)))
(defun parse-time-interval-string (string)
(let ((regs (cl-ppcre:split "([ymwdhsnoiYMWDHSNOI\s]+)" string :with-registers-p t)))
(let ((keys (loop for (num unit) on regs by #'cddr
nconc
(let ((key (gethash (string-upcase unit) *unit-hash*))
(num (parse-integer num)))
(unless (and key num)
(error "bad time-interval-string!"))
(list key num)))))
(when keys
(apply #'make-instance 'time-interval keys)))))
(defun timestamp-add-interval (time interval)
(with-accessors ((years interval-years)
(months interval-months)
(weeks interval-weeks)
(days interval-days)
(hours interval-hours)
(minutes interval-minutes)
(seconds interval-seconds)
(nanoseconds interval-nanoseconds))
interval
(reduce (lambda (time increment)
(destructuring-bind (amount unit)
increment
(if amount
(local-time:timestamp+ time amount unit)
time)))
`((,years :year)
(,months :month)
(,(* 7 (or-zero weeks)) :day)
(,days :day)
(,hours :hour)
(,minutes :minute)
(,seconds :sec)
(,nanoseconds :nsec))
:initial-value time)))
(defun negate-time-interval (orig)
(make-instance 'time-interval
:years (- (interval-years orig))
:months (- (interval-months orig))
:weeks (- (interval-weeks orig))
:days (- (interval-days orig))
:hours (- (interval-hours orig))
:minutes (- (interval-minutes orig))
:seconds (- (interval-seconds orig))
:nanoseconds (- (interval-nanoseconds orig))))
(defun make-timestamp* (&key year month day hour minute second nanosecond)
(local-time:encode-timestamp
(or-zero nanosecond)
(or-zero second)
(or-zero minute)
(or-zero hour)
(or-zero day)
(or-zero month)
(or-zero year)))
(defgeneric t+ (time-or-interval-1 time-or-interval-2))
(defmethod t+ ((t1 local-time:timestamp) (t2 time-interval))
(timestamp-add-interval t1 t2))
(defmethod t+ ((t1 time-interval) (t2 local-time:timestamp))
(timestamp-add-interval t2 t1))
(defmethod t+ ((t1 time-interval) (t2 time-interval))
(make-instance 'time-interval
:years (+ (interval-years t1) (interval-years t2))
:months (+ (interval-months t1) (interval-months t2))
:weeks (+ (interval-weeks t1) (interval-weeks t2))
:days (+ (interval-days t1) (interval-days t2))
:hours (+ (interval-hours t1) (interval-hours t2))
:minutes (+ (interval-minutes t1) (interval-minutes t2))
:seconds (+ (interval-seconds t1) (interval-seconds t2))
:nanoseconds (+ (interval-nanoseconds t1) (interval-nanoseconds t2))))
(defgeneric t- (time-or-interval-1 time-or-interval-2))
(defmethod t- ((t1 local-time:timestamp) (t2 time-interval))
(timestamp-add-interval t1 (negate-time-interval t2)))
(defmethod t- ((t1 time-interval) (t2 time-interval))
(make-instance 'time-interval
:years (- (interval-years t1) (interval-years t2))
:months (- (interval-months t1) (interval-months t2))
:weeks (- (interval-weeks t1) (interval-weeks t2))
:days (- (interval-days t1) (interval-days t2))
:hours (- (interval-hours t1) (interval-hours t2))
:minutes (- (interval-minutes t1) (interval-minutes t2))
:seconds (- (interval-seconds t1) (interval-seconds t2))
:nanoseconds (- (interval-nanoseconds t1) (interval-nanoseconds t2))))
(defun timestamp-decoded-difference (tend tstart)
"Computes the difference between the individual components of
local-time:timestamp t1 and t2. Returns 8 values: dyear dmonth dday
dhh dmm dss dns dday-of-week where dyear is the difference in years,
etc..."
(destructuring-bind (dns dss dmm dhh dday dmonth dyear dday-of-week)
(mapcar #'-
(subseq (multiple-value-list
(local-time:decode-timestamp tend))
0 8)
(subseq (multiple-value-list
(local-time:decode-timestamp tstart))
0 8))
(values dyear dmonth dday dhh dmm dss dns dday-of-week)))
(defmethod t- ((t1 local-time:timestamp) (t2 local-time:timestamp))
(multiple-value-bind (y mo d h m s ns)
(timestamp-decoded-difference t1 t2)
(time-interval :years y :months mo :days d :hours h :minutes m :seconds s :nanoseconds ns)))