1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
6 ;;;; A variety of structures and function for creating and
7 ;;;; manipulating dates, times, durations and intervals for
10 ;;;; This file was originally part of ODCL and is Copyright (c) 2002 -
11 ;;;; 2003 onShore Development, Inc.
13 ;;;; CLSQL users are granted the rights to distribute and use this software
14 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
15 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
16 ;;;; *************************************************************************
18 (in-package #:clsql-sys)
20 ;; ------------------------------------------------------------
23 (defvar *month-keywords*
24 '(:january :february :march :april :may :june :july :august :september
25 :october :november :december))
28 '("" "January" "February" "March" "April" "May" "June" "July" "August"
29 "September" "October" "November" "December"))
31 (defun month-name (month-index)
32 (nth month-index *month-names*))
34 (defun ordinal-month (month-keyword)
35 "Return the zero-based month number for the given MONTH keyword."
36 (position month-keyword *month-keywords*))
39 ;; ------------------------------------------------------------
42 (defvar *day-keywords*
43 '(:sunday :monday :tuesday :wednesday :thursday :friday :saturday))
46 '("Sunday" "Monday" "Tuesday" "Wednesday" "Thursday" "Friday" "Saturday"))
48 (defun day-name (day-index)
49 (nth day-index *day-names*))
51 (defun ordinal-day (day-keyword)
52 "Return the zero-based day number for the given DAY keyword."
53 (position day-keyword *day-keywords*))
56 ;; ------------------------------------------------------------
57 ;; time classes: wall-time, duration
59 (eval-when (:compile-toplevel :load-toplevel)
61 (defstruct (wall-time (:conc-name time-)
62 (:constructor %make-wall-time)
63 (:print-function %print-wall-time))
65 (second 0 :type fixnum)
66 (usec 0 :type fixnum))
68 (defun %print-wall-time (time stream depth)
69 (declare (ignore depth))
70 (format stream "#<WALL-TIME: ~a>" (format-time nil time)))
72 (defstruct (duration (:constructor %make-duration)
73 (:print-function %print-duration))
75 (month 0 :type fixnum)
78 (second 0 :type fixnum)
79 (minute 0 :type fixnum)
80 (usec 0 :type fixnum))
82 (defun %print-duration (duration stream depth)
83 (declare (ignore depth))
84 (format stream "#<DURATION: ~a>"
85 (format-duration nil duration :precision :second)))
89 (defun duration-timestring (duration)
90 (let ((second (duration-second duration))
91 (minute (duration-minute duration))
92 (hour (duration-hour duration))
93 (day (duration-day duration)))
94 (format nil "P~dD~dH~dM~dS" day hour minute second)))
97 ;; ------------------------------------------------------------
100 (defun make-time (&key (year 0) (month 1) (day 1) (hour 0) (minute 0)
101 (second 0) (usec 0) (offset 0))
102 (let ((mjd (gregorian-to-mjd month day year))
103 (sec (+ (* hour 60 60)
106 (multiple-value-bind (day-add raw-sec)
107 (floor sec (* 60 60 24))
108 (%make-wall-time :mjd (+ mjd day-add) :second raw-sec :usec usec))))
110 (defun copy-time (time)
111 (%make-wall-time :mjd (time-mjd time)
112 :second (time-second time)))
114 (defun utime->time (utime)
115 "Return a pair: (GREGORIAN DAY . TIME-OF-DAY)"
116 (multiple-value-bind (second minute hour day mon year)
117 (decode-universal-time utime)
118 (make-time :year year :month mon :day day :hour hour :minute minute
122 "Return a pair: (GREGORIAN DAY . TIME-OF-DAY)"
123 (utime->time (get-universal-time)))
125 (defun make-duration (&key (year 0) (month 0) (day 0) (hour 0) (minute 0)
127 (multiple-value-bind (second-add usec-1000000)
129 (multiple-value-bind (minute-add second-60)
130 (floor (+ second second-add) 60)
131 (multiple-value-bind (hour-add minute-60)
132 (floor (+ minute minute-add) 60)
133 (multiple-value-bind (day-add hour-24)
134 (floor (+ hour hour-add) 24)
135 (%make-duration :year year :month month :day (+ day day-add)
139 :usec usec-1000000))))))
142 ;; ------------------------------------------------------------
145 (defun time-hms (time)
146 (multiple-value-bind (hourminute second)
147 (floor (time-second time) 60)
148 (multiple-value-bind (hour minute)
149 (floor hourminute 60)
150 (values hour minute second))))
152 (defun time-ymd (time)
153 (destructuring-bind (month day year)
154 (mjd-to-gregorian (time-mjd time))
155 (values year month day)))
157 (defun time-dow (time)
158 "Return the 0 indexed Day of the week starting with Sunday"
159 (mod (+ 3 (time-mjd time)) 7))
161 (defun decode-time (time)
162 "returns the decoded time as multiple values: usec, second, minute, hour,
163 day, month, year, integer day-of-week"
164 (multiple-value-bind (year month day)
166 (multiple-value-bind (hour minute second)
168 (values (time-usec time) second minute hour day month year (mod (+ (time-mjd time) 3) 7)))))
171 (defun duration-reduce (duration precision &optional round)
174 (+ (duration-usec duration)
175 (* (duration-reduce duration :second) 1000000)))
178 (floor (duration-usec duration) 500000)
180 (duration-second duration)
181 (* (duration-reduce duration :minute) 60)))
184 (floor (duration-second duration) 30)
186 (duration-minute duration)
187 (* (duration-reduce duration :hour) 60)))
190 (floor (duration-minute duration) 30)
192 (duration-hour duration)
193 (* (duration-reduce duration :day) 24)))
196 (floor (duration-hour duration) 12)
198 (duration-day duration)))))
201 ;; ------------------------------------------------------------
202 ;; Arithemetic and comparators
204 (defun duration= (duration-a duration-b)
205 (= (duration-reduce duration-a :usec)
206 (duration-reduce duration-b :usec)))
208 (defun duration< (duration-a duration-b)
209 (< (duration-reduce duration-a :usec)
210 (duration-reduce duration-b :usec)))
212 (defun duration<= (duration-a duration-b)
213 (<= (duration-reduce duration-a :usec)
214 (duration-reduce duration-b :usec)))
216 (defun duration>= (x y)
219 (defun duration> (x y)
223 (let ((mjd-x (time-mjd x))
224 (mjd-y (time-mjd y)))
227 (if (/= (time-second x) (time-second y))
228 (< (time-second x) (time-second y))
229 (< (time-usec x) (time-usec y))))))
232 (if (/= (time-mjd x) (time-mjd y))
233 (>= (time-mjd x) (time-mjd y))
234 (if (/= (time-second x) (time-second y))
235 (>= (time-second x) (time-second y))
236 (>= (time-usec x) (time-usec y)))))
239 (if (/= (time-mjd x) (time-mjd y))
240 (<= (time-mjd x) (time-mjd y))
241 (if (/= (time-second x) (time-second y))
242 (<= (time-second x) (time-second y))
243 (<= (time-usec x) (time-usec y)))))
246 (if (/= (time-mjd x) (time-mjd y))
247 (> (time-mjd x) (time-mjd y))
248 (if (/= (time-second x) (time-second y))
249 (> (time-second x) (time-second y))
250 (> (time-usec x) (time-usec y)))))
253 (and (= (time-mjd x) (time-mjd y))
254 (= (time-second x) (time-second y))
255 (= (time-usec x) (time-usec y))))
257 (defun time= (number &rest more-numbers)
258 "Returns T if all of its arguments are numerically equal, NIL otherwise."
259 (do ((nlist more-numbers (cdr nlist)))
261 (declare (list nlist))
262 (if (not (%time= (car nlist) number)) (return nil))))
264 (defun time/= (number &rest more-numbers)
265 "Returns T if no two of its arguments are numerically equal, NIL otherwise."
266 (do* ((head number (car nlist))
267 (nlist more-numbers (cdr nlist)))
269 (declare (list nlist))
270 (unless (do* ((nl nlist (cdr nl)))
273 (if (%time= head (car nl)) (return nil)))
276 (defun time< (number &rest more-numbers)
277 "Returns T if its arguments are in strictly increasing order, NIL otherwise."
278 (do* ((n number (car nlist))
279 (nlist more-numbers (cdr nlist)))
281 (declare (list nlist))
282 (if (not (%time< n (car nlist))) (return nil))))
284 (defun time> (number &rest more-numbers)
285 "Returns T if its arguments are in strictly decreasing order, NIL otherwise."
286 (do* ((n number (car nlist))
287 (nlist more-numbers (cdr nlist)))
289 (declare (list nlist))
290 (if (not (%time> n (car nlist))) (return nil))))
292 (defun time<= (number &rest more-numbers)
293 "Returns T if arguments are in strictly non-decreasing order, NIL otherwise."
294 (do* ((n number (car nlist))
295 (nlist more-numbers (cdr nlist)))
297 (declare (list nlist))
298 (if (not (%time<= n (car nlist))) (return nil))))
300 (defun time>= (number &rest more-numbers)
301 "Returns T if arguments are in strictly non-increasing order, NIL otherwise."
302 (do* ((n number (car nlist))
303 (nlist more-numbers (cdr nlist)))
305 (declare (list nlist))
306 (if (not (%time>= n (car nlist))) (return nil))))
308 (defun time-max (number &rest more-numbers)
309 "Returns the greatest of its arguments."
310 (do ((nlist more-numbers (cdr nlist))
312 ((null nlist) (return result))
313 (declare (list nlist))
314 (if (%time> (car nlist) result) (setf result (car nlist)))))
316 (defun time-min (number &rest more-numbers)
317 "Returns the least of its arguments."
318 (do ((nlist more-numbers (cdr nlist))
320 ((null nlist) (return result))
321 (declare (list nlist))
322 (if (%time< (car nlist) result) (setf result (car nlist)))))
324 (defun time-compare (time-a time-b)
325 (let ((mjd-a (time-mjd time-a))
326 (mjd-b (time-mjd time-b))
327 (sec-a (time-second time-a))
328 (sec-b (time-second time-b))
329 (usec-a (time-usec time-a))
330 (usec-b (time-usec time-b)))
333 (if (= usec-a usec-b)
335 (if (< usec-a usec-b)
346 ;; ------------------------------------------------------------
347 ;; Formatting and output
349 (defvar +decimal-printer+ #(#\0 #\1 #\2 #\3 #\4 #\5 #\6 #\7 #\8 #\9))
351 (defun db-timestring (time)
352 "return the string to store the given time in the database"
353 (declare (optimize (speed 3)))
354 (let ((output (copy-seq "'XXXX-XX-XX XX:XX:XX.")))
355 (flet ((inscribe-base-10 (output offset size decimal)
356 (declare (type fixnum offset size decimal)
357 (type (simple-vector 10) +decimal-printer+))
359 (declare (type fixnum x)
360 (optimize (safety 0)))
361 (multiple-value-bind (next this)
363 (setf (aref output (+ (- size x 1) offset))
364 (aref +decimal-printer+ this))
365 (setf decimal next)))))
366 (multiple-value-bind (usec second minute hour day month year)
368 (inscribe-base-10 output 1 4 year)
369 (inscribe-base-10 output 6 2 month)
370 (inscribe-base-10 output 9 2 day)
371 (inscribe-base-10 output 12 2 hour)
372 (inscribe-base-10 output 15 2 minute)
373 (inscribe-base-10 output 18 2 second)
374 (format nil "~a~d'" output usec)))))
376 (defun iso-timestring (time)
377 "return the string to store the given time in the database"
378 (declare (optimize (speed 3)))
379 (let ((output (copy-seq "XXXX-XX-XX XX:XX:XX,")))
380 (flet ((inscribe-base-10 (output offset size decimal)
381 (declare (type fixnum offset size decimal)
382 (type (simple-vector 10) +decimal-printer+))
384 (declare (type fixnum x)
385 (optimize (safety 0)))
386 (multiple-value-bind (next this)
388 (setf (aref output (+ (- size x 1) offset))
389 (aref +decimal-printer+ this))
390 (setf decimal next)))))
391 (multiple-value-bind (usec second minute hour day month year)
393 (inscribe-base-10 output 0 4 year)
394 (inscribe-base-10 output 5 2 month)
395 (inscribe-base-10 output 8 2 day)
396 (inscribe-base-10 output 11 2 hour)
397 (inscribe-base-10 output 14 2 minute)
398 (inscribe-base-10 output 17 2 second)
399 (format nil "~a,~d" output usec)))))
402 ;; ------------------------------------------------------------
413 ;; fix : should also return :contains / :contained
415 (defun interval-relation (x y)
416 "Compare the relationship of node x to node y. Returns either
417 :contained :contains :follows :overlaps or :precedes."
418 (let ((xst (interval-start x))
419 (xend (interval-end x))
420 (yst (interval-start y))
421 (yend (interval-end y)))
422 (case (time-compare xst yst)
424 (case (time-compare xend yend)
427 ((:equal :greater-than)
430 (case (time-compare xst yend)
431 ((:equal :greater-than)
434 (case (time-compare xend yend)
440 (case (time-compare xend yst)
444 (case (time-compare xend yend)
447 ((:equal :greater-than)
450 ;; ------------------------------------------------------------
453 (defun sort-interval-list (list)
454 (sort list (lambda (x y)
455 (case (interval-relation x y)
456 ((:precedes :contains) t)
457 ((:follows :overlaps :contained) nil)))))
459 ;; interval push will return its list of intervals in strict order.
460 (defun interval-push (interval-list interval &optional container-rule)
461 (declare (ignore container-rule))
462 (let ((sorted-list (sort-interval-list interval-list)))
463 (dotimes (x (length sorted-list))
464 (let ((elt (nth x sorted-list)))
465 (case (interval-relation elt interval)
467 (return-from interval-push (insert-at-index x sorted-list interval)))
469 (return-from interval-push
470 (replace-at-index x sorted-list
471 (make-interval :start (interval-start elt)
472 :end (interval-end elt)
473 :type (interval-type elt)
474 :contained (interval-push (interval-contained elt) interval)
475 :data (interval-data elt)))))
476 ((:overlaps :contained)
477 (error "Overlap")))))
478 (append sorted-list (list interval))))
482 (defun interval-match (list time)
483 "Return the index of the first interval in list containing time"
484 ;; this depends on ordering of intervals!
485 (let ((list (sort-interval-list list)))
486 (dotimes (x (length list))
487 (let ((elt (nth x list)))
488 (when (and (time<= (interval-start elt) time)
489 (time< time (interval-end elt)))
490 (return-from interval-match x))))))
492 (defun interval-clear (list time)
493 (dotimes (x (length list))
494 (let ((elt (nth x list)))
495 (when (and (time<= (interval-start elt) time)
496 (time< time (interval-end elt)))
497 (if (interval-match (interval-contained elt) time)
498 (return-from interval-clear
499 (replace-at-index x list
500 (make-interval :start (interval-start elt)
501 :end (interval-end elt)
502 :type (interval-type elt)
503 :contained (interval-clear (interval-contained elt) time)
504 :data (interval-data elt))))
505 (return-from interval-clear
506 (delete-at-index x list)))))))
508 (defun interval-edit (list time start end &optional tag)
509 "Attempts to modify the most deeply nested interval in list which
510 begins at time. If no changes are made, returns nil."
511 ;; function required sorted interval list
512 (let ((list (sort-interval-list list)))
514 (dotimes (x (length list))
515 (let ((elt (nth x list)))
516 (when (and (time<= (interval-start elt) time)
517 (time< time (interval-end elt)))
518 (or (interval-edit (interval-contained elt) time start end tag)
520 (time< start (interval-end (nth (1- x) list))))
521 (error "Overlap of previous interval"))
522 ((and (< x (1- (length list)))
523 (time< (interval-start (nth (1+ x) list)) end))
524 (error "~S ~S ~S ~S Overlap of next interval" x (length list) (interval-start (nth (1+ x) list)) end ))
525 ((time= (interval-start elt) time)
526 (return-from interval-edit
527 (replace-at-index x list
528 (make-interval :start start
530 :type (interval-type elt)
531 :contained (restrict-intervals (interval-contained elt) start end)
532 :data (or tag (interval-data elt))))))))))))))
534 (defun restrict-intervals (list start end &aux newlist)
535 (let ((test-interval (make-interval :start start :end end)))
537 (when (equal :contained
538 (interval-relation elt test-interval))
542 ;;; utils from odcl/list.lisp
544 (defun replace-at-index (idx list elt)
546 (cons elt (cdr list)))
547 ((= idx (1- (length list)))
548 (append (butlast list) (list elt)))
550 (append (subseq list 0 idx)
552 (subseq list (1+ idx))))))
554 (defun insert-at-index (idx list elt)
557 ((= idx (1- (length list)))
558 (append list (list elt)))
560 (append (subseq list 0 idx)
562 (subseq list idx)))))
564 (defun delete-at-index (idx list)
567 ((= idx (1- (length list)))
570 (append (subseq list 0 idx)
571 (subseq list (1+ idx))))))
574 ;; ------------------------------------------------------------
575 ;; return MJD for Gregorian date
577 (defun gregorian-to-mjd (month day year)
580 (year-adj (if (< year 0)
588 (unless (or (< year 1582)
593 (let ((a (floor (/ year-adj 100))))
594 (setf b (+ (- 2 a) (floor (/ a 4))))))
596 (setf c (floor (- (* 365.25d0 year-adj) 679006.75d0)))
597 (setf c (floor (- (* 365.25d0 year-adj) 679006d0))))
598 (setf d (floor (* 30.6001 (+ 1 month-adj))))
599 ;; (cmsg "b ~s c ~s d ~s day ~s" b c d day)
602 ;; convert MJD to Gregorian date
604 (defun mjd-to-gregorian (mjd)
605 (let (z r g a b c year month day)
606 (setf z (floor (+ mjd 678882)))
607 (setf r (- (+ mjd 678882) z))
609 (setf a (floor (/ g 36524.25)))
610 (setf b (- a (floor (/ a 4))))
611 (setf year (floor (/ (+ b g) 365.25)))
612 (setf c (- (+ b z) (floor (* 365.25 year))))
613 (setf month (truncate (/ (+ (* 5 c) 456) 153)))
614 (setf day (+ (- c (truncate (/ (- (* 153 month) 457) 5))) r))
618 (list month day year)))
620 (defun duration+ (time &rest durations)
621 "Add each DURATION to TIME, returning a new wall-time value."
622 (let ((year (duration-year time))
623 (month (duration-month time))
624 (day (duration-day time))
625 (hour (duration-hour time))
626 (minute (duration-minute time))
627 (second (duration-second time))
628 (usec (duration-usec time)))
629 (dolist (duration durations)
630 (incf year (duration-year duration))
631 (incf month (duration-month duration))
632 (incf day (duration-day duration))
633 (incf hour (duration-hour duration))
634 (incf minute (duration-minute duration))
635 (incf second (duration-second duration))
636 (incf usec (duration-usec duration)))
637 (make-duration :year year :month month :day day :hour hour :minute minute
638 :second second :usec usec)))
640 (defun duration- (duration &rest durations)
641 "Subtract each DURATION from TIME, returning a new duration value."
642 (let ((year (duration-year duration))
643 (month (duration-month duration))
644 (day (duration-day duration))
645 (hour (duration-hour duration))
646 (minute (duration-minute duration))
647 (second (duration-second duration))
648 (usec (duration-usec duration)))
649 (dolist (duration durations)
650 (decf year (duration-year duration))
651 (decf month (duration-month duration))
652 (decf day (duration-day duration))
653 (decf hour (duration-hour duration))
654 (decf minute (duration-minute duration))
655 (decf second (duration-second duration))
656 (decf usec (duration-usec duration)))
657 (make-duration :year year :month month :day day :hour hour :minute minute
658 :second second :usec usec)))
662 (defun time+ (time &rest durations)
663 "Add each DURATION to TIME, returning a new wall-time value."
664 (let ((new-time (copy-time time)))
665 (dolist (duration durations)
667 :year (duration-year duration)
668 :month (duration-month duration)
669 :day (duration-day duration)
670 :hour (duration-hour duration)
671 :minute (duration-minute duration)
672 :second (duration-second duration)
673 :usec (duration-usec duration)
677 (defun time- (time &rest durations)
678 "Subtract each DURATION from TIME, returning a new wall-time value."
679 (let ((new-time (copy-time time)))
680 (dolist (duration durations)
682 :year (- (duration-year duration))
683 :month (- (duration-month duration))
684 :day (- (duration-day duration))
685 :hour (- (duration-hour duration))
686 :minute (- (duration-minute duration))
687 :second (- (duration-second duration))
688 :usec (- (duration-usec duration))
692 (defun time-difference (time1 time2)
693 "Returns a DURATION representing the difference between TIME1 and
695 (flet ((do-diff (time1 time2)
697 (let (day-diff sec-diff)
698 (setf day-diff (- (time-mjd time2)
701 (progn (decf day-diff)
702 (setf sec-diff (+ (time-second time2)
704 (time-second time1)))))
705 (setf sec-diff (- (time-second time2)
706 (time-second time1))))
707 (make-duration :day day-diff
709 (if (time< time1 time2)
710 (do-diff time1 time2)
711 (do-diff time2 time1))))
713 (defun format-time (stream time &key format
716 (internal-separator " "))
717 "produces on stream the timestring corresponding to the wall-time
718 with the given options"
719 (let ((*print-circle* nil))
720 (multiple-value-bind (usec second minute hour day month year dow)
724 (format stream "~A ~A, ~A ~D, ~D"
725 (pretty-time hour minute)
731 (format stream "~A, ~D/~D/~D"
732 (pretty-time hour minute)
735 (let ((string (iso-timestring time)))
737 (write-string string stream)
740 (format stream "~2,'0D~A~2,'0D~A~2,'0D~A~2,'0D~A~2,'0D~A~2,'0D.~6,'0D"
741 year date-separator month date-separator day
742 internal-separator hour time-separator minute time-separator
745 (defun pretty-time (hour minute)
748 (format nil "12:~2,'0D AM" minute))
750 (format nil "12:~2,'0D PM" minute))
752 (format nil "~D:~2,'0D AM" hour minute))
753 ((and (> hour 12) (< hour 24))
754 (format nil "~D:~2,'0D PM" (- hour 12) minute))
756 (error "pretty-time got bad hour"))))
758 (defun leap-days-in-days (days)
759 ;; return the number of leap days between Mar 1 2000 and
760 ;; (Mar 1 2000) + days, where days can be negative
762 (ceiling (/ (- days) (* 365 4)))
763 (floor (/ days (* 365 4)))))
765 (defun current-year ()
766 (third (mjd-to-gregorian (time-mjd (get-time)))))
768 (defun current-month ()
769 (second (mjd-to-gregorian (time-mjd (get-time)))))
771 (defun current-day ()
772 (first (mjd-to-gregorian (time-mjd (get-time)))))
774 (defun parse-date-time (string)
775 "parses date like 08/08/01, 8.8.2001, eg"
776 (when (> (length string) 1)
777 (let ((m (current-month))
780 (let ((integers (mapcar #'parse-integer (hork-integers string))))
781 (case (length integers)
783 (setf y (car integers)))
785 (setf m (car integers))
786 (setf y (cadr integers)))
788 (setf m (car integers))
789 (setf d (cadr integers))
790 (setf y (caddr integers)))
792 (return-from parse-date-time))))
795 (make-time :year y :month m :day d))))
797 (defun hork-integers (input)
800 (dotimes (x (length input))
801 (unless (<= 48 (char-code (aref input x)) 57)
802 (push (subseq input start x) output)
803 (setf start (1+ x))))
804 (nreverse (push (subseq input start) output))))
806 (defun merged-time (day time-of-day)
807 (%make-wall-time :mjd (time-mjd day)
808 :second (time-second time-of-day)))
810 (defun time-meridian (hours)
816 (values (- hours 12) "PM"))
818 (values hours "AM"))))
820 (defgeneric to-string (val &rest keys)
823 (defmethod to-string ((time wall-time) &rest keys)
824 (destructuring-bind (&key (style :daytime) &allow-other-keys)
826 (print-date time style)))
828 (defun print-date (time &optional (style :daytime))
829 (multiple-value-bind (second minute hour day month year dow)
831 (declare (ignore second))
832 (multiple-value-bind (hours meridian)
837 (format nil "~d:~2,'0d ~a" hours minute meridian))
839 ;; October 11th, 2000
840 (format nil "~a ~d, ~d" (month-name month) day year))
846 (format nil "~a ~d" (month-name month) year))
848 ;; 11:08 AM, November 22, 2002
849 (format nil "~d:~2,'0d ~a, ~a ~d, ~d"
850 hours minute meridian (month-name month) day year))
852 ;; 11:09 AM Friday, November 22, 2002
853 (format nil "~d:~2,'0d ~a ~a, ~a ~d, ~d"
854 hours minute meridian (nth dow *day-names*)
855 (month-name month) day year))
857 ;; 11:09 AM, 11/22/2002
858 (format-time nil time :format :short-pretty))
861 (format nil "~d/~d/~d" month day year))))))
863 (defun time-element (time element)
864 (multiple-value-bind (second minute hour day month year dow)
875 (:integer-day-of-week
878 (nth dow *day-keywords*))
884 (defun format-duration (stream duration &key (precision :minute))
885 (let ((second (duration-second duration))
886 (minute (duration-minute duration))
887 (hour (duration-hour duration))
888 (day (duration-day duration))
889 (return (null stream))
890 (stream (or stream (make-string-output-stream))))
893 (setf hour 0 second 0 minute 0))
895 (setf second 0 minute 0))
900 (if (= 0 day hour minute)
901 (format stream "0 minutes")
904 (format stream "~d day~p" day day)
908 (write-char #\Space stream))
909 (format stream "~d hour~p" hour hour)
913 (write-char #\Space stream))
914 (format stream "~d min~p" minute minute)
918 (write-char #\Space stream))
919 (format stream "~d sec~p" second second))))
921 (get-output-stream-string stream))))
923 (defgeneric midnight (self))
924 (defmethod midnight ((self wall-time))
925 "truncate hours, minutes and seconds"
926 (%make-wall-time :mjd (time-mjd self)))
928 (defun roll (date &key (year 0) (month 0) (day 0) (second 0) (hour 0)
929 (minute 0) (usec 0) (destructive nil))
930 (unless (= 0 year month)
931 (multiple-value-bind (year-orig month-orig day-orig)
933 (setf date (make-time :year (+ year year-orig)
934 :month (+ month month-orig)
936 :second (time-second date)
938 (let ((mjd (time-mjd date))
939 (sec (time-second date))
940 (usec (time-usec date)))
941 (multiple-value-bind (usec-new sec)
948 (multiple-value-bind (mjd-new sec-new)
949 (floor sec (* 60 60 24))
952 (setf (time-mjd date) (+ mjd mjd-new day)
953 (time-second date) sec-new
954 (time-usec date) usec-new)
956 (%make-wall-time :mjd (+ mjd mjd-new day)
960 (defun roll-to (date size position)
966 (- (time-element date :day-of-month)))))
968 (roll date :day (+ (days-in-month (time-element date :month)
969 (time-element date :year))
970 (- (time-element date :day-of-month)))))))))
972 (defun week-containing (time)
973 (let* ((midn (midnight time))
974 (dow (time-element midn :integer-day-of-week)))
975 (list (roll midn :day (- dow))
976 (roll midn :day (- 7 dow)))))
978 (defun leap-year? (year)
979 "t if YEAR is a leap yeap in the Gregorian calendar"
980 (and (= 0 (mod year 4))
981 (or (not (= 0 (mod year 100)))
982 (= 0 (mod year 400)))))
984 (defun valid-month-p (month)
985 "t if MONTH exists in the Gregorian calendar"
988 (defun valid-gregorian-date-p (date)
989 "t if DATE (year month day) exists in the Gregorian calendar"
990 (let ((max-day (days-in-month (nth 1 date) (nth 0 date))))
991 (<= 1 (nth 2 date) max-day)))
993 (defun days-in-month (month year &key (careful t))
994 "the number of days in MONTH of YEAR, observing Gregorian leap year
996 (declare (type fixnum month year))
998 (check-type month (satisfies valid-month-p)
999 "between 1 (January) and 12 (December)"))
1000 (if (eql month 2) ; feb
1001 (if (leap-year? year)
1003 (let ((even (mod (1- month) 2)))
1004 (if (< month 8) ; aug
1008 (defun day-of-year (year month day &key (careful t))
1009 "the day number within the year of the date DATE. For example,
1011 (declare (type fixnum year month day))
1013 (let ((date (list year month day)))
1014 (check-type date (satisfies valid-gregorian-date-p)
1015 "a valid Gregorian date")))
1016 (let ((doy (+ day (* 31 (1- month)))))
1017 (declare (type fixnum doy))
1019 (setf doy (- doy (floor (+ 23 (* 4 month)) 10)))
1020 (when (leap-year? year)
1024 (defun parse-yearstring (string)
1025 (let ((year (or (parse-integer-insensitively string)
1026 (extract-roman string))))
1027 (when (and year (< 1500 year 2500))
1028 (make-time :year year))))
1030 (defun parse-integer-insensitively (string)
1031 (let ((start (position-if #'digit-char-p string))
1032 (end (position-if #'digit-char-p string :from-end t)))
1033 (when (and start end)
1034 (parse-integer (subseq string start (1+ end)) :junk-allowed t))))
1036 (defvar *roman-digits*
1045 (defun extract-roman (string &aux parse)
1046 (dotimes (x (length string))
1047 (let ((val (cdr (assoc (aref string x) *roman-digits*))))
1048 (when (and val parse (< (car parse) val))
1049 (push (- (pop parse)) parse))
1054 ;; ------------------------------------------------------------
1055 ;; Parsing iso-8601 timestrings
1057 (define-condition iso-8601-syntax-error (sql-user-error)
1058 ((bad-component;; year, month whatever
1059 :initarg :bad-component
1060 :reader bad-component))
1061 (:report (lambda (c stream)
1062 (format stream "Bad component: ~A " (bad-component c)))))
1064 (defun parse-timestring (timestring &key (start 0) end junk-allowed)
1065 "parse a timestring and return the corresponding wall-time. If the
1066 timestring starts with P, read a duration; otherwise read an ISO 8601
1067 formatted date string."
1068 (declare (ignore junk-allowed))
1069 (let ((string (subseq timestring start end)))
1070 (if (char= (aref string 0) #\P)
1071 (parse-iso-8601-duration string)
1072 (parse-iso-8601-time string))))
1074 (defvar *iso-8601-duration-delimiters*
1080 (defun iso-8601-delimiter (elt)
1081 (cdr (assoc elt *iso-8601-duration-delimiters*)))
1083 (defun iso-8601-duration-subseq (string start)
1084 (let* ((pos (position-if #'iso-8601-delimiter string :start start))
1085 (number (when pos (parse-integer (subseq string start pos)
1090 (iso-8601-delimiter (aref string pos))))))
1092 (defun parse-iso-8601-duration (string)
1093 "return a wall-time from a duration string"
1095 (let ((days 0) (secs 0) (hours 0) (minutes 0) (index 1))
1097 (multiple-value-bind (duration next-index duration-type)
1098 (iso-8601-duration-subseq string index)
1101 (incf hours duration))
1103 (incf minutes duration))
1105 (incf secs duration))
1107 (incf days duration))
1109 (return-from parse (make-duration :day days :hour hours
1110 :minute minutes :second secs))))
1111 (setf index next-index))))))
1113 ;; e.g. 2000-11-11 00:00:00-06
1115 (defun parse-iso-8601-time (string)
1116 "return the wall-time corresponding to the given ISO 8601 datestring"
1117 (multiple-value-bind (year month day hour minute second usec offset)
1118 (syntax-parse-iso-8601 string)
1119 (make-time :year year
1129 (defun syntax-parse-iso-8601 (string)
1130 ;; use strlen to determine if fractional seconds are present in timestamp
1131 (let ((strlen (length string))
1132 year month day hour minute second usec gmt-sec-offset)
1135 (setf year (parse-integer string :start 0 :end 4)
1136 month (parse-integer string :start 5 :end 7)
1137 day (parse-integer string :start 8 :end 10)
1138 hour (if (<= 13 strlen)
1139 (parse-integer string :start 11 :end 13)
1141 minute (if (<= 16 strlen)
1142 (parse-integer string :start 14 :end 16)
1144 second (if (<= 19 strlen)
1145 (parse-integer string :start 17 :end 19)
1148 ((or (char= #\, (char string 19))
1149 (char= #\. (char string 19)))
1150 (multiple-value-bind (parsed-usec usec-end)
1151 (parse-integer string :start 20 :junk-allowed t)
1152 (setf usec parsed-usec
1153 gmt-sec-offset (if (<= (+ 3 usec-end) strlen)
1154 (let ((skip-to (or (position #\+ string :start 19)
1155 (position #\- string :start 19))))
1158 (parse-integer string :start skip-to
1159 :end (+ skip-to 3)))
1164 gmt-sec-offset (if (<= 22 strlen)
1165 (let ((skip-to (or (position #\+ string :start 19)
1166 (position #\- string :start 19))))
1169 (parse-integer string :start skip-to
1170 :end (+ skip-to 3)))
1174 (error 'iso-8601-syntax-error
1175 :bad-component '(year . 0)))
1177 (error 'iso-8601-syntax-error
1178 :bad-component '(month . 0)))
1180 (error 'iso-8601-syntax-error
1181 :bad-component '(month . 0)))
1182 (values year month day hour minute second usec gmt-sec-offset))
1184 (error 'iso-8601-syntax-error
1186 (car (find-if (lambda (pair) (null (cdr pair)))
1187 `((year . ,year) (month . ,month)
1188 (day . ,day) (hour ,hour)
1189 (minute ,minute) (second ,second)
1190 (timezone ,gmt-sec-offset)))))))))