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-base-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))
67 (defun %print-wall-time (time stream depth)
68 (declare (ignore depth))
69 (format stream "#<WALL-TIME: ~a>" (format-time nil time)))
71 (defstruct (duration (:constructor %make-duration)
72 (:print-function %print-duration))
74 (month 0 :type fixnum)
77 (second 0 :type fixnum)
78 (minute 0 :type fixnum))
80 (defun %print-duration (duration stream depth)
81 (declare (ignore depth))
82 (format stream "#<DURATION: ~a>"
83 (format-duration nil duration :precision :second)))
87 (defun duration-timestring (duration)
88 (let ((second (duration-second duration))
89 (minute (duration-minute duration))
90 (hour (duration-hour duration))
91 (day (duration-day duration)))
92 (format nil "P~dD~dH~dM~dS" day hour minute second)))
95 ;; ------------------------------------------------------------
98 (defun make-time (&key (year 0) (month 1) (day 1) (hour 0) (minute 0)
99 (second 0) (offset 0))
100 (let ((mjd (gregorian-to-mjd month day year))
101 (sec (+ (* hour 60 60)
104 (multiple-value-bind (day-add raw-sec)
105 (floor sec (* 60 60 24))
106 (%make-wall-time :mjd (+ mjd day-add) :second raw-sec))))
108 (defun copy-time (time)
109 (%make-wall-time :mjd (time-mjd time)
110 :second (time-second time)))
113 "Return a pair: (GREGORIAN DAY . TIME-OF-DAY)"
114 (multiple-value-bind (second minute hour day mon year)
115 (decode-universal-time (get-universal-time))
116 (make-time :year year :month mon :day day :hour hour :minute minute
119 (defun make-duration (&key (year 0) (month 0) (day 0) (hour 0) (minute 0)
121 (multiple-value-bind (minute-add second-60)
123 (multiple-value-bind (hour-add minute-60)
124 (floor (+ minute minute-add) 60)
125 (multiple-value-bind (day-add hour-24)
126 (floor (+ hour hour-add) 24)
127 (%make-duration :year year :month month :day (+ day day-add)
130 :second second-60)))))
133 ;; ------------------------------------------------------------
136 (defun time-hms (time)
137 (multiple-value-bind (hourminute second)
138 (floor (time-second time) 60)
139 (multiple-value-bind (hour minute)
140 (floor hourminute 60)
141 (values hour minute second))))
143 (defun time-ymd (time)
144 (destructuring-bind (month day year)
145 (mjd-to-gregorian (time-mjd time))
146 (values year month day)))
148 (defun time-dow (time)
149 "Return the 0 indexed Day of the week starting with Sunday"
150 (mod (+ 3 (time-mjd time)) 7))
152 (defun decode-time (time)
153 "returns the decoded time as multiple values: second, minute, hour, day,
154 month, year, integer day-of-week"
155 (multiple-value-bind (year month day)
157 (multiple-value-bind (hour minute second)
159 (values second minute hour day month year (mod (+ (time-mjd time) 3) 7)))))
162 (defun duration-reduce (duration precision &optional round)
165 (+ (duration-second duration)
166 (* (duration-reduce duration :minute) 60)))
169 (floor (duration-second duration) 30)
171 (duration-minute duration)
172 (* (duration-reduce duration :hour) 60)))
175 (floor (duration-minute duration) 30)
177 (duration-hour duration)
178 (* (duration-reduce duration :day) 24)))
181 (floor (duration-hour duration) 12)
183 (duration-day duration)))))
186 ;; ------------------------------------------------------------
187 ;; Arithemetic and comparators
189 (defun duration= (duration-a duration-b)
190 (= (duration-reduce duration-a :second)
191 (duration-reduce duration-b :second)))
193 (defun duration< (duration-a duration-b)
194 (< (duration-reduce duration-a :second)
195 (duration-reduce duration-b :second)))
197 (defun duration<= (duration-a duration-b)
198 (<= (duration-reduce duration-a :second)
199 (duration-reduce duration-b :second)))
201 (defun duration>= (x y)
204 (defun duration> (x y)
208 (let ((mjd-x (time-mjd x))
209 (mjd-y (time-mjd y)))
212 (< (time-second x) (time-second y)))))
215 (if (/= (time-mjd x) (time-mjd y))
216 (>= (time-mjd x) (time-mjd y))
217 (>= (time-second x) (time-second y))))
220 (if (/= (time-mjd x) (time-mjd y))
221 (<= (time-mjd x) (time-mjd y))
222 (<= (time-second x) (time-second y))))
225 (if (/= (time-mjd x) (time-mjd y))
226 (> (time-mjd x) (time-mjd y))
227 (> (time-second x) (time-second y))))
230 (and (= (time-mjd x) (time-mjd y))
231 (= (time-second x) (time-second y))))
233 (defun time= (number &rest more-numbers)
234 "Returns T if all of its arguments are numerically equal, NIL otherwise."
235 (do ((nlist more-numbers (cdr nlist)))
237 (declare (list nlist))
238 (if (not (%time= (car nlist) number)) (return nil))))
240 (defun time/= (number &rest more-numbers)
241 "Returns T if no two of its arguments are numerically equal, NIL otherwise."
242 (do* ((head number (car nlist))
243 (nlist more-numbers (cdr nlist)))
245 (declare (list nlist))
246 (unless (do* ((nl nlist (cdr nl)))
249 (if (%time= head (car nl)) (return nil)))
252 (defun time< (number &rest more-numbers)
253 "Returns T if its arguments are in strictly increasing order, NIL otherwise."
254 (do* ((n number (car nlist))
255 (nlist more-numbers (cdr nlist)))
257 (declare (list nlist))
258 (if (not (%time< n (car nlist))) (return nil))))
260 (defun time> (number &rest more-numbers)
261 "Returns T if its arguments are in strictly decreasing order, NIL otherwise."
262 (do* ((n number (car nlist))
263 (nlist more-numbers (cdr nlist)))
265 (declare (list nlist))
266 (if (not (%time> n (car nlist))) (return nil))))
268 (defun time<= (number &rest more-numbers)
269 "Returns T if arguments are in strictly non-decreasing order, NIL otherwise."
270 (do* ((n number (car nlist))
271 (nlist more-numbers (cdr nlist)))
273 (declare (list nlist))
274 (if (not (%time<= n (car nlist))) (return nil))))
276 (defun time>= (number &rest more-numbers)
277 "Returns T if arguments are in strictly non-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-max (number &rest more-numbers)
285 "Returns the greatest of its arguments."
286 (do ((nlist more-numbers (cdr nlist))
288 ((null nlist) (return result))
289 (declare (list nlist))
290 (if (%time> (car nlist) result) (setf result (car nlist)))))
292 (defun time-min (number &rest more-numbers)
293 "Returns the least of its arguments."
294 (do ((nlist more-numbers (cdr nlist))
296 ((null nlist) (return result))
297 (declare (list nlist))
298 (if (%time< (car nlist) result) (setf result (car nlist)))))
300 (defun time-compare (time-a time-b)
301 (let ((mjd-a (time-mjd time-a))
302 (mjd-b (time-mjd time-b))
303 (sec-a (time-second time-a))
304 (sec-b (time-second time-b)))
316 ;; ------------------------------------------------------------
317 ;; Formatting and output
319 (defvar +decimal-printer+ #(#\0 #\1 #\2 #\3 #\4 #\5 #\6 #\7 #\8 #\9))
321 (defun db-timestring (time)
322 "return the string to store the given time in the database"
323 (declare (optimize (speed 3)))
324 (let ((output (copy-seq "'XXXX-XX-XX XX:XX:XX'")))
325 (flet ((inscribe-base-10 (output offset size decimal)
326 (declare (type fixnum offset size decimal)
327 (type (simple-vector 10) +decimal-printer+))
329 (declare (type fixnum x)
330 (optimize (safety 0)))
331 (multiple-value-bind (next this)
333 (setf (aref output (+ (- size x 1) offset))
334 (aref +decimal-printer+ this))
335 (setf decimal next)))))
336 (multiple-value-bind (second minute hour day month year)
338 (inscribe-base-10 output 1 4 year)
339 (inscribe-base-10 output 6 2 month)
340 (inscribe-base-10 output 9 2 day)
341 (inscribe-base-10 output 12 2 hour)
342 (inscribe-base-10 output 15 2 minute)
343 (inscribe-base-10 output 18 2 second)
346 (defun iso-timestring (time)
347 "return the string to store the given time in the database"
348 (declare (optimize (speed 3)))
349 (let ((output (copy-seq "XXXX-XX-XX XX:XX:XX")))
350 (flet ((inscribe-base-10 (output offset size decimal)
351 (declare (type fixnum offset size decimal)
352 (type (simple-vector 10) +decimal-printer+))
354 (declare (type fixnum x)
355 (optimize (safety 0)))
356 (multiple-value-bind (next this)
358 (setf (aref output (+ (- size x 1) offset))
359 (aref +decimal-printer+ this))
360 (setf decimal next)))))
361 (multiple-value-bind (second minute hour day month year)
363 (inscribe-base-10 output 0 4 year)
364 (inscribe-base-10 output 5 2 month)
365 (inscribe-base-10 output 8 2 day)
366 (inscribe-base-10 output 11 2 hour)
367 (inscribe-base-10 output 14 2 minute)
368 (inscribe-base-10 output 17 2 second)
372 ;; ------------------------------------------------------------
383 ;; fix : should also return :contains / :contained
385 (defun interval-relation (x y)
386 "Compare the relationship of node x to node y. Returns either
387 :contained :contains :follows :overlaps or :precedes."
388 (let ((xst (interval-start x))
389 (xend (interval-end x))
390 (yst (interval-start y))
391 (yend (interval-end y)))
392 (case (time-compare xst yst)
394 (case (time-compare xend yend)
397 ((:equal :greater-than)
400 (case (time-compare xst yend)
401 ((:equal :greater-than)
404 (case (time-compare xend yend)
410 (case (time-compare xend yst)
414 (case (time-compare xend yend)
417 ((:equal :greater-than)
420 ;; ------------------------------------------------------------
423 (defun sort-interval-list (list)
424 (sort list (lambda (x y)
425 (case (interval-relation x y)
426 ((:precedes :contains) t)
427 ((:follows :overlaps :contained) nil)))))
429 ;; interval push will return its list of intervals in strict order.
430 (defun interval-push (interval-list interval &optional container-rule)
431 (declare (ignore container-rule))
432 (let ((sorted-list (sort-interval-list interval-list)))
433 (dotimes (x (length sorted-list))
434 (let ((elt (nth x sorted-list)))
435 (case (interval-relation elt interval)
437 (return-from interval-push (insert-at-index x sorted-list interval)))
439 (return-from interval-push
440 (replace-at-index x sorted-list
441 (make-interval :start (interval-start elt)
442 :end (interval-end elt)
443 :type (interval-type elt)
444 :contained (interval-push (interval-contained elt) interval)
445 :data (interval-data elt)))))
446 ((:overlaps :contained)
447 (error "Overlap")))))
448 (append sorted-list (list interval))))
452 (defun interval-match (list time)
453 "Return the index of the first interval in list containing time"
454 ;; this depends on ordering of intervals!
455 (let ((list (sort-interval-list list)))
456 (dotimes (x (length list))
457 (let ((elt (nth x list)))
458 (when (and (time<= (interval-start elt) time)
459 (time< time (interval-end elt)))
460 (return-from interval-match x))))))
462 (defun interval-clear (list time)
463 (dotimes (x (length list))
464 (let ((elt (nth x list)))
465 (when (and (time<= (interval-start elt) time)
466 (time< time (interval-end elt)))
467 (if (interval-match (interval-contained elt) time)
468 (return-from interval-clear
469 (replace-at-index x list
470 (make-interval :start (interval-start elt)
471 :end (interval-end elt)
472 :type (interval-type elt)
473 :contained (interval-clear (interval-contained elt) time)
474 :data (interval-data elt))))
475 (return-from interval-clear
476 (delete-at-index x list)))))))
478 (defun interval-edit (list time start end &optional tag)
479 "Attempts to modify the most deeply nested interval in list which
480 begins at time. If no changes are made, returns nil."
481 ;; function required sorted interval list
482 (let ((list (sort-interval-list list)))
484 (dotimes (x (length list))
485 (let ((elt (nth x list)))
486 (when (and (time<= (interval-start elt) time)
487 (time< time (interval-end elt)))
488 (or (interval-edit (interval-contained elt) time start end tag)
490 (time< start (interval-end (nth (1- x) list))))
491 (error "Overlap of previous interval"))
492 ((and (< x (1- (length list)))
493 (time< (interval-start (nth (1+ x) list)) end))
494 (error "~S ~S ~S ~S Overlap of next interval" x (length list) (interval-start (nth (1+ x) list)) end ))
495 ((time= (interval-start elt) time)
496 (return-from interval-edit
497 (replace-at-index x list
498 (make-interval :start start
500 :type (interval-type elt)
501 :contained (restrict-intervals (interval-contained elt) start end)
502 :data (or tag (interval-data elt))))))))))))))
504 (defun restrict-intervals (list start end &aux newlist)
505 (let ((test-interval (make-interval :start start :end end)))
507 (when (equal :contained
508 (interval-relation elt test-interval))
512 ;;; utils from odcl/list.lisp
514 (defun replace-at-index (idx list elt)
516 (cons elt (cdr list)))
517 ((= idx (1- (length list)))
518 (append (butlast list) (list elt)))
520 (append (subseq list 0 idx)
522 (subseq list (1+ idx))))))
524 (defun insert-at-index (idx list elt)
527 ((= idx (1- (length list)))
528 (append list (list elt)))
530 (append (subseq list 0 idx)
532 (subseq list idx)))))
534 (defun delete-at-index (idx list)
537 ((= idx (1- (length list)))
540 (append (subseq list 0 idx)
541 (subseq list (1+ idx))))))
544 ;; ------------------------------------------------------------
545 ;; return MJD for Gregorian date
547 (defun gregorian-to-mjd (month day year)
550 (year-adj (if (< year 0)
558 (unless (or (< year 1582)
563 (let ((a (floor (/ year-adj 100))))
564 (setf b (+ (- 2 a) (floor (/ a 4))))))
566 (setf c (floor (- (* 365.25d0 year-adj) 679006.75d0)))
567 (setf c (floor (- (* 365.25d0 year-adj) 679006d0))))
568 (setf d (floor (* 30.6001 (+ 1 month-adj))))
569 ;; (cmsg "b ~s c ~s d ~s day ~s" b c d day)
572 ;; convert MJD to Gregorian date
574 (defun mjd-to-gregorian (mjd)
575 (let (z r g a b c year month day)
576 (setf z (floor (+ mjd 678882)))
577 (setf r (- (+ mjd 678882) z))
579 (setf a (floor (/ g 36524.25)))
580 (setf b (- a (floor (/ a 4))))
581 (setf year (floor (/ (+ b g) 365.25)))
582 (setf c (- (+ b z) (floor (* 365.25 year))))
583 (setf month (truncate (/ (+ (* 5 c) 456) 153)))
584 (setf day (+ (- c (truncate (/ (- (* 153 month) 457) 5))) r))
588 (list month day year)))
590 (defun duration+ (time &rest durations)
591 "Add each DURATION to TIME, returning a new wall-time value."
592 (let ((year (duration-year time))
593 (month (duration-month time))
594 (day (duration-day time))
595 (hour (duration-hour time))
596 (minute (duration-minute time))
597 (second (duration-second time)))
598 (dolist (duration durations)
599 (incf year (duration-year duration))
600 (incf month (duration-month duration))
601 (incf day (duration-day duration))
602 (incf hour (duration-hour duration))
603 (incf minute (duration-minute duration))
604 (incf second (duration-second duration)))
605 (make-duration :year year :month month :day day :hour hour :minute minute
608 (defun duration- (duration &rest durations)
609 "Subtract each DURATION from TIME, returning a new duration value."
610 (let ((year (duration-year duration))
611 (month (duration-month duration))
612 (day (duration-day duration))
613 (hour (duration-hour duration))
614 (minute (duration-minute duration))
615 (second (duration-second duration)))
616 (dolist (duration durations)
617 (decf year (duration-year duration))
618 (decf month (duration-month duration))
619 (decf day (duration-day duration))
620 (decf hour (duration-hour duration))
621 (decf minute (duration-minute duration))
622 (decf second (duration-second duration)))
623 (make-duration :year year :month month :day day :hour hour :minute minute
628 (defun time+ (time &rest durations)
629 "Add each DURATION to TIME, returning a new wall-time value."
630 (let ((new-time (copy-time time)))
631 (dolist (duration durations)
633 :year (duration-year duration)
634 :month (duration-month duration)
635 :day (duration-day duration)
636 :hour (duration-hour duration)
637 :minute (duration-minute duration)
638 :second (duration-second duration)
642 (defun time- (time &rest durations)
643 "Subtract each DURATION from TIME, returning a new wall-time value."
644 (let ((new-time (copy-time time)))
645 (dolist (duration durations)
647 :year (- (duration-year duration))
648 :month (- (duration-month duration))
649 :day (- (duration-day duration))
650 :hour (- (duration-hour duration))
651 :minute (- (duration-minute duration))
652 :second (- (duration-second duration))
656 (defun time-difference (time1 time2)
657 "Returns a DURATION representing the difference between TIME1 and
659 (flet ((do-diff (time1 time2)
661 (let (day-diff sec-diff)
662 (setf day-diff (- (time-mjd time2)
665 (progn (decf day-diff)
666 (setf sec-diff (+ (time-second time2)
668 (time-second time1)))))
669 (setf sec-diff (- (time-second time2)
670 (time-second time1))))
671 (make-duration :day day-diff
673 (if (time< time1 time2)
674 (do-diff time1 time2)
675 (do-diff time2 time1))))
677 (defun format-time (stream time &key format
680 (internal-separator " "))
681 "produces on stream the timestring corresponding to the wall-time
682 with the given options"
683 (multiple-value-bind (second minute hour day month year dow)
687 (format stream "~A ~A, ~A ~D, ~D"
688 (pretty-time hour minute)
694 (format stream "~A, ~D/~D/~D"
695 (pretty-time hour minute)
698 (let ((string (iso-timestring time)))
700 (write-string string stream)
703 (format stream "~2,'0D~A~2,'0D~A~2,'0D~A~2,'0D~A~2,'0D~A~2,'0D"
704 year date-separator month date-separator day
705 internal-separator hour time-separator minute time-separator
708 (defun pretty-time (hour minute)
711 (format nil "12:~2,'0D AM" minute))
713 (format nil "12:~2,'0D PM" minute))
715 (format nil "~D:~2,'0D AM" hour minute))
716 ((and (> hour 12) (< hour 24))
717 (format nil "~D:~2,'0D PM" (- hour 12) minute))
719 (error "pretty-time got bad hour"))))
721 (defun leap-days-in-days (days)
722 ;; return the number of leap days between Mar 1 2000 and
723 ;; (Mar 1 2000) + days, where days can be negative
725 (ceiling (/ (- days) (* 365 4)))
726 (floor (/ days (* 365 4)))))
728 (defun current-year ()
729 (third (mjd-to-gregorian (time-mjd (get-time)))))
731 (defun current-month ()
732 (second (mjd-to-gregorian (time-mjd (get-time)))))
734 (defun current-day ()
735 (first (mjd-to-gregorian (time-mjd (get-time)))))
737 (defun parse-date-time (string)
738 "parses date like 08/08/01, 8.8.2001, eg"
739 (when (> (length string) 1)
740 (let ((m (current-month))
743 (let ((integers (mapcar #'parse-integer (hork-integers string))))
744 (case (length integers)
746 (setf y (car integers)))
748 (setf m (car integers))
749 (setf y (cadr integers)))
751 (setf m (car integers))
752 (setf d (cadr integers))
753 (setf y (caddr integers)))
755 (return-from parse-date-time))))
758 (make-time :year y :month m :day d))))
760 (defun hork-integers (input)
763 (dotimes (x (length input))
764 (unless (<= 48 (char-code (aref input x)) 57)
765 (push (subseq input start x) output)
766 (setf start (1+ x))))
767 (nreverse (push (subseq input start) output))))
769 (defun merged-time (day time-of-day)
770 (%make-wall-time :mjd (time-mjd day)
771 :second (time-second time-of-day)))
773 (defun time-meridian (hours)
779 (values (- hours 12) "PM"))
781 (values hours "AM"))))
783 (defmethod to-string ((time wall-time) &rest keys)
784 (destructuring-bind (&key (style :daytime) &allow-other-keys)
786 (print-date time style)))
788 (defun print-date (time &optional (style :daytime))
789 (multiple-value-bind (second minute hour day month year dow)
791 (declare (ignore second))
792 (multiple-value-bind (hours meridian)
797 (format nil "~d:~2,'0d ~a" hours minute meridian))
799 ;; October 11th, 2000
800 (format nil "~a ~d, ~d" (month-name month) day year))
806 (format nil "~a ~d" (month-name month) year))
808 ;; 11:08 AM, November 22, 2002
809 (format nil "~d:~2,'0d ~a, ~a ~d, ~d"
810 hours minute meridian (month-name month) day year))
812 ;; 11:09 AM Friday, November 22, 2002
813 (format nil "~d:~2,'0d ~a ~a, ~a ~d, ~d"
814 hours minute meridian (nth dow *day-names*)
815 (month-name month) day year))
817 ;; 11:09 AM, 11/22/2002
818 (format-time nil time :format :short-pretty))
821 (format nil "~d/~d/~d" month day year))))))
823 (defun time-element (time element)
824 (multiple-value-bind (second minute hour day month year dow)
835 (:integer-day-of-week
838 (nth dow *day-keywords*))
844 (defun format-duration (stream duration &key (precision :minute))
845 (let ((second (duration-second duration))
846 (minute (duration-minute duration))
847 (hour (duration-hour duration))
848 (day (duration-day duration))
849 (return (null stream))
850 (stream (or stream (make-string-output-stream))))
853 (setf hour 0 second 0 minute 0))
855 (setf second 0 minute 0))
860 (if (= 0 day hour minute)
861 (format stream "0 minutes")
864 (format stream "~d day~p" day day)
868 (write-char #\Space stream))
869 (format stream "~d hour~p" hour hour)
873 (write-char #\Space stream))
874 (format stream "~d min~p" minute minute)
878 (write-char #\Space stream))
879 (format stream "~d sec~p" second second))))
881 (get-output-stream-string stream))))
883 (defgeneric midnight (self))
884 (defmethod midnight ((self wall-time))
885 "truncate hours, minutes and seconds"
886 (%make-wall-time :mjd (time-mjd self)))
888 (defun roll (date &key (year 0) (month 0) (day 0) (second 0) (hour 0)
889 (minute 0) (destructive nil))
890 (unless (= 0 year month)
891 (multiple-value-bind (year-orig month-orig day-orig)
893 (setf date (make-time :year (+ year year-orig)
894 :month (+ month month-orig)
896 :second (time-second date)))))
897 (let ((mjd (time-mjd date))
898 (sec (time-second date)))
899 (multiple-value-bind (mjd-new sec-new)
902 (* 60 60 hour)) (* 60 60 24))
905 (setf (time-mjd date) (+ mjd mjd-new day)
906 (time-second date) sec-new)
908 (%make-wall-time :mjd (+ mjd mjd-new day)
911 (defun roll-to (date size position)
917 (- (time-element date :day-of-month)))))
919 (roll date :day (+ (days-in-month (time-element date :month)
920 (time-element date :year))
921 (- (time-element date :day-of-month)))))))))
923 (defun week-containing (time)
924 (let* ((midn (midnight time))
925 (dow (time-element midn :integer-day-of-week)))
926 (list (roll midn :day (- dow))
927 (roll midn :day (- 7 dow)))))
929 (defun leap-year? (year)
930 "t if YEAR is a leap yeap in the Gregorian calendar"
931 (and (= 0 (mod year 4))
932 (or (not (= 0 (mod year 100)))
933 (= 0 (mod year 400)))))
935 (defun valid-month-p (month)
936 "t if MONTH exists in the Gregorian calendar"
939 (defun valid-gregorian-date-p (date)
940 "t if DATE (year month day) exists in the Gregorian calendar"
941 (let ((max-day (days-in-month (nth 1 date) (nth 0 date))))
942 (<= 1 (nth 2 date) max-day)))
944 (defun days-in-month (month year &key (careful t))
945 "the number of days in MONTH of YEAR, observing Gregorian leap year
947 (declare (type fixnum month year))
949 (check-type month (satisfies valid-month-p)
950 "between 1 (January) and 12 (December)"))
951 (if (eql month 2) ; feb
952 (if (leap-year? year)
954 (let ((even (mod (1- month) 2)))
955 (if (< month 8) ; aug
959 (defun day-of-year (year month day &key (careful t))
960 "the day number within the year of the date DATE. For example,
962 (declare (type fixnum year month day))
964 (let ((date (list year month day)))
965 (check-type date (satisfies valid-gregorian-date-p)
966 "a valid Gregorian date")))
967 (let ((doy (+ day (* 31 (1- month)))))
968 (declare (type fixnum doy))
970 (setf doy (- doy (floor (+ 23 (* 4 month)) 10)))
971 (when (leap-year? year)
975 (defun parse-yearstring (string)
976 (let ((year (or (parse-integer-insensitively string)
977 (extract-roman string))))
978 (when (and year (< 1500 year 2500))
979 (make-time :year year))))
981 (defun parse-integer-insensitively (string)
982 (let ((start (position-if #'digit-char-p string))
983 (end (position-if #'digit-char-p string :from-end t)))
984 (when (and start end)
985 (parse-integer (subseq string start (1+ end)) :junk-allowed t))))
987 (defvar *roman-digits*
996 (defun extract-roman (string &aux parse)
997 (dotimes (x (length string))
998 (let ((val (cdr (assoc (aref string x) *roman-digits*))))
999 (when (and val parse (< (car parse) val))
1000 (push (- (pop parse)) parse))
1005 ;; ------------------------------------------------------------
1006 ;; Parsing iso-8601 timestrings
1008 (define-condition iso-8601-syntax-error (error)
1009 ((bad-component;; year, month whatever
1010 :initarg :bad-component
1011 :reader bad-component)))
1013 (defun parse-timestring (timestring &key (start 0) end junk-allowed)
1014 "parse a timestring and return the corresponding wall-time. If the
1015 timestring starts with P, read a duration; otherwise read an ISO 8601
1016 formatted date string."
1017 (declare (ignore junk-allowed)) ;; FIXME
1018 (let ((string (subseq timestring start end)))
1019 (if (char= (aref string 0) #\P)
1020 (parse-iso-8601-duration string)
1021 (parse-iso-8601-time string))))
1023 (defvar *iso-8601-duration-delimiters*
1029 (defun iso-8601-delimiter (elt)
1030 (cdr (assoc elt *iso-8601-duration-delimiters*)))
1032 (defun iso-8601-duration-subseq (string start)
1033 (let* ((pos (position-if #'iso-8601-delimiter string :start start))
1034 (number (when pos (parse-integer (subseq string start pos)
1039 (iso-8601-delimiter (aref string pos))))))
1041 (defun parse-iso-8601-duration (string)
1042 "return a wall-time from a duration string"
1044 (let ((days 0) (secs 0) (hours 0) (minutes 0) (index 1))
1046 (multiple-value-bind (duration next-index duration-type)
1047 (iso-8601-duration-subseq string index)
1050 (incf hours duration))
1052 (incf minutes duration))
1054 (incf secs duration))
1056 (incf days duration))
1058 (return-from parse (make-duration :day days :hour hours
1059 :minute minutes :second secs))))
1060 (setf index next-index))))))
1062 ;; e.g. 2000-11-11 00:00:00-06
1064 (defun parse-iso-8601-time (string)
1065 "return the wall-time corresponding to the given ISO 8601 datestring"
1066 (multiple-value-bind (year month day hour minute second offset)
1067 (syntax-parse-iso-8601 string)
1068 (make-time :year year
1077 (defun syntax-parse-iso-8601 (string)
1078 (let (year month day hour minute second gmt-sec-offset)
1081 (setf year (parse-integer (subseq string 0 4))
1082 month (parse-integer (subseq string 5 7))
1083 day (parse-integer (subseq string 8 10))
1084 hour (if (<= 13 (length string))
1085 (parse-integer (subseq string 11 13))
1087 minute (if (<= 16 (length string))
1088 (parse-integer (subseq string 14 16))
1090 second (if (<= 19 (length string))
1091 (parse-integer (subseq string 17 19))
1093 gmt-sec-offset (if (<= 22 (length string))
1095 (parse-integer (subseq string 19 22)))
1098 (error 'iso-8601-syntax-error
1099 :bad-component '(year . 0)))
1101 (error 'iso-8601-syntax-error
1102 :bad-component '(month . 0)))
1104 (error 'iso-8601-syntax-error
1105 :bad-component '(month . 0)))
1106 (values year month day hour minute second gmt-sec-offset))
1108 (error 'iso-8601-syntax-error
1110 (car (find-if (lambda (pair) (null (cdr pair)))
1111 `((year . ,year) (month . ,month)
1112 (day . ,day) (hour ,hour)
1113 (minute ,minute) (second ,second)
1114 (timezone ,gmt-sec-offset)))))))))