-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathperiods.lisp
1824 lines (1624 loc) · 60.5 KB
/
periods.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; periods --- A library for working with periods of time
;; Copyright (C) 2007 John Wiegley. All rights reserved.
;; Author: John Wiegley <[email protected]>
;; Created: 29 Oct 2007
;; Modified: 17 Nov 2007
;; Version: 0.2
;; Keywords: lisp programming development
;; X-URL: http://www.newartisans.com/
;; 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.
;;
;; - Neither the name of New Artisans LLC nor the names of its
;; contributors may be used to endorse or promote products derived from
;; this software without specific prior written permission.
;;
;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
;; "AS IS" AND ANY EXPRESS 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 COPYRIGHT
;; OWNER OR CONTRIBUTORS 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.
;;; Commentary:
;; The PERIODS library is fully described in the PDF documentation which
;; accompanies this source code. Please refer there for complete details.
(declaim (optimize (debug 3) (safety 3) (speed 1) (space 0)))
(defpackage :periods
(:use :common-lisp :local-time)
(:nicknames :time-periods)
(:export leapp
increment-time
decrement-time
floor-time
parse-time-period
parse-time-range
time-period-generator
time-periods
time-period
time-range
time-range-begin
time-range-end
time-range-duration
time-within-range-p
with-timestamp-range
update-range
map-over-time
do-over-time
collate-by-time-period
sleep-until
day-of-week
time-difference
duration-seconds
falls-on-weekend-p
current-year
find-smallest-resolution
duration
add-duration
add-time
subtract-duration
subtract-time
*input-time-format*
*output-time-format*
fixed-time
read-fixed-time
strptime
strptime-decoded
strftime)
(:shadow day-of
days-in-month))
(in-package :periods)
;;;_ * Global variables
(defvar *input-time-format* "%Y/%m/%d%| %H:%M:%S")
(defvar *output-time-format* *input-time-format*)
;;;_ * Basic utility functions
(defparameter *days-in-months* #(31 28 31 30 31 30 31 31 30 31 30 31))
;; Snippet courtesy of Xach on #lisp
(declaim (inline leapp))
(defun leapp (year)
"Return T if YEAR falls on a leap year."
(cond ((zerop (mod year 400)) t)
((zerop (mod year 100)) nil)
((zerop (mod year 4)) t)
(t nil)))
(declaim (inline current-year))
(defun current-year ()
"Return the current year as a FIXNUM."
(nth-value 5 (get-decoded-time)))
(defun days-in-month (month &optional year)
(let ((days (aref *days-in-months* (1- month)))
(the-year (or year (current-year))))
(if (and (= month 2)
(leapp the-year))
(incf days)
days)))
(declaim (inline nanosecond-part microsecond-part millisecond-part))
(defun nanosecond-part (bignum)
(nth-value 1 (floor bignum 1000)))
(defun microsecond-part (bignum)
(nth-value 1 (floor (floor bignum 1000) 1000)))
(defun millisecond-part (bignum)
(nth-value 0 (floor bignum 1000000)))
(declaim (inline set-nanosecond-part set-microsecond-part
set-millisecond-part))
(defun set-nanosecond-part (bignum nsecs)
(+ (* (floor bignum 1000) 1000) nsecs))
(defun set-microsecond-part (bignum usecs)
(+ (* (floor bignum 1000000) 1000000)
(* usecs 1000)
(nth-value 1 (floor bignum 1000))))
(defun set-millisecond-part (bignum msecs)
(+ (* msecs 1000000)
(nth-value 1 (floor bignum 1000000))))
(defun floor-time (fixed-time &optional resolution)
"Reduce a fixed time to be no finer than RESOLUTION.
For example, if the date is 2007-04-20, and the resolution is :month, the
date is floored to 2007-04-01. Anything smaller than the resolution is
reduced to zero (or 1, if it is a day or month being reduced)."
(declare (type timestamp fixed-time))
(multiple-value-bind
(nsec ss mm hh day month year)
(decode-timestamp fixed-time)
(block nil
(if (eq resolution :nanosecond) (return))
(setf nsec (set-nanosecond-part nsec 0))
(if (eq resolution :microsecond) (return))
(setf nsec (set-microsecond-part nsec 0))
(if (eq resolution :millisecond) (return))
(setf nsec (set-millisecond-part nsec 0))
(if (eq resolution :second) (return))
(setf ss 0)
(if (eq resolution :minute) (return))
(setf mm 0)
(if (eq resolution :hour) (return))
(setf hh 0)
(if (or (eq resolution :day)
(eq resolution :day-of-week)) (return))
(setf day 1)
(if (eq resolution :month) (return))
(setf month 1))
(encode-timestamp nsec ss mm hh day month year)))
(defun find-smallest-resolution (step-by)
"Examine the property list STEP-BY and return the smallest unit of time
specified.
For example, given the following property list:
(:DAY 10 :HOUR 5 :MINUTE 2)
The result is :MINUTE."
(cond
((member :nanosecond step-by) :nanosecond)
((member :microsecond step-by) :microsecond)
((member :millisecond step-by) :millisecond)
((member :second step-by) :second)
((member :minute step-by) :minute)
((member :hour step-by) :hour)
((member :day step-by) :day)
((member :day-of-week step-by) :day-of-week)
((member :month step-by) :month)
(t (error "Could not find a time resolution in ~S" step-by))))
;;;_ * FIXED-TIME
(deftype fixed-time ()
'local-time:timestamp)
(defun fixed-time (&rest args)
"Return a fixed point in time relative to the time of the call. ARGS is a
property list giving a specific precision for the return value.
If the keyword argument :NOW is given, all else is ignored; this is
equivalent to calling LOCAL-TIME:NOW.
Otherwise, any keyword arguments given override their corresponding elements
in the current time. Further, any elements smaller in resolution than the
finest specified element are reduced to 0 or 1, according to their position.
For example, assuming the current time is \"@2007-11-17T23:02:00.000\",
compare these outputs:
(fixed-time :month 4) => @2007-04-01T00:00:00.000
(fixed-time :day 10) => @2007-11-10T00:00:00.000
(fixed-time :hour 15) => @2007-11-17T15:00:00.000
This behavior makes it very easy to return a fixed time for \"april of this
year\", etc. If you wish to determine the date of the previous April, while
preserving the current day of the month, hour of the day, etc., then see the
function PREVIOUS-TIME."
(if (member :now args)
(local-time:now)
(multiple-value-bind
(nsec ss mm hh day month year)
(decode-timestamp (local-time:now))
(block nil
(let ((this-nsec (getf args :nanosecond))
(this-usec (getf args :microsecond))
(this-ms (getf args :millisecond))
(this-ss (getf args :second))
(this-mm (getf args :minute))
(this-hh (getf args :hour))
(this-day (getf args :day))
(this-month (getf args :month))
(this-year (getf args :year))
(truncate t))
(if this-nsec
(setf nsec (set-nanosecond-part nsec this-nsec)
truncate nil)
(if truncate (setf nsec (set-nanosecond-part nsec 0))))
(if this-usec
(setf nsec (set-microsecond-part nsec this-usec)
truncate nil)
(if truncate (setf nsec (set-microsecond-part nsec 0))))
(if this-ms
(setf nsec (set-millisecond-part nsec this-ms)
truncate nil)
(if truncate (setf nsec (set-millisecond-part nsec 0))))
(if this-ss
(setf ss this-ss truncate nil)
(if truncate (setf ss 0)))
(if this-mm
(setf mm this-mm truncate nil)
(if truncate (setf mm 0)))
(if this-hh
(setf hh this-hh truncate nil)
(if truncate (setf hh 0)))
(if this-day
(setf day this-day truncate nil)
(if truncate (setf day 1)))
(if this-month
(setf month this-month truncate nil)
(if truncate (setf month 1)))
(if this-year
(setf year this-year))))
(encode-timestamp nsec ss mm hh day month year))))
(declaim (inline year-of
month-of
day-of
hour-of
minute-of
second-of
millisecond-of
microsecond-of
nanosecond-of))
(defun year-of (fixed-time)
(nth-value 6 (decode-timestamp fixed-time)))
(defun month-of (fixed-time)
(nth-value 5 (decode-timestamp fixed-time)))
(defun day-of (fixed-time)
(nth-value 4 (decode-timestamp fixed-time)))
(defun hour-of (fixed-time)
(nth-value 3 (decode-timestamp fixed-time)))
(defun minute-of (fixed-time)
(nth-value 2 (decode-timestamp fixed-time)))
(defun second-of (fixed-time)
(nth-value 1 (decode-timestamp fixed-time)))
(defun millisecond-of (fixed-time)
(millisecond-part (nth-value 0 (decode-timestamp fixed-time))))
(defun microsecond-of (fixed-time)
(microsecond-part (nth-value 0 (decode-timestamp fixed-time))))
(defun nanosecond-of (fixed-time)
(nanosecond-part (nth-value 0 (decode-timestamp fixed-time))))
(declaim (inline day-of-week))
(defun day-of-week (fixed-time)
"Return the day of the week associated with a given FIXED-TIME.
The result is a FIXNUM with 0 representing Sunday, through 6 on Saturday."
(declare (type fixed-time fixed-time))
(nth-value 7 (decode-timestamp fixed-time)))
(declaim (inline falls-on-weekend-p))
(defun falls-on-weekend-p (fixed-time)
"Return T if the given FIXED-TIME occurs on a Saturday or Sunday."
(let ((dow (day-of-week fixed-time)))
(or (= 0 dow) (= 6 dow))))
;;;_ * RELATIVE-DURATION
(defstruct duration
(years 0 :type integer)
(months 0 :type integer)
(days 0 :type integer)
(hours 0 :type integer)
(minutes 0 :type integer)
(seconds 0 :type integer)
(milliseconds 0 :type integer)
(microseconds 0 :type integer)
(nanoseconds 0 :type integer))
(declaim (inline duration))
(defun duration (&rest args)
"Create a DURATION object.
One thing to note about duration: there is no way to determine the total
length of a duration in terms of any specific time quantity, without first
binding that duration to a fixed point in time (after all, how many days are
in a month if you don't know which month it is?) Therefore, those looking for
a function like \"duration-seconds\" are really wanting to work with ranges,
not just durations."
(apply #'make-duration args))
(defmacro with-skippers (&body body)
`(labels
((skip-year (skip)
(incf year skip))
(skip-month (skip)
(if (minusp skip)
(let ((remainder (+ (1- month) skip)))
(if (minusp remainder)
(progn
(skip-year -1)
(setf month 12)
(skip-month (1+ remainder)))
(incf month skip)))
(if (plusp skip)
(let ((remainder (- (+ month skip) 12)))
(if (plusp remainder)
(progn
(skip-year 1)
(setf month 1)
(skip-month (1- remainder)))
(incf month skip))))))
(skip-day (skip)
(if (minusp skip)
(let ((remainder (+ (1- day) skip)))
(if (minusp remainder)
(progn
(skip-month -1)
(setf day (days-in-month month year))
(skip-day (1+ remainder)))
(incf day skip)))
(if (plusp skip)
(let ((remainder (- (+ day skip)
(days-in-month month year))))
(if (plusp remainder)
(progn
(skip-month 1)
(setf day 1)
(skip-day (1- remainder)))
(incf day skip))))))
(skip-hour (skip)
(if (minusp skip)
(let ((remainder (+ hh skip)))
(if (minusp remainder)
(progn
(skip-day -1)
(setf hh 23)
(skip-hour (1+ remainder)))
(incf hh skip)))
(if (plusp skip)
(let ((remainder (- (+ hh skip) 23)))
(if (plusp remainder)
(progn
(skip-day 1)
(setf hh 0)
(skip-hour (1- remainder)))
(incf hh skip))))))
(skip-minute (skip)
(if (minusp skip)
(let ((remainder (+ mm skip)))
(if (minusp remainder)
(progn
(skip-hour -1)
(setf mm 59)
(skip-minute (1+ remainder)))
(incf mm skip)))
(if (plusp skip)
(let ((remainder (- (+ mm skip) 59)))
(if (plusp remainder)
(progn
(skip-hour 1)
(setf mm 0)
(skip-minute (1- remainder)))
(incf mm skip))))))
(skip-second (skip)
(if (minusp skip)
(let ((remainder (+ ss skip)))
(if (minusp remainder)
(progn
(skip-minute -1)
(setf ss 59)
(skip-second (1+ remainder)))
(incf ss skip)))
(if (plusp skip)
(let ((remainder (- (+ ss skip) 59)))
(if (plusp remainder)
(progn
(skip-minute 1)
(setf ss 0)
(skip-second (1- remainder)))
(incf ss skip))))))
(skip-millisecond (skip)
(if (minusp skip)
(let ((remainder (+ (millisecond-part nsec) skip)))
(if (minusp remainder)
(progn
(skip-second -1)
(setf nsec (set-millisecond-part nsec 999))
(skip-millisecond (1+ remainder)))
(setf nsec (set-millisecond-part
nsec (+ (millisecond-part nsec) skip)))))
(if (plusp skip)
(let ((remainder (- (+ (millisecond-part nsec) skip) 999)))
(if (plusp remainder)
(progn
(skip-second 1)
(setf nsec (set-millisecond-part nsec 0))
(skip-millisecond (1- remainder)))
(setf nsec (set-millisecond-part
nsec (+ (millisecond-part nsec)
skip))))))))
(skip-microsecond (skip)
(if (minusp skip)
(let ((remainder (+ (microsecond-part nsec) skip)))
(if (minusp remainder)
(progn
(skip-second -1)
(setf nsec (set-microsecond-part nsec 999))
(skip-microsecond (1+ remainder)))
(setf nsec (set-microsecond-part
nsec (+ (microsecond-part nsec) skip)))))
(if (plusp skip)
(let ((remainder (- (+ (microsecond-part nsec) skip) 999)))
(if (plusp remainder)
(progn
(skip-second 1)
(setf nsec (set-microsecond-part nsec 0))
(skip-microsecond (1- remainder)))
(setf nsec (set-microsecond-part
nsec (+ (microsecond-part nsec)
skip))))))))
(skip-nanosecond (skip)
(if (minusp skip)
(let ((remainder (+ (nanosecond-part nsec) skip)))
(if (minusp remainder)
(progn
(skip-second -1)
(setf nsec (set-nanosecond-part nsec 999))
(skip-nanosecond (1+ remainder)))
(setf nsec (set-nanosecond-part
nsec (+ (nanosecond-part nsec) skip)))))
(if (plusp skip)
(let ((remainder (- (+ (nanosecond-part nsec) skip) 999)))
(if (plusp remainder)
(progn
(skip-second 1)
(setf nsec (set-nanosecond-part nsec 0))
(skip-nanosecond (1- remainder)))
(setf nsec (set-nanosecond-part
nsec (+ (nanosecond-part nsec)
skip)))))))))
,@body))
(defun add-time (fixed-time duration &key (reverse nil))
"Given a FIXED-TIME, add the supplied DURATION.
Example (reader notation requires calling LOCAL-TIME:ENABLE-READ-MACROS):
(add-time @2007-05-20T12:10:10.000 (duration :hours 50))
=> @2007-05-22T14:10:10.000
NOTE: This function always adds the largest increments first, so:
(add-time @2003-01-09 (duration :years 1 :days 20)) => @2004-02-29
If days has been added before years, the result would have been
\"@2004-03-01\"."
(declare (type fixed-time fixed-time))
(declare (type duration duration))
(declare (type boolean reverse))
(if (and (zerop (duration-years duration))
(zerop (duration-months duration))
(zerop (duration-days duration))
(zerop (duration-hours duration))
(zerop (duration-minutes duration)))
(multiple-value-bind (quotient remainder)
(floor (funcall (if reverse #'- #'+)
(+ (* (timestamp-to-unix fixed-time) 1000000000)
(nsec-of fixed-time))
(+ (* (duration-seconds duration) 1000000000)
(* (duration-milliseconds duration) 1000000)
(* (duration-microseconds duration) 1000)
(duration-nanoseconds duration)))
1000000000)
(unix-to-timestamp quotient :nsec remainder))
(multiple-value-bind
(nsec ss mm hh day month year)
(decode-timestamp fixed-time)
(let ((identity (if reverse -1 1)))
(with-skippers
(if (duration-years duration)
(skip-year (* identity (duration-years duration))))
(if (duration-months duration)
(skip-month (* identity (duration-months duration))))
(if (duration-days duration)
(skip-day (* identity (duration-days duration))))
(if (duration-hours duration)
(skip-hour (* identity (duration-hours duration))))
(if (duration-minutes duration)
(skip-minute (* identity (duration-minutes duration))))
(if (duration-seconds duration)
(skip-second (* identity (duration-seconds duration))))
(if (duration-milliseconds duration)
(skip-millisecond (* identity (duration-milliseconds duration))))
(if (duration-microseconds duration)
(skip-microsecond (* identity (duration-microseconds duration))))
(if (duration-nanoseconds duration)
(skip-nanosecond (* identity (duration-nanoseconds duration))))))
(encode-timestamp nsec ss mm hh day month year))))
(declaim (inline subtract-time))
(defun subtract-time (fixed-time duration)
(add-time fixed-time duration :reverse t))
(defun bounded-add (left right bound)
"A bounded addition operator. Returns: VALUE CARRY."
(assert (< left bound))
(multiple-value-bind (quotient remainder)
(floor right bound)
(let ((sum (+ left remainder)))
(if (< sum bound)
(values sum quotient)
(values (- (+ left remainder) bound)
(+ 1 quotient))))))
(defun bounded-subtract (left right bound)
"A bounded subtraction operator. Returns: VALUE CARRY."
(assert (< left bound))
(multiple-value-bind (quotient remainder)
(floor right bound)
(if (>= left remainder)
(values (- left remainder) quotient)
(values (+ left (- bound remainder))
(+ 1 quotient)))))
(declaim (inline add-years subtract-years))
(defun add-years (duration years)
(incf (duration-years duration) years)
duration)
(defun subtract-years (duration years)
(decf (duration-years duration) years)
duration)
(declaim (inline add-months subtract-months))
(defun add-months (duration months)
(incf (duration-months duration) months)
duration)
(defun subtract-months (duration months)
(decf (duration-months duration) months)
duration)
(declaim (inline add-days subtract-days))
(defun add-days (duration days)
(incf (duration-days duration) days)
duration)
(defun subtract-days (duration days)
(decf (duration-days duration) days)
duration)
(declaim (inline add-hours subtract-hours))
(defun add-hours (duration hours)
(incf (duration-hours duration) hours)
duration)
(defun subtract-hours (duration hours)
(decf (duration-hours duration) hours)
duration)
(declaim (inline add-minutes subtract-minutes))
(defun add-minutes (duration minutes)
(incf (duration-minutes duration) minutes)
duration)
(defun subtract-minutes (duration minutes)
(decf (duration-minutes duration) minutes)
duration)
(declaim (inline add-seconds subtract-seconds))
(defun add-seconds (duration seconds)
(incf (duration-seconds duration) seconds)
duration)
(defun subtract-seconds (duration seconds)
(decf (duration-seconds duration) seconds)
duration)
(declaim (inline add-milliseconds subtract-milliseconds))
(defun add-milliseconds (duration milliseconds)
(incf (duration-milliseconds duration) milliseconds)
duration)
(defun subtract-milliseconds (duration milliseconds)
(decf (duration-milliseconds duration) milliseconds)
duration)
(declaim (inline add-microseconds subtract-microseconds))
(defun add-microseconds (duration microseconds)
(incf (duration-microseconds duration) microseconds)
duration)
(defun subtract-microseconds (duration microseconds)
(decf (duration-microseconds duration) microseconds)
duration)
(declaim (inline add-nanoseconds subtract-nanoseconds))
(defun add-nanoseconds (duration nanoseconds)
(incf (duration-nanoseconds duration) nanoseconds)
duration)
(defun subtract-nanoseconds (duration nanoseconds)
(decf (duration-nanoseconds duration) nanoseconds)
duration)
(defun time-difference (left right)
"Compute the duration existing between fixed-times LEFT and RIGHT.
The order of left or right is ignored; the returned DURATION, if added to
the earlier value, will result in the later.
A complexity of this process which might surprise some is that larger
quantities are added by ADD-TIME before smaller quantities. For example, what
is the difference between 2003-02-10 and 2004-03-01? If you add years before
days, the difference is 1 year and 20 days. If you were to add days before
years, however, the difference would be 1 year and 21 days. The question, do
you advance to 2004 and then calculate between 2-10 and 3-01, or do you move
from 2-10 to 3-01, and then increment the year? This library chooses to add
years before days, since this follows human reckoning a bit closer (i.e., a
person would likely flip to the 2004 calendar and then start counting off
days, rather than the other way around). This difference in reckoning can be
tricky, however, so bear this in mind."
(if (timestamp< left right)
(rotatef left right))
(let ((nsec (- (nsec-of left) (nsec-of right)))
(sec (- (timestamp-to-universal left) (timestamp-to-universal right))))
(if (minusp nsec)
(decf sec))
(duration :seconds sec :nanoseconds nsec)))
(declaim (inline add-duration))
(defun add-duration (left right)
"Add one duration to another."
(duration :years (+ (duration-years left)
(duration-years right))
:months (+ (duration-months left)
(duration-months right))
:days (+ (duration-days left)
(duration-days right))
:hours (+ (duration-hours left)
(duration-hours right))
:minutes (+ (duration-minutes left)
(duration-minutes right))
:seconds (+ (duration-seconds left)
(duration-seconds right))
:milliseconds (+ (duration-milliseconds left)
(duration-milliseconds right))
:microseconds (+ (duration-microseconds left)
(duration-microseconds right))
:nanoseconds (+ (duration-nanoseconds left)
(duration-nanoseconds right))))
(declaim (inline subtract-duration))
(defun subtract-duration (left right)
"Subtract one duration from another."
(duration :years (- (duration-years left)
(duration-years right))
:months (- (duration-months left)
(duration-months right))
:days (- (duration-days left)
(duration-days right))
:hours (- (duration-hours left)
(duration-hours right))
:minutes (- (duration-minutes left)
(duration-minutes right))
:seconds (- (duration-seconds left)
(duration-seconds right))
:milliseconds (- (duration-milliseconds left)
(duration-milliseconds right))
:microseconds (- (duration-microseconds left)
(duration-microseconds right))
:nanoseconds (- (duration-nanoseconds left)
(duration-nanoseconds right))))
(declaim (inline multiply-duration))
(defun multiply-duration (left multiplier)
"Add one duration to another."
(duration :years (* (duration-years left) multiplier)
:months (* (duration-months left) multiplier)
:days (* (duration-days left) multiplier)
:hours (* (duration-hours left) multiplier)
:minutes (* (duration-minutes left) multiplier)
:seconds (* (duration-seconds left) multiplier)
:milliseconds (* (duration-milliseconds left) multiplier)
:microseconds (* (duration-microseconds left) multiplier)
:nanoseconds (* (duration-nanoseconds left) multiplier)))
(declaim (inline time-stepper))
(defun time-stepper (duration &key (reverse nil))
(declare (type duration duration))
(declare (type boolean reverse))
(lambda (time)
(add-time time duration :reverse reverse)))
(declaim (inline time-generator))
(defun time-generator (start duration &key (reverse nil))
(declare (type fixed-time start))
(declare (type duration duration))
(declare (type boolean reverse))
(let (next)
(lambda ()
(setf next (add-time (or next start) duration
:reverse reverse)))))
(defmacro loop-times (forms start duration end
&key (reverse nil) (inclusive-p nil))
"Map over a set of times separated by DURATION, calling CALLABLE with the
start of each."
(let ((generator-sym (gensym))
(start-sym (gensym))
(end-sym (gensym)))
`(let ((,start-sym ,start)
(,end-sym ,end))
(assert (,(if reverse
'timestamp>
'timestamp<) ,start-sym ,end-sym))
(loop
with ,generator-sym = (time-generator ,start-sym ,duration)
for value = (funcall ,generator-sym)
while ,(if reverse
(if inclusive-p
`(timestamp>= value ,end-sym)
`(timestamp> value ,end-sym))
(if inclusive-p
`(timestamp<= value ,end-sym)
`(timestamp< value ,end-sym)))
,@forms))))
(defmacro map-times (callable start duration end
&key (reverse nil) (inclusive-p nil))
"Map over a set of times separated by DURATION, calling CALLABLE with the
start of each."
`(loop-times (do (funcall ,callable value))
,start ,duration ,end :reverse ,reverse
:inclusive-p ,inclusive-p))
(defmacro list-times (start duration end
&key (reverse nil) (inclusive-p nil))
"Return a list of all times within the given range."
`(loop-times (collect value)
,start ,duration ,end :reverse ,reverse
:inclusive-p ,inclusive-p))
(defmacro do-times ((var start duration end &optional (result nil))
&body body)
"A 'do' style version of the functional MAP-TIMES macro.
The disadvantage to DO-TIMES is that there is no way to ask for a reversed
time sequence, or specify an inclusive endpoint."
`(block nil
(map-times #'(lambda (,var) ,@body) ,start ,duration ,end)
,result))
;;;_ * RELATIVE-TIME
(defstruct relative-time
(year nil :type (or keyword integer null))
(month nil :type (or keyword integer null))
(week nil :type (or keyword integer null))
(day-of-week nil :type (or keyword integer null))
(day nil :type (or keyword integer null))
(hour nil :type (or keyword integer null))
(minute nil :type (or keyword integer null))
(second nil :type (or keyword integer null))
(millisecond nil :type (or keyword integer null))
(microsecond nil :type (or keyword integer null))
(nanosecond nil :type (or keyword integer null)))
(declaim (inline relative-time))
(defun relative-time (&rest args)
(apply #'make-relative-time args))
(declaim (inline range-dec))
(defun range-dec (value min max)
(if (= value min)
max
(1- value)))
(declaim (inline range-inc))
(defun range-inc (value min max)
(if (= value max)
min
(1+ value)))
(defun enclosing-duration (relative-time)
"Return a DURATION which, if applied to a time, causes NEXT-TIME to move to
the next matching occurrence of that time pattern.
For example, if you ask for ':day 18' on Nov 18, it will return the same
time back to you. If you add enclosing duration for that relative time to Nov
18 and then ask again, you'll get Dec 18."
(cond
((relative-time-month relative-time)
(duration :months 1))
((relative-time-day relative-time)
(duration :days 1))
((relative-time-hour relative-time)
(duration :hours 1))
((relative-time-minute relative-time)
(duration :minutes 1))
((relative-time-second relative-time)
(duration :seconds 1))
((relative-time-millisecond relative-time)
(duration :milliseconds 1))
((relative-time-microsecond relative-time)
(duration :microseconds 1))
((relative-time-nanosecond relative-time)
(duration :nanoseconds 1))
((relative-time-day-of-week relative-time)
(duration :days 1))
(t
(error "`enclosing-duration' has failed."))))
(defun details-match-relative-time-p (relative-time nsec ss mm hh day month year
day-of-week daylight-p
timezone tz-abbrev)
(declare (ignore daylight-p))
(declare (ignore timezone))
(declare (ignore tz-abbrev))
"Return T if the given time elements honor the details in RELATIVE-TIME."
(and (or (not (relative-time-nanosecond relative-time))
(= (nanosecond-part nsec)
(relative-time-nanosecond relative-time)))
(or (not (relative-time-microsecond relative-time))
(= (microsecond-part nsec)
(relative-time-microsecond relative-time)))
(or (not (relative-time-millisecond relative-time))
(= (millisecond-part nsec)
(relative-time-millisecond relative-time)))
(or (not (relative-time-second relative-time))
(= ss (relative-time-second relative-time)))
(or (not (relative-time-minute relative-time))
(= mm (relative-time-minute relative-time)))
(or (not (relative-time-hour relative-time))
(= hh (relative-time-hour relative-time)))
(or (not (relative-time-day relative-time))
(= day (relative-time-day relative-time)))
(or (not (relative-time-month relative-time))
(= month (relative-time-month relative-time)))
(or (not (relative-time-year relative-time))
(= year (relative-time-year relative-time)))
(or (not (relative-time-day-of-week relative-time))
(= day-of-week (relative-time-day-of-week relative-time)))))
(declaim (inline matches-relative-time-p))
(defun matches-relative-time-p (fixed-time relative-time)
"Return T if the given FIXED-TIME honors the details in RELATIVE-TIME."
(apply #'details-match-relative-time-p
relative-time (multiple-value-list (decode-timestamp fixed-time))))
;; jww (2007-11-18): The following bug occurs:
;; (next-time @2008-04-01 (relative-time :month 2 :day 29))
;; => @2009-03-29T00:33:08.004
;; jww (2007-11-22): This function fails to compile under CMUCL, although it
;; does work under SBCL and LispWorks. I get this:
;;
;; Error in function LISP::ASSERT-ERROR:
;; The assertion (MEMBER C::KIND '(:OPTIONAL :CLEANUP :ESCAPE)) failed.
;; [Condition of type SIMPLE-ERROR]
(defun next-time (anchor relative-time
&key (reverse nil) (accept-anchor nil) (recursive-call nil))
"Compute the first time after FIXED-TIME which matches RELATIVE-TIME.
This function finds the first moment after FIXED-TIME which honors every
element in RELATIVE-TIME:
(next-time @2007-05-20 (relative-time :month 3)) => @2008-03-20
The relative time constructor arguments may also be symbolic:
(relative-time :month :this)
(relative-time :month :next)
(relative-time :month :prev)
To find the date two weeks after next February, a combination of NEXT-TIME
and ADD-TIME must be used, since \"next February\" is a relative time concept,
while \"two weeks\" is a duration concept:
(add-time (next-time @2007-05-20 (relative-time :month 2))
(duration :days 14))
NOTE: The keyword arguments to RELATIVE-TIME are always singular; those to
DURATION are always plural.
The following form resolves to the first sunday of the given year:
(next-time (previous-time @2007-05-20
(relative-time :month 1 :day 1))
(relative-time :week-day 0))
This form finds the first Friday the 13th after today:
(next-time @2007-05-20 (relative-time :day 13 :day-of-week 5))
NOTE: When adding times, NEXT-TIME always seeks the next time that fully
honors your request. If asked for Feb 29, the year of the resulting time will
fall in a leap year. If asked for Thu, Apr 29, it returns the next occurrence
of Apr 29 which falls on a Friday. Example:
(next-time @2007-11-01
(relative-time :month 4 :day 29 :day-of-week 4))
=> @2010-04-29T00:00:00.000"
(declare (type (or fixed-time null) anchor))
(declare (type relative-time relative-time))
(declare (type boolean reverse))
(let ((moment (or anchor (local-time:now))))
(multiple-value-bind
(nsec ss mm hh day month year day-of-week)
(decode-timestamp moment)
;; If the moment we just decoded already matches the relative-time,
;; either return it immediately (if :ACCEPT-ANCHOR is T), or else
;; recurse exactly one level to get the next relative time.
(if (and (not recursive-call)
(details-match-relative-time-p relative-time
nsec ss mm hh day month year
day-of-week nil nil nil))
(return-from next-time
(if accept-anchor
moment
(next-time (add-time moment
(enclosing-duration relative-time)
:reverse reverse)
relative-time
:reverse reverse
:recursive-call t))))
(let ((identity (if reverse -1 1))
(test (if reverse #'> #'<))
now-nsec now-ss now-mm now-hh
now-day now-month now-year now-day-of-week)
(labels
((decode-now ()
(if anchor
(multiple-value-setq
(now-nsec now-ss now-mm now-hh
now-day now-month
now-year now-day-of-week)
(decode-timestamp (local-time:now)))
(setf now-nsec nsec
now-ss ss
now-mm mm
now-hh hh
now-day day
now-month month
now-year year)))