1 ;;; srfi-19.scm --- Time/Date Library
3 ;; Copyright (C) 2001, 2002, 2003, 2005, 2006, 2007, 2008, 2009, 2010,
4 ;; 2011, 2014 Free Software Foundation, Inc.
6 ;; This library is free software; you can redistribute it and/or
7 ;; modify it under the terms of the GNU Lesser General Public
8 ;; License as published by the Free Software Foundation; either
9 ;; version 3 of the License, or (at your option) any later version.
11 ;; This library is distributed in the hope that it will be useful,
12 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ;; Lesser General Public License for more details.
16 ;; You should have received a copy of the GNU Lesser General Public
17 ;; License along with this library; if not, write to the Free Software
18 ;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 ;;; Author: Rob Browning <rlb@cs.utexas.edu>
21 ;;; Originally from SRFI reference implementation by Will Fitzgerald.
25 ;; This module is fully documented in the Guile Reference Manual.
29 ;; FIXME: I haven't checked a decent amount of this code for potential
30 ;; performance improvements, but I suspect that there may be some
31 ;; substantial ones to be realized, esp. in the later "parsing" half
32 ;; of the file, by rewriting the code with use of more Guile native
33 ;; functions that do more work in a "chunk".
35 ;; FIXME: mkoeppe: Time zones are treated a little simplistic in
36 ;; SRFI-19; they are only a numeric offset. Thus, printing time zones
37 ;; (LOCALE-PRINT-TIME-ZONE) can't be implemented sensibly. The
38 ;; functions taking an optional TZ-OFFSET should be extended to take a
39 ;; symbolic time-zone (like "CET"); this string should be stored in
40 ;; the DATE structure.
42 (define-module (srfi srfi-19)
43 :use-module (srfi srfi-6)
44 :use-module (srfi srfi-8)
45 :use-module (srfi srfi-9)
46 :autoload (ice-9 rdelim) (read-line)
47 :use-module (ice-9 i18n)
48 :replace (current-time)
56 ;; Current time and clock resolution
59 current-modified-julian-day
61 ;; Time object and accessors
71 ;; Time comparison procedures
77 ;; Time arithmetic procedures
84 ;; Date object and accessors
98 ;; Time/Date/Julian Day/Modified Julian Day converters
100 date->modified-julian-day
105 julian-day->time-monotonic
108 modified-julian-day->date
109 modified-julian-day->time-monotonic
110 modified-julian-day->time-tai
111 modified-julian-day->time-utc
113 time-monotonic->julian-day
114 time-monotonic->modified-julian-day
115 time-monotonic->time-tai
116 time-monotonic->time-tai!
117 time-monotonic->time-utc
118 time-monotonic->time-utc!
121 time-tai->modified-julian-day
122 time-tai->time-monotonic
123 time-tai->time-monotonic!
128 time-utc->modified-julian-day
129 time-utc->time-monotonic
130 time-utc->time-monotonic!
133 ;; Date to string/string to date converters.
137 (cond-expand-provide (current-module) '(srfi-19))
139 (define time-tai 'time-tai)
140 (define time-utc 'time-utc)
141 (define time-monotonic 'time-monotonic)
142 (define time-thread 'time-thread)
143 (define time-process 'time-process)
144 (define time-duration 'time-duration)
146 ;; FIXME: do we want to add gc time?
147 ;; (define time-gc 'time-gc)
149 ;;-- LOCALE dependent constants
152 (define locale-date-time-format "~a ~b ~d ~H:~M:~S~z ~Y")
153 (define locale-short-date-format "~m/~d/~y")
154 (define locale-time-format "~H:~M:~S")
155 (define iso-8601-date-time-format "~Y-~m-~dT~H:~M:~S~z")
157 ;;-- Miscellaneous Constants.
158 ;;-- only the tai-epoch-in-jd might need changing if
159 ;; a different epoch is used.
161 (define nano 1000000000) ; nanoseconds in a second
162 (define sid 86400) ; seconds in a day
163 (define sihd 43200) ; seconds in a half day
164 (define tai-epoch-in-jd 4881175/2) ; julian day number for 'the epoch'
166 ;; FIXME: should this be something other than misc-error?
167 (define (time-error caller type value)
169 (throw 'misc-error caller "TIME-ERROR type ~A: ~S" (list type value) #f)
170 (throw 'misc-error caller "TIME-ERROR type ~A" (list type) #f)))
172 ;; A table of leap seconds
173 ;; See ftp://maia.usno.navy.mil/ser7/tai-utc.dat
174 ;; and update as necessary.
175 ;; this procedures reads the file in the above
176 ;; format and creates the leap second table
177 ;; it also calls the almost standard, but not R5 procedures read-line
178 ;; & open-input-string
179 ;; ie (set! leap-second-table (read-tai-utc-date "tai-utc.dat"))
181 (define (read-tai-utc-data filename)
182 (define (convert-jd jd)
183 (* (- (inexact->exact jd) tai-epoch-in-jd) sid))
184 (define (convert-sec sec)
185 (inexact->exact sec))
186 (let ((port (open-input-file filename))
188 (let loop ((line (read-line port)))
189 (if (not (eof-object? line))
191 (let* ((data (read (open-input-string
192 (string-append "(" line ")"))))
194 (jd (cadddr (cdr data)))
195 (secs (cadddr (cdddr data))))
198 (cons (convert-jd jd) (convert-sec secs))
200 (loop (read-line port))))))
203 ;; each entry is (tai seconds since epoch . # seconds to subtract for utc)
204 ;; note they go higher to lower, and end in 1972.
205 (define leap-second-table
233 (define (read-leap-second-table filename)
234 (set! leap-second-table (read-tai-utc-data filename)))
237 (define (leap-second-delta utc-seconds)
238 (letrec ((lsd (lambda (table)
239 (cond ((>= utc-seconds (caar table))
241 (else (lsd (cdr table)))))))
242 (if (< utc-seconds (* (- 1972 1970) 365 sid)) 0
243 (lsd leap-second-table))))
246 ;;; the TIME structure; creates the accessors, too.
248 (define-record-type time
249 (make-time-unnormalized type nanosecond second)
251 (type time-type set-time-type!)
252 (nanosecond time-nanosecond set-time-nanosecond!)
253 (second time-second set-time-second!))
255 (define (copy-time time)
256 (make-time (time-type time) (time-nanosecond time) (time-second time)))
258 (define (split-real r)
260 (values (inexact->exact r) 0)
261 (let ((l (truncate r)))
262 (values (inexact->exact l) (- r l)))))
264 (define (time-normalize! t)
265 (if (>= (abs (time-nanosecond t)) 1000000000)
267 (split-real (time-nanosecond t))
268 (set-time-second! t (+ (time-second t)
269 (quotient int 1000000000)))
270 (set-time-nanosecond! t (+ (remainder int 1000000000)
272 (if (and (positive? (time-second t))
273 (negative? (time-nanosecond t)))
275 (set-time-second! t (- (time-second t) 1))
276 (set-time-nanosecond! t (+ 1000000000 (time-nanosecond t))))
277 (if (and (negative? (time-second t))
278 (positive? (time-nanosecond t)))
280 (set-time-second! t (+ (time-second t) 1))
281 (set-time-nanosecond! t (+ 1000000000 (time-nanosecond t))))))
284 (define (make-time type nanosecond second)
285 (time-normalize! (make-time-unnormalized type nanosecond second)))
288 ;; FIXME: finish this and publish it?
289 (define (date->broken-down-time date)
290 (let ((result (mktime 0)))
291 ;; FIXME: What should we do about leap-seconds which may overflow
293 (set-tm:sec result (date-second date))
294 (set-tm:min result (date-minute date))
295 (set-tm:hour result (date-hour date))
296 ;; FIXME: SRFI day ranges from 0-31. (not compatible with set-tm:mday).
297 (set-tm:mday result (date-day date))
298 (set-tm:mon result (- (date-month date) 1))
299 ;; FIXME: need to signal error on range violation.
300 (set-tm:year result (+ 1900 (date-year date)))
301 (set-tm:isdst result -1)
302 (set-tm:gmtoff result (- (date-zone-offset date)))
307 ;;; specific time getters.
309 (define (current-time-utc)
310 ;; Resolution is microseconds.
311 (let ((tod (gettimeofday)))
312 (make-time time-utc (* (cdr tod) 1000) (car tod))))
314 (define (current-time-tai)
315 ;; Resolution is microseconds.
316 (let* ((tod (gettimeofday))
321 (+ (car tod) (leap-second-delta sec)))))
323 ;;(define (current-time-ms-time time-type proc)
324 ;; (let ((current-ms (proc)))
325 ;; (make-time time-type
326 ;; (quotient current-ms 10000)
327 ;; (* (remainder current-ms 1000) 10000))))
329 ;; -- we define it to be the same as TAI.
330 ;; A different implemation of current-time-montonic
331 ;; will require rewriting all of the time-monotonic converters,
334 (define (current-time-monotonic)
335 ;; Resolution is microseconds.
338 (define (current-time-thread)
339 (time-error 'current-time 'unsupported-clock-type 'time-thread))
341 (define ns-per-guile-tick (/ 1000000000 internal-time-units-per-second))
343 (define (current-time-process)
344 (let ((run-time (get-internal-run-time)))
347 (* (remainder run-time internal-time-units-per-second)
349 (quotient run-time internal-time-units-per-second))))
351 ;;(define (current-time-gc)
352 ;; (current-time-ms-time time-gc current-gc-milliseconds))
354 (define (current-time . clock-type)
355 (let ((clock-type (if (null? clock-type) time-utc (car clock-type))))
357 ((eq? clock-type time-tai) (current-time-tai))
358 ((eq? clock-type time-utc) (current-time-utc))
359 ((eq? clock-type time-monotonic) (current-time-monotonic))
360 ((eq? clock-type time-thread) (current-time-thread))
361 ((eq? clock-type time-process) (current-time-process))
362 ;; ((eq? clock-type time-gc) (current-time-gc))
363 (else (time-error 'current-time 'invalid-clock-type clock-type)))))
365 ;; -- Time Resolution
366 ;; This is the resolution of the clock in nanoseconds.
367 ;; This will be implementation specific.
369 (define (time-resolution . clock-type)
370 (let ((clock-type (if (null? clock-type) time-utc (car clock-type))))
374 ((time-monotonic) 1000)
375 ((time-process) ns-per-guile-tick)
376 ;; ((eq? clock-type time-thread) 1000)
377 ;; ((eq? clock-type time-gc) 10000)
378 (else (time-error 'time-resolution 'invalid-clock-type clock-type)))))
380 ;; -- Time comparisons
382 (define (time=? t1 t2)
383 ;; Arrange tests for speed and presume that t1 and t2 are actually times.
384 ;; also presume it will be rare to check two times of different types.
385 (and (= (time-second t1) (time-second t2))
386 (= (time-nanosecond t1) (time-nanosecond t2))
387 (eq? (time-type t1) (time-type t2))))
389 (define (time>? t1 t2)
390 (or (> (time-second t1) (time-second t2))
391 (and (= (time-second t1) (time-second t2))
392 (> (time-nanosecond t1) (time-nanosecond t2)))))
394 (define (time<? t1 t2)
395 (or (< (time-second t1) (time-second t2))
396 (and (= (time-second t1) (time-second t2))
397 (< (time-nanosecond t1) (time-nanosecond t2)))))
399 (define (time>=? t1 t2)
400 (or (> (time-second t1) (time-second t2))
401 (and (= (time-second t1) (time-second t2))
402 (>= (time-nanosecond t1) (time-nanosecond t2)))))
404 (define (time<=? t1 t2)
405 (or (< (time-second t1) (time-second t2))
406 (and (= (time-second t1) (time-second t2))
407 (<= (time-nanosecond t1) (time-nanosecond t2)))))
409 ;; -- Time arithmetic
411 (define (time-difference! time1 time2)
412 (let ((sec-diff (- (time-second time1) (time-second time2)))
413 (nsec-diff (- (time-nanosecond time1) (time-nanosecond time2))))
414 (set-time-type! time1 time-duration)
415 (set-time-second! time1 sec-diff)
416 (set-time-nanosecond! time1 nsec-diff)
417 (time-normalize! time1)))
419 (define (time-difference time1 time2)
420 (let ((result (copy-time time1)))
421 (time-difference! result time2)))
423 (define (add-duration! t duration)
424 (if (not (eq? (time-type duration) time-duration))
425 (time-error 'add-duration 'not-duration duration)
426 (let ((sec-plus (+ (time-second t) (time-second duration)))
427 (nsec-plus (+ (time-nanosecond t) (time-nanosecond duration))))
428 (set-time-second! t sec-plus)
429 (set-time-nanosecond! t nsec-plus)
430 (time-normalize! t))))
432 (define (add-duration t duration)
433 (let ((result (copy-time t)))
434 (add-duration! result duration)))
436 (define (subtract-duration! t duration)
437 (if (not (eq? (time-type duration) time-duration))
438 (time-error 'add-duration 'not-duration duration)
439 (let ((sec-minus (- (time-second t) (time-second duration)))
440 (nsec-minus (- (time-nanosecond t) (time-nanosecond duration))))
441 (set-time-second! t sec-minus)
442 (set-time-nanosecond! t nsec-minus)
443 (time-normalize! t))))
445 (define (subtract-duration time1 duration)
446 (let ((result (copy-time time1)))
447 (subtract-duration! result duration)))
449 ;; -- Converters between types.
451 (define (priv:time-tai->time-utc! time-in time-out caller)
452 (if (not (eq? (time-type time-in) time-tai))
453 (time-error caller 'incompatible-time-types time-in))
454 (set-time-type! time-out time-utc)
455 (set-time-nanosecond! time-out (time-nanosecond time-in))
456 (set-time-second! time-out (- (time-second time-in)
458 (time-second time-in))))
461 (define (time-tai->time-utc time-in)
462 (priv:time-tai->time-utc! time-in (make-time-unnormalized #f #f #f) 'time-tai->time-utc))
465 (define (time-tai->time-utc! time-in)
466 (priv:time-tai->time-utc! time-in time-in 'time-tai->time-utc!))
468 (define (priv:time-utc->time-tai! time-in time-out caller)
469 (if (not (eq? (time-type time-in) time-utc))
470 (time-error caller 'incompatible-time-types time-in))
471 (set-time-type! time-out time-tai)
472 (set-time-nanosecond! time-out (time-nanosecond time-in))
473 (set-time-second! time-out (+ (time-second time-in)
475 (time-second time-in))))
478 (define (time-utc->time-tai time-in)
479 (priv:time-utc->time-tai! time-in (make-time-unnormalized #f #f #f) 'time-utc->time-tai))
481 (define (time-utc->time-tai! time-in)
482 (priv:time-utc->time-tai! time-in time-in 'time-utc->time-tai!))
484 ;; -- these depend on time-monotonic having the same definition as time-tai!
485 (define (time-monotonic->time-utc time-in)
486 (if (not (eq? (time-type time-in) time-monotonic))
487 (time-error 'time-monotonic->time-utc
488 'incompatible-time-types time-in))
489 (let ((ntime (copy-time time-in)))
490 (set-time-type! ntime time-tai)
491 (priv:time-tai->time-utc! ntime ntime 'time-monotonic->time-utc)))
493 (define (time-monotonic->time-utc! time-in)
494 (if (not (eq? (time-type time-in) time-monotonic))
495 (time-error 'time-monotonic->time-utc!
496 'incompatible-time-types time-in))
497 (set-time-type! time-in time-tai)
498 (priv:time-tai->time-utc! time-in time-in 'time-monotonic->time-utc))
500 (define (time-monotonic->time-tai time-in)
501 (if (not (eq? (time-type time-in) time-monotonic))
502 (time-error 'time-monotonic->time-tai
503 'incompatible-time-types time-in))
504 (let ((ntime (copy-time time-in)))
505 (set-time-type! ntime time-tai)
508 (define (time-monotonic->time-tai! time-in)
509 (if (not (eq? (time-type time-in) time-monotonic))
510 (time-error 'time-monotonic->time-tai!
511 'incompatible-time-types time-in))
512 (set-time-type! time-in time-tai)
515 (define (time-utc->time-monotonic time-in)
516 (if (not (eq? (time-type time-in) time-utc))
517 (time-error 'time-utc->time-monotonic
518 'incompatible-time-types time-in))
519 (let ((ntime (priv:time-utc->time-tai! time-in (make-time-unnormalized #f #f #f)
520 'time-utc->time-monotonic)))
521 (set-time-type! ntime time-monotonic)
524 (define (time-utc->time-monotonic! time-in)
525 (if (not (eq? (time-type time-in) time-utc))
526 (time-error 'time-utc->time-monotonic!
527 'incompatible-time-types time-in))
528 (let ((ntime (priv:time-utc->time-tai! time-in time-in
529 'time-utc->time-monotonic!)))
530 (set-time-type! ntime time-monotonic)
533 (define (time-tai->time-monotonic time-in)
534 (if (not (eq? (time-type time-in) time-tai))
535 (time-error 'time-tai->time-monotonic
536 'incompatible-time-types time-in))
537 (let ((ntime (copy-time time-in)))
538 (set-time-type! ntime time-monotonic)
541 (define (time-tai->time-monotonic! time-in)
542 (if (not (eq? (time-type time-in) time-tai))
543 (time-error 'time-tai->time-monotonic!
544 'incompatible-time-types time-in))
545 (set-time-type! time-in time-monotonic)
548 ;; -- Date Structures
550 ;; FIXME: to be really safe, perhaps we should normalize the
551 ;; seconds/nanoseconds/minutes coming in to make-date...
553 (define-record-type date
554 (make-date nanosecond second minute
559 (nanosecond date-nanosecond set-date-nanosecond!)
560 (second date-second set-date-second!)
561 (minute date-minute set-date-minute!)
562 (hour date-hour set-date-hour!)
563 (day date-day set-date-day!)
564 (month date-month set-date-month!)
565 (year date-year set-date-year!)
566 (zone-offset date-zone-offset set-date-zone-offset!))
568 ;; gives the julian day which starts at noon.
569 (define (encode-julian-day-number day month year)
570 (let* ((a (quotient (- 14 month) 12))
571 (y (- (+ year 4800) a (if (negative? year) -1 0)))
572 (m (- (+ month (* 12 a)) 3)))
574 (quotient (+ (* 153 m) 2) 5)
581 ;; gives the seconds/date/month/year
582 (define (decode-julian-day-number jdn)
583 (let* ((days (inexact->exact (truncate jdn)))
585 (b (quotient (+ (* 4 a) 3) 146097))
586 (c (- a (quotient (* 146097 b) 4)))
587 (d (quotient (+ (* 4 c) 3) 1461))
588 (e (- c (quotient (* 1461 d) 4)))
589 (m (quotient (+ (* 5 e) 2) 153))
590 (y (+ (* 100 b) d -4800 (quotient m 10))))
591 (values ; seconds date month year
593 (+ e (- (quotient (+ (* 153 m) 2) 5)) 1)
594 (+ m 3 (* -12 (quotient m 10)))
595 (if (>= 0 y) (- y 1) y))))
597 ;; relies on the fact that we named our time zone accessor
598 ;; differently from MzScheme's....
599 ;; This should be written to be OS specific.
601 (define (local-tz-offset utc-time)
602 ;; SRFI uses seconds West, but guile (and libc) use seconds East.
603 (- (tm:gmtoff (localtime (time-second utc-time)))))
605 ;; special thing -- ignores nanos
606 (define (time->julian-day-number seconds tz-offset)
607 (+ (/ (+ seconds tz-offset sihd)
611 (define (leap-second? second)
612 (and (assoc second leap-second-table) #t))
614 (define (time-utc->date time . tz-offset)
615 (if (not (eq? (time-type time) time-utc))
616 (time-error 'time->date 'incompatible-time-types time))
617 (let* ((offset (if (null? tz-offset)
618 (local-tz-offset time)
620 (leap-second? (leap-second? (+ offset (time-second time))))
621 (jdn (time->julian-day-number (if leap-second?
622 (- (time-second time) 1)
626 (call-with-values (lambda () (decode-julian-day-number jdn))
627 (lambda (secs date month year)
628 ;; secs is a real because jdn is a real in Guile;
629 ;; but it is conceptionally an integer.
630 (let* ((int-secs (inexact->exact (round secs)))
631 (hours (quotient int-secs (* 60 60)))
632 (rem (remainder int-secs (* 60 60)))
633 (minutes (quotient rem 60))
634 (seconds (remainder rem 60)))
635 (make-date (time-nanosecond time)
636 (if leap-second? (+ seconds 1) seconds)
644 (define (time-tai->date time . tz-offset)
645 (if (not (eq? (time-type time) time-tai))
646 (time-error 'time->date 'incompatible-time-types time))
647 (let* ((offset (if (null? tz-offset)
648 (local-tz-offset (time-tai->time-utc time))
650 (seconds (- (time-second time)
651 (leap-second-delta (time-second time))))
652 (leap-second? (leap-second? (+ offset seconds)))
653 (jdn (time->julian-day-number (if leap-second?
657 (call-with-values (lambda () (decode-julian-day-number jdn))
658 (lambda (secs date month year)
659 ;; secs is a real because jdn is a real in Guile;
660 ;; but it is conceptionally an integer.
661 ;; adjust for leap seconds if necessary ...
662 (let* ((int-secs (inexact->exact (round secs)))
663 (hours (quotient int-secs (* 60 60)))
664 (rem (remainder int-secs (* 60 60)))
665 (minutes (quotient rem 60))
666 (seconds (remainder rem 60)))
667 (make-date (time-nanosecond time)
668 (if leap-second? (+ seconds 1) seconds)
676 ;; this is the same as time-tai->date.
677 (define (time-monotonic->date time . tz-offset)
678 (if (not (eq? (time-type time) time-monotonic))
679 (time-error 'time->date 'incompatible-time-types time))
680 (let* ((offset (if (null? tz-offset)
681 (local-tz-offset (time-monotonic->time-utc time))
683 (seconds (- (time-second time)
684 (leap-second-delta (time-second time))))
685 (leap-second? (leap-second? (+ offset seconds)))
686 (jdn (time->julian-day-number (if leap-second?
690 (call-with-values (lambda () (decode-julian-day-number jdn))
691 (lambda (secs date month year)
692 ;; secs is a real because jdn is a real in Guile;
693 ;; but it is conceptionally an integer.
694 ;; adjust for leap seconds if necessary ...
695 (let* ((int-secs (inexact->exact (round secs)))
696 (hours (quotient int-secs (* 60 60)))
697 (rem (remainder int-secs (* 60 60)))
698 (minutes (quotient rem 60))
699 (seconds (remainder rem 60)))
700 (make-date (time-nanosecond time)
701 (if leap-second? (+ seconds 1) seconds)
709 (define (date->time-utc date)
710 (let* ((jdays (- (encode-julian-day-number (date-day date)
714 ;; jdays is an integer plus 1/2,
715 (jdays-1/2 (inexact->exact (- jdays 1/2))))
718 (date-nanosecond date)
719 (+ (* jdays-1/2 24 60 60)
720 (* (date-hour date) 60 60)
721 (* (date-minute date) 60)
723 (- (date-zone-offset date))))))
725 (define (date->time-tai date)
726 (time-utc->time-tai! (date->time-utc date)))
728 (define (date->time-monotonic date)
729 (time-utc->time-monotonic! (date->time-utc date)))
731 (define (leap-year? year)
732 (or (= (modulo year 400) 0)
733 (and (= (modulo year 4) 0) (not (= (modulo year 100) 0)))))
735 ;; Map 1-based month number M to number of days in the year before the
736 ;; start of month M (in a non-leap year).
737 (define month-assoc '((1 . 0) (2 . 31) (3 . 59) (4 . 90)
738 (5 . 120) (6 . 151) (7 . 181) (8 . 212)
739 (9 . 243) (10 . 273) (11 . 304) (12 . 334)))
741 (define (year-day day month year)
742 (let ((days-pr (assoc month month-assoc)))
744 (time-error 'date-year-day 'invalid-month-specification month))
745 (if (and (leap-year? year) (> month 2))
746 (+ day (cdr days-pr) 1)
747 (+ day (cdr days-pr)))))
749 (define (date-year-day date)
750 (year-day (date-day date) (date-month date) (date-year date)))
753 (define (week-day day month year)
754 (let* ((a (quotient (- 14 month) 12))
756 (m (+ month (* 12 a) -2)))
762 (quotient (* 31 m) 12))
765 (define (date-week-day date)
766 (week-day (date-day date) (date-month date) (date-year date)))
768 (define (days-before-first-week date day-of-week-starting-week)
769 (let* ((first-day (make-date 0 0 0 0
774 (fdweek-day (date-week-day first-day)))
775 (modulo (- day-of-week-starting-week fdweek-day)
778 ;; The "-1" here is a fix for the reference implementation, to make a new
779 ;; week start on the given day-of-week-starting-week. date-year-day returns
780 ;; a day starting from 1 for 1st Jan.
782 (define (date-week-number date day-of-week-starting-week)
783 (quotient (- (date-year-day date)
785 (days-before-first-week date day-of-week-starting-week))
788 (define (current-date . tz-offset)
789 (let ((time (current-time time-utc)))
792 (if (null? tz-offset)
793 (local-tz-offset time)
796 ;; given a 'two digit' number, find the year within 50 years +/-
797 (define (natural-year n)
798 (let* ((current-year (date-year (current-date)))
799 (current-century (* (quotient current-year 100) 100)))
803 ((<= (- (+ current-century n) current-year) 50) (+ current-century n))
804 (else (+ (- current-century 100) n)))))
806 (define (date->julian-day date)
807 (let ((nanosecond (date-nanosecond date))
808 (second (date-second date))
809 (minute (date-minute date))
810 (hour (date-hour date))
811 (day (date-day date))
812 (month (date-month date))
813 (year (date-year date))
814 (offset (date-zone-offset date)))
815 (+ (encode-julian-day-number day month year)
824 (define (date->modified-julian-day date)
825 (- (date->julian-day date)
828 (define (time-utc->julian-day time)
829 (if (not (eq? (time-type time) time-utc))
830 (time-error 'time->date 'incompatible-time-types time))
831 (+ (/ (+ (time-second time) (/ (time-nanosecond time) nano))
835 (define (time-utc->modified-julian-day time)
836 (- (time-utc->julian-day time)
839 (define (time-tai->julian-day time)
840 (if (not (eq? (time-type time) time-tai))
841 (time-error 'time->date 'incompatible-time-types time))
842 (+ (/ (+ (- (time-second time)
843 (leap-second-delta (time-second time)))
844 (/ (time-nanosecond time) nano))
848 (define (time-tai->modified-julian-day time)
849 (- (time-tai->julian-day time)
852 ;; this is the same as time-tai->julian-day
853 (define (time-monotonic->julian-day time)
854 (if (not (eq? (time-type time) time-monotonic))
855 (time-error 'time->date 'incompatible-time-types time))
856 (+ (/ (+ (- (time-second time)
857 (leap-second-delta (time-second time)))
858 (/ (time-nanosecond time) nano))
862 (define (time-monotonic->modified-julian-day time)
863 (- (time-monotonic->julian-day time)
866 (define (julian-day->time-utc jdn)
867 (let ((secs (* sid (- jdn tai-epoch-in-jd))))
868 (receive (seconds parts)
874 (define (julian-day->time-tai jdn)
875 (time-utc->time-tai! (julian-day->time-utc jdn)))
877 (define (julian-day->time-monotonic jdn)
878 (time-utc->time-monotonic! (julian-day->time-utc jdn)))
880 (define (julian-day->date jdn . tz-offset)
881 (let* ((time (julian-day->time-utc jdn))
882 (offset (if (null? tz-offset)
883 (local-tz-offset time)
885 (time-utc->date time offset)))
887 (define (modified-julian-day->date jdn . tz-offset)
888 (apply julian-day->date (+ jdn 4800001/2)
891 (define (modified-julian-day->time-utc jdn)
892 (julian-day->time-utc (+ jdn 4800001/2)))
894 (define (modified-julian-day->time-tai jdn)
895 (julian-day->time-tai (+ jdn 4800001/2)))
897 (define (modified-julian-day->time-monotonic jdn)
898 (julian-day->time-monotonic (+ jdn 4800001/2)))
900 (define (current-julian-day)
901 (time-utc->julian-day (current-time time-utc)))
903 (define (current-modified-julian-day)
904 (time-utc->modified-julian-day (current-time time-utc)))
906 ;; returns a string rep. of number N, of minimum LENGTH, padded with
907 ;; character PAD-WITH. If PAD-WITH is #f, no padding is done, and it's
908 ;; as if number->string was used. if string is longer than or equal
909 ;; in length to LENGTH, it's as if number->string was used.
911 (define (padding n pad-with length)
912 (let* ((str (number->string n))
913 (str-len (string-length str)))
914 (if (or (>= str-len length)
917 (string-append (make-string (- length str-len) pad-with) str))))
919 (define (last-n-digits i n)
920 (abs (remainder i (expt 10 n))))
922 (define (locale-abbr-weekday n) (locale-day-short (+ 1 n)))
923 (define (locale-long-weekday n) (locale-day (+ 1 n)))
924 (define locale-abbr-month locale-month-short)
925 (define locale-long-month locale-month)
927 (define (date-reverse-lookup needle haystack-ref haystack-len
929 ;; Lookup NEEDLE (a string) using HAYSTACK-REF (a one argument procedure
930 ;; that returns a string corresponding to the given index) by passing it
931 ;; indices lower than HAYSTACK-LEN.
932 (let loop ((index 1))
933 (cond ((> index haystack-len) #f)
934 ((same? needle (haystack-ref index))
936 (else (loop (+ index 1))))))
938 (define (locale-abbr-weekday->index string)
939 (date-reverse-lookup string locale-day-short 7 string=?))
941 (define (locale-long-weekday->index string)
942 (date-reverse-lookup string locale-day 7 string=?))
944 (define (locale-abbr-month->index string)
945 (date-reverse-lookup string locale-abbr-month 12 string=?))
947 (define (locale-long-month->index string)
948 (date-reverse-lookup string locale-long-month 12 string=?))
951 ;; FIXME: mkoeppe: Put a symbolic time zone in the date structs.
952 ;; Print it here instead of the numerical offset if available.
953 (define (locale-print-time-zone date port)
954 (tz-printer (date-zone-offset date) port))
956 (define (locale-am-string/pm hr)
957 (if (> hr 11) (locale-pm-string) (locale-am-string)))
959 (define (tz-printer offset port)
961 ((= offset 0) (display "Z" port))
962 ((negative? offset) (display "-" port))
963 (else (display "+" port)))
964 (if (not (= offset 0))
965 (let ((hours (abs (quotient offset (* 60 60))))
966 (minutes (abs (quotient (remainder offset (* 60 60)) 60))))
967 (display (padding hours #\0 2) port)
968 (display (padding minutes #\0 2) port))))
970 ;; A table of output formatting directives.
971 ;; the first time is the format char.
972 ;; the second is a procedure that takes the date, a padding character
973 ;; (which might be #f), and the output port.
977 (cons #\~ (lambda (date pad-with port)
979 (cons #\a (lambda (date pad-with port)
980 (display (locale-abbr-weekday (date-week-day date))
982 (cons #\A (lambda (date pad-with port)
983 (display (locale-long-weekday (date-week-day date))
985 (cons #\b (lambda (date pad-with port)
986 (display (locale-abbr-month (date-month date))
988 (cons #\B (lambda (date pad-with port)
989 (display (locale-long-month (date-month date))
991 (cons #\c (lambda (date pad-with port)
992 (display (date->string date locale-date-time-format) port)))
993 (cons #\d (lambda (date pad-with port)
994 (display (padding (date-day date)
997 (cons #\D (lambda (date pad-with port)
998 (display (date->string date "~m/~d/~y") port)))
999 (cons #\e (lambda (date pad-with port)
1000 (display (padding (date-day date)
1003 (cons #\f (lambda (date pad-with port)
1004 (if (> (date-nanosecond date)
1006 (display (padding (+ (date-second date) 1)
1009 (display (padding (date-second date)
1014 (date-nanosecond date)
1016 (let* ((ns (number->string f))
1017 (le (string-length ns)))
1020 (display (locale-decimal-point) port)
1021 (display (substring ns 2 le) port)))))))
1022 (cons #\h (lambda (date pad-with port)
1023 (display (date->string date "~b") port)))
1024 (cons #\H (lambda (date pad-with port)
1025 (display (padding (date-hour date)
1028 (cons #\I (lambda (date pad-with port)
1029 (let ((hr (date-hour date)))
1031 (display (padding (- hr 12)
1034 (display (padding hr
1037 (cons #\j (lambda (date pad-with port)
1038 (display (padding (date-year-day date)
1041 (cons #\k (lambda (date pad-with port)
1042 (display (padding (date-hour date)
1045 (cons #\l (lambda (date pad-with port)
1046 (let ((hr (if (> (date-hour date) 12)
1047 (- (date-hour date) 12) (date-hour date))))
1048 (display (padding hr #\Space 2)
1050 (cons #\m (lambda (date pad-with port)
1051 (display (padding (date-month date)
1054 (cons #\M (lambda (date pad-with port)
1055 (display (padding (date-minute date)
1058 (cons #\n (lambda (date pad-with port)
1060 (cons #\N (lambda (date pad-with port)
1061 (display (padding (date-nanosecond date)
1064 (cons #\p (lambda (date pad-with port)
1065 (display (locale-am-string/pm (date-hour date)) port)))
1066 (cons #\r (lambda (date pad-with port)
1067 (display (date->string date "~I:~M:~S ~p") port)))
1068 (cons #\s (lambda (date pad-with port)
1069 (display (time-second (date->time-utc date)) port)))
1070 (cons #\S (lambda (date pad-with port)
1071 (if (> (date-nanosecond date)
1073 (display (padding (+ (date-second date) 1)
1076 (display (padding (date-second date)
1079 (cons #\t (lambda (date pad-with port)
1080 (display #\Tab port)))
1081 (cons #\T (lambda (date pad-with port)
1082 (display (date->string date "~H:~M:~S") port)))
1083 (cons #\U (lambda (date pad-with port)
1084 (if (> (days-before-first-week date 0) 0)
1085 (display (padding (+ (date-week-number date 0) 1)
1087 (display (padding (date-week-number date 0)
1089 (cons #\V (lambda (date pad-with port)
1090 (display (padding (date-week-number date 1)
1092 (cons #\w (lambda (date pad-with port)
1093 (display (date-week-day date) port)))
1094 (cons #\x (lambda (date pad-with port)
1095 (display (date->string date locale-short-date-format) port)))
1096 (cons #\X (lambda (date pad-with port)
1097 (display (date->string date locale-time-format) port)))
1098 (cons #\W (lambda (date pad-with port)
1099 (if (> (days-before-first-week date 1) 0)
1100 (display (padding (+ (date-week-number date 1) 1)
1102 (display (padding (date-week-number date 1)
1104 (cons #\y (lambda (date pad-with port)
1105 (display (padding (last-n-digits
1110 (cons #\Y (lambda (date pad-with port)
1111 (display (date-year date) port)))
1112 (cons #\z (lambda (date pad-with port)
1113 (tz-printer (date-zone-offset date) port)))
1114 (cons #\Z (lambda (date pad-with port)
1115 (locale-print-time-zone date port)))
1116 (cons #\1 (lambda (date pad-with port)
1117 (display (date->string date "~Y-~m-~d") port)))
1118 (cons #\2 (lambda (date pad-with port)
1119 (display (date->string date "~H:~M:~S~z") port)))
1120 (cons #\3 (lambda (date pad-with port)
1121 (display (date->string date "~H:~M:~S") port)))
1122 (cons #\4 (lambda (date pad-with port)
1123 (display (date->string date "~Y-~m-~dT~H:~M:~S~z") port)))
1124 (cons #\5 (lambda (date pad-with port)
1125 (display (date->string date "~Y-~m-~dT~H:~M:~S") port)))))
1128 (define (get-formatter char)
1129 (let ((associated (assoc char directives)))
1130 (if associated (cdr associated) #f)))
1132 (define (date-printer date index format-string str-len port)
1133 (if (< index str-len)
1134 (let ((current-char (string-ref format-string index)))
1135 (if (not (char=? current-char #\~))
1137 (display current-char port)
1138 (date-printer date (+ index 1) format-string str-len port))
1139 (if (= (+ index 1) str-len) ; bad format string.
1140 (time-error 'date-printer 'bad-date-format-string
1142 (let ((pad-char? (string-ref format-string (+ index 1))))
1144 ((char=? pad-char? #\-)
1145 (if (= (+ index 2) str-len) ; bad format string.
1146 (time-error 'date-printer
1147 'bad-date-format-string
1149 (let ((formatter (get-formatter
1150 (string-ref format-string
1153 (time-error 'date-printer
1154 'bad-date-format-string
1157 (formatter date #f port)
1164 ((char=? pad-char? #\_)
1165 (if (= (+ index 2) str-len) ; bad format string.
1166 (time-error 'date-printer
1167 'bad-date-format-string
1169 (let ((formatter (get-formatter
1170 (string-ref format-string
1173 (time-error 'date-printer
1174 'bad-date-format-string
1177 (formatter date #\Space port)
1184 (let ((formatter (get-formatter
1185 (string-ref format-string
1188 (time-error 'date-printer
1189 'bad-date-format-string
1192 (formatter date #\0 port)
1200 (define (date->string date . format-string)
1201 (let ((str-port (open-output-string))
1202 (fmt-str (if (null? format-string) "~c" (car format-string))))
1203 (date-printer date 0 fmt-str (string-length fmt-str) str-port)
1204 (get-output-string str-port)))
1206 (define (char->int ch)
1218 (else (time-error 'char->int 'bad-date-template-string
1219 (list "Non-integer character" ch)))))
1221 ;; read an integer upto n characters long on port; upto -> #f is any length
1222 (define (integer-reader upto port)
1223 (let loop ((accum 0) (nchars 0))
1224 (let ((ch (peek-char port)))
1225 (if (or (eof-object? ch)
1226 (not (char-numeric? ch))
1227 (and upto (>= nchars upto)))
1229 (loop (+ (* accum 10) (char->int (read-char port)))
1232 (define (make-integer-reader upto)
1234 (integer-reader upto port)))
1236 ;; read *exactly* n characters and convert to integer; could be padded
1237 (define (integer-reader-exact n port)
1238 (let ((padding-ok #t))
1239 (define (accum-int port accum nchars)
1240 (let ((ch (peek-char port)))
1242 ((>= nchars n) accum)
1244 (time-error 'string->date 'bad-date-template-string
1245 "Premature ending to integer read."))
1247 (set! padding-ok #f)
1249 (+ (* accum 10) (char->int (read-char port)))
1252 (read-char port) ; consume padding
1253 (accum-int port accum (+ nchars 1)))
1254 (else ; padding where it shouldn't be
1255 (time-error 'string->date 'bad-date-template-string
1256 "Non-numeric characters in integer read.")))))
1257 (accum-int port 0 0)))
1260 (define (make-integer-exact-reader n)
1262 (integer-reader-exact n port)))
1264 (define (zone-reader port)
1267 (let ((ch (read-char port)))
1268 (if (eof-object? ch)
1269 (time-error 'string->date 'bad-date-template-string
1270 (list "Invalid time zone +/-" ch)))
1271 (if (or (char=? ch #\Z) (char=? ch #\z))
1275 ((char=? ch #\+) (set! positive? #t))
1276 ((char=? ch #\-) (set! positive? #f))
1278 (time-error 'string->date 'bad-date-template-string
1279 (list "Invalid time zone +/-" ch))))
1280 (let ((ch (read-char port)))
1281 (if (eof-object? ch)
1282 (time-error 'string->date 'bad-date-template-string
1283 (list "Invalid time zone number" ch)))
1284 (set! offset (* (char->int ch)
1286 (let ((ch (read-char port)))
1287 (if (eof-object? ch)
1288 (time-error 'string->date 'bad-date-template-string
1289 (list "Invalid time zone number" ch)))
1290 (set! offset (+ offset (* (char->int ch)
1292 (let ((ch (read-char port)))
1293 (if (eof-object? ch)
1294 (time-error 'string->date 'bad-date-template-string
1295 (list "Invalid time zone number" ch)))
1296 (set! offset (+ offset (* (char->int ch)
1298 (let ((ch (read-char port)))
1299 (if (eof-object? ch)
1300 (time-error 'string->date 'bad-date-template-string
1301 (list "Invalid time zone number" ch)))
1302 (set! offset (+ offset (* (char->int ch)
1304 (if positive? offset (- offset)))))))
1306 ;; looking at a char, read the char string, run thru indexer, return index
1307 (define (locale-reader port indexer)
1309 (define (read-char-string result)
1310 (let ((ch (peek-char port)))
1311 (if (char-alphabetic? ch)
1312 (read-char-string (cons (read-char port) result))
1313 (list->string (reverse! result)))))
1315 (let* ((str (read-char-string '()))
1316 (index (indexer str)))
1317 (if index index (time-error 'string->date
1318 'bad-date-template-string
1319 (list "Invalid string for " indexer)))))
1321 (define (make-locale-reader indexer)
1323 (locale-reader port indexer)))
1325 (define (make-char-id-reader char)
1327 (if (char=? char (read-char port))
1329 (time-error 'string->date
1330 'bad-date-template-string
1331 "Invalid character match."))))
1333 ;; A List of formatted read directives.
1334 ;; Each entry is a list.
1335 ;; 1. the character directive;
1336 ;; a procedure, which takes a character as input & returns
1337 ;; 2. #t as soon as a character on the input port is acceptable
1339 ;; 3. a port reader procedure that knows how to read the current port
1340 ;; for a value. Its one parameter is the port.
1341 ;; 4. an optional action procedure, that takes the value (from 3.) and
1342 ;; some object (here, always the date) and (probably) side-effects it.
1343 ;; If no action is required, as with ~A, this element may be #f.
1345 (define read-directives
1346 (let ((ireader4 (make-integer-reader 4))
1347 (ireader2 (make-integer-reader 2))
1348 (eireader2 (make-integer-exact-reader 2))
1349 (locale-reader-abbr-weekday (make-locale-reader
1350 locale-abbr-weekday->index))
1351 (locale-reader-long-weekday (make-locale-reader
1352 locale-long-weekday->index))
1353 (locale-reader-abbr-month (make-locale-reader
1354 locale-abbr-month->index))
1355 (locale-reader-long-month (make-locale-reader
1356 locale-long-month->index))
1357 (char-fail (lambda (ch) #t)))
1360 (list #\~ char-fail (make-char-id-reader #\~) #f)
1361 (list #\a char-alphabetic? locale-reader-abbr-weekday #f)
1362 (list #\A char-alphabetic? locale-reader-long-weekday #f)
1363 (list #\b char-alphabetic? locale-reader-abbr-month
1364 (lambda (val object)
1365 (set-date-month! object val)))
1366 (list #\B char-alphabetic? locale-reader-long-month
1367 (lambda (val object)
1368 (set-date-month! object val)))
1369 (list #\d char-numeric? ireader2 (lambda (val object)
1372 (list #\e char-fail eireader2 (lambda (val object)
1373 (set-date-day! object val)))
1374 (list #\h char-alphabetic? locale-reader-abbr-month
1375 (lambda (val object)
1376 (set-date-month! object val)))
1377 (list #\H char-numeric? ireader2 (lambda (val object)
1378 (set-date-hour! object val)))
1379 (list #\k char-fail eireader2 (lambda (val object)
1380 (set-date-hour! object val)))
1381 (list #\m char-numeric? ireader2 (lambda (val object)
1382 (set-date-month! object val)))
1383 (list #\M char-numeric? ireader2 (lambda (val object)
1386 (list #\S char-numeric? ireader2 (lambda (val object)
1387 (set-date-second! object val)))
1388 (list #\y char-fail eireader2
1389 (lambda (val object)
1390 (set-date-year! object (natural-year val))))
1391 (list #\Y char-numeric? ireader4 (lambda (val object)
1392 (set-date-year! object val)))
1393 (list #\z (lambda (c)
1398 zone-reader (lambda (val object)
1399 (set-date-zone-offset! object val))))))
1401 (define (priv:string->date date index format-string str-len port template-string)
1402 (define (skip-until port skipper)
1403 (let ((ch (peek-char port)))
1404 (if (eof-object? ch)
1405 (time-error 'string->date 'bad-date-format-string template-string)
1406 (if (not (skipper ch))
1407 (begin (read-char port) (skip-until port skipper))))))
1408 (if (< index str-len)
1409 (let ((current-char (string-ref format-string index)))
1410 (if (not (char=? current-char #\~))
1411 (let ((port-char (read-char port)))
1412 (if (or (eof-object? port-char)
1413 (not (char=? current-char port-char)))
1414 (time-error 'string->date
1415 'bad-date-format-string template-string))
1416 (priv:string->date date
1422 ;; otherwise, it's an escape, we hope
1423 (if (> (+ index 1) str-len)
1424 (time-error 'string->date
1425 'bad-date-format-string template-string)
1426 (let* ((format-char (string-ref format-string (+ index 1)))
1427 (format-info (assoc format-char read-directives)))
1428 (if (not format-info)
1429 (time-error 'string->date
1430 'bad-date-format-string template-string)
1432 (let ((skipper (cadr format-info))
1433 (reader (caddr format-info))
1434 (actor (cadddr format-info)))
1435 (skip-until port skipper)
1436 (let ((val (reader port)))
1437 (if (eof-object? val)
1438 (time-error 'string->date
1439 'bad-date-format-string
1441 (if actor (actor val date))))
1442 (priv:string->date date
1447 template-string))))))))))
1449 (define (string->date input-string template-string)
1450 (define (date-ok? date)
1451 (and (date-nanosecond date)
1458 (date-zone-offset date)))
1459 (let ((newdate (make-date 0 0 0 0 #f #f #f #f)))
1460 (priv:string->date newdate
1463 (string-length template-string)
1464 (open-input-string input-string)
1466 (if (not (date-zone-offset newdate))
1468 ;; this is necessary to get DST right -- as far as we can
1469 ;; get it right (think of the double/missing hour in the
1470 ;; night when we are switching between normal time and DST).
1471 (set-date-zone-offset! newdate
1473 (make-time time-utc 0 0)))
1474 (set-date-zone-offset! newdate
1476 (date->time-utc newdate)))))
1477 (if (date-ok? newdate)
1481 'bad-date-format-string
1482 (list "Incomplete date read. " newdate template-string)))))
1484 ;;; srfi-19.scm ends here