c0a27b1a2d952313b64f6e056a60204abc5280ab
[bpt/guile.git] / module / srfi / srfi-19.scm
1 ;;; srfi-19.scm --- Time/Date Library
2
3 ;; Copyright (C) 2001, 2002, 2003, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
4 ;;
5 ;; This library is free software; you can redistribute it and/or
6 ;; modify it under the terms of the GNU Lesser General Public
7 ;; License as published by the Free Software Foundation; either
8 ;; version 3 of the License, or (at your option) any later version.
9 ;;
10 ;; This library is distributed in the hope that it will be useful,
11 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ;; Lesser General Public License for more details.
14 ;;
15 ;; You should have received a copy of the GNU Lesser General Public
16 ;; License along with this library; if not, write to the Free Software
17 ;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
19 ;;; Author: Rob Browning <rlb@cs.utexas.edu>
20 ;;; Originally from SRFI reference implementation by Will Fitzgerald.
21
22 ;;; Commentary:
23
24 ;; This module is fully documented in the Guile Reference Manual.
25
26 ;;; Code:
27
28 ;; FIXME: I haven't checked a decent amount of this code for potential
29 ;; performance improvements, but I suspect that there may be some
30 ;; substantial ones to be realized, esp. in the later "parsing" half
31 ;; of the file, by rewriting the code with use of more Guile native
32 ;; functions that do more work in a "chunk".
33 ;;
34 ;; FIXME: mkoeppe: Time zones are treated a little simplistic in
35 ;; SRFI-19; they are only a numeric offset. Thus, printing time zones
36 ;; (LOCALE-PRINT-TIME-ZONE) can't be implemented sensibly. The
37 ;; functions taking an optional TZ-OFFSET should be extended to take a
38 ;; symbolic time-zone (like "CET"); this string should be stored in
39 ;; the DATE structure.
40
41 (define-module (srfi srfi-19)
42 :use-module (srfi srfi-6)
43 :use-module (srfi srfi-8)
44 :use-module (srfi srfi-9)
45 :autoload (ice-9 rdelim) (read-line)
46 :use-module (ice-9 i18n)
47 :replace (current-time)
48 :export (;; Constants
49 time-duration
50 time-monotonic
51 time-process
52 time-tai
53 time-thread
54 time-utc
55 ;; Current time and clock resolution
56 current-date
57 current-julian-day
58 current-modified-julian-day
59 time-resolution
60 ;; Time object and accessors
61 make-time
62 time?
63 time-type
64 time-nanosecond
65 time-second
66 set-time-type!
67 set-time-nanosecond!
68 set-time-second!
69 copy-time
70 ;; Time comparison procedures
71 time<=?
72 time<?
73 time=?
74 time>=?
75 time>?
76 ;; Time arithmetic procedures
77 time-difference
78 time-difference!
79 add-duration
80 add-duration!
81 subtract-duration
82 subtract-duration!
83 ;; Date object and accessors
84 make-date
85 date?
86 date-nanosecond
87 date-second
88 date-minute
89 date-hour
90 date-day
91 date-month
92 date-year
93 date-zone-offset
94 date-year-day
95 date-week-day
96 date-week-number
97 ;; Time/Date/Julian Day/Modified Julian Day converters
98 date->julian-day
99 date->modified-julian-day
100 date->time-monotonic
101 date->time-tai
102 date->time-utc
103 julian-day->date
104 julian-day->time-monotonic
105 julian-day->time-tai
106 julian-day->time-utc
107 modified-julian-day->date
108 modified-julian-day->time-monotonic
109 modified-julian-day->time-tai
110 modified-julian-day->time-utc
111 time-monotonic->date
112 time-monotonic->julian-day
113 time-monotonic->modified-julian-day
114 time-monotonic->time-tai
115 time-monotonic->time-tai!
116 time-monotonic->time-utc
117 time-monotonic->time-utc!
118 time-tai->date
119 time-tai->julian-day
120 time-tai->modified-julian-day
121 time-tai->time-monotonic
122 time-tai->time-monotonic!
123 time-tai->time-utc
124 time-tai->time-utc!
125 time-utc->date
126 time-utc->julian-day
127 time-utc->modified-julian-day
128 time-utc->time-monotonic
129 time-utc->time-monotonic!
130 time-utc->time-tai
131 time-utc->time-tai!
132 ;; Date to string/string to date converters.
133 date->string
134 string->date))
135
136 (cond-expand-provide (current-module) '(srfi-19))
137
138 (define time-tai 'time-tai)
139 (define time-utc 'time-utc)
140 (define time-monotonic 'time-monotonic)
141 (define time-thread 'time-thread)
142 (define time-process 'time-process)
143 (define time-duration 'time-duration)
144
145 ;; FIXME: do we want to add gc time?
146 ;; (define time-gc 'time-gc)
147
148 ;;-- LOCALE dependent constants
149
150 ;; See date->string
151 (define locale-date-time-format "~a ~b ~d ~H:~M:~S~z ~Y")
152 (define locale-short-date-format "~m/~d/~y")
153 (define locale-time-format "~H:~M:~S")
154 (define iso-8601-date-time-format "~Y-~m-~dT~H:~M:~S~z")
155
156 ;;-- Miscellaneous Constants.
157 ;;-- only the tai-epoch-in-jd might need changing if
158 ;; a different epoch is used.
159
160 (define nano 1000000000) ; nanoseconds in a second
161 (define sid 86400) ; seconds in a day
162 (define sihd 43200) ; seconds in a half day
163 (define tai-epoch-in-jd 4881175/2) ; julian day number for 'the epoch'
164
165 ;; FIXME: should this be something other than misc-error?
166 (define (time-error caller type value)
167 (if value
168 (throw 'misc-error caller "TIME-ERROR type ~A: ~S" (list type value) #f)
169 (throw 'misc-error caller "TIME-ERROR type ~A" (list type) #f)))
170
171 ;; A table of leap seconds
172 ;; See ftp://maia.usno.navy.mil/ser7/tai-utc.dat
173 ;; and update as necessary.
174 ;; this procedures reads the file in the abover
175 ;; format and creates the leap second table
176 ;; it also calls the almost standard, but not R5 procedures read-line
177 ;; & open-input-string
178 ;; ie (set! leap-second-table (read-tai-utc-date "tai-utc.dat"))
179
180 (define (read-tai-utc-data filename)
181 (define (convert-jd jd)
182 (* (- (inexact->exact jd) tai-epoch-in-jd) sid))
183 (define (convert-sec sec)
184 (inexact->exact sec))
185 (let ((port (open-input-file filename))
186 (table '()))
187 (let loop ((line (read-line port)))
188 (if (not (eof-object? line))
189 (begin
190 (let* ((data (read (open-input-string
191 (string-append "(" line ")"))))
192 (year (car data))
193 (jd (cadddr (cdr data)))
194 (secs (cadddr (cdddr data))))
195 (if (>= year 1972)
196 (set! table (cons
197 (cons (convert-jd jd) (convert-sec secs))
198 table)))
199 (loop (read-line port))))))
200 table))
201
202 ;; each entry is (tai seconds since epoch . # seconds to subtract for utc)
203 ;; note they go higher to lower, and end in 1972.
204 (define leap-second-table
205 '((1136073600 . 33)
206 (915148800 . 32)
207 (867715200 . 31)
208 (820454400 . 30)
209 (773020800 . 29)
210 (741484800 . 28)
211 (709948800 . 27)
212 (662688000 . 26)
213 (631152000 . 25)
214 (567993600 . 24)
215 (489024000 . 23)
216 (425865600 . 22)
217 (394329600 . 21)
218 (362793600 . 20)
219 (315532800 . 19)
220 (283996800 . 18)
221 (252460800 . 17)
222 (220924800 . 16)
223 (189302400 . 15)
224 (157766400 . 14)
225 (126230400 . 13)
226 (94694400 . 12)
227 (78796800 . 11)
228 (63072000 . 10)))
229
230 (define (read-leap-second-table filename)
231 (set! leap-second-table (read-tai-utc-data filename)))
232
233
234 (define (leap-second-delta utc-seconds)
235 (letrec ((lsd (lambda (table)
236 (cond ((>= utc-seconds (caar table))
237 (cdar table))
238 (else (lsd (cdr table)))))))
239 (if (< utc-seconds (* (- 1972 1970) 365 sid)) 0
240 (lsd leap-second-table))))
241
242
243 ;;; the TIME structure; creates the accessors, too.
244
245 (define-record-type time
246 (make-time-unnormalized type nanosecond second)
247 time?
248 (type time-type set-time-type!)
249 (nanosecond time-nanosecond set-time-nanosecond!)
250 (second time-second set-time-second!))
251
252 (define (copy-time time)
253 (make-time (time-type time) (time-nanosecond time) (time-second time)))
254
255 (define (split-real r)
256 (if (integer? r)
257 (values (inexact->exact r) 0)
258 (let ((l (truncate r)))
259 (values (inexact->exact l) (- r l)))))
260
261 (define (time-normalize! t)
262 (if (>= (abs (time-nanosecond t)) 1000000000)
263 (receive (int frac)
264 (split-real (time-nanosecond t))
265 (set-time-second! t (+ (time-second t)
266 (quotient int 1000000000)))
267 (set-time-nanosecond! t (+ (remainder int 1000000000)
268 frac))))
269 (if (and (positive? (time-second t))
270 (negative? (time-nanosecond t)))
271 (begin
272 (set-time-second! t (- (time-second t) 1))
273 (set-time-nanosecond! t (+ 1000000000 (time-nanosecond t))))
274 (if (and (negative? (time-second t))
275 (positive? (time-nanosecond t)))
276 (begin
277 (set-time-second! t (+ (time-second t) 1))
278 (set-time-nanosecond! t (+ 1000000000 (time-nanosecond t))))))
279 t)
280
281 (define (make-time type nanosecond second)
282 (time-normalize! (make-time-unnormalized type nanosecond second)))
283
284 ;; Helpers
285 ;; FIXME: finish this and publish it?
286 (define (date->broken-down-time date)
287 (let ((result (mktime 0)))
288 ;; FIXME: What should we do about leap-seconds which may overflow
289 ;; set-tm:sec?
290 (set-tm:sec result (date-second date))
291 (set-tm:min result (date-minute date))
292 (set-tm:hour result (date-hour date))
293 ;; FIXME: SRFI day ranges from 0-31. (not compatible with set-tm:mday).
294 (set-tm:mday result (date-day date))
295 (set-tm:mon result (- (date-month date) 1))
296 ;; FIXME: need to signal error on range violation.
297 (set-tm:year result (+ 1900 (date-year date)))
298 (set-tm:isdst result -1)
299 (set-tm:gmtoff result (- (date-zone-offset date)))
300 result))
301
302 ;;; current-time
303
304 ;;; specific time getters.
305
306 (define (current-time-utc)
307 ;; Resolution is microseconds.
308 (let ((tod (gettimeofday)))
309 (make-time time-utc (* (cdr tod) 1000) (car tod))))
310
311 (define (current-time-tai)
312 ;; Resolution is microseconds.
313 (let* ((tod (gettimeofday))
314 (sec (car tod))
315 (usec (cdr tod)))
316 (make-time time-tai
317 (* usec 1000)
318 (+ (car tod) (leap-second-delta sec)))))
319
320 ;;(define (current-time-ms-time time-type proc)
321 ;; (let ((current-ms (proc)))
322 ;; (make-time time-type
323 ;; (quotient current-ms 10000)
324 ;; (* (remainder current-ms 1000) 10000))))
325
326 ;; -- we define it to be the same as TAI.
327 ;; A different implemation of current-time-montonic
328 ;; will require rewriting all of the time-monotonic converters,
329 ;; of course.
330
331 (define (current-time-monotonic)
332 ;; Resolution is microseconds.
333 (current-time-tai))
334
335 (define (current-time-thread)
336 (time-error 'current-time 'unsupported-clock-type 'time-thread))
337
338 (define ns-per-guile-tick (/ 1000000000 internal-time-units-per-second))
339
340 (define (current-time-process)
341 (let ((run-time (get-internal-run-time)))
342 (make-time
343 time-process
344 (* (remainder run-time internal-time-units-per-second)
345 ns-per-guile-tick)
346 (quotient run-time internal-time-units-per-second))))
347
348 ;;(define (current-time-gc)
349 ;; (current-time-ms-time time-gc current-gc-milliseconds))
350
351 (define (current-time . clock-type)
352 (let ((clock-type (if (null? clock-type) time-utc (car clock-type))))
353 (cond
354 ((eq? clock-type time-tai) (current-time-tai))
355 ((eq? clock-type time-utc) (current-time-utc))
356 ((eq? clock-type time-monotonic) (current-time-monotonic))
357 ((eq? clock-type time-thread) (current-time-thread))
358 ((eq? clock-type time-process) (current-time-process))
359 ;; ((eq? clock-type time-gc) (current-time-gc))
360 (else (time-error 'current-time 'invalid-clock-type clock-type)))))
361
362 ;; -- Time Resolution
363 ;; This is the resolution of the clock in nanoseconds.
364 ;; This will be implementation specific.
365
366 (define (time-resolution . clock-type)
367 (let ((clock-type (if (null? clock-type) time-utc (car clock-type))))
368 (case clock-type
369 ((time-tai) 1000)
370 ((time-utc) 1000)
371 ((time-monotonic) 1000)
372 ((time-process) ns-per-guile-tick)
373 ;; ((eq? clock-type time-thread) 1000)
374 ;; ((eq? clock-type time-gc) 10000)
375 (else (time-error 'time-resolution 'invalid-clock-type clock-type)))))
376
377 ;; -- Time comparisons
378
379 (define (time=? t1 t2)
380 ;; Arrange tests for speed and presume that t1 and t2 are actually times.
381 ;; also presume it will be rare to check two times of different types.
382 (and (= (time-second t1) (time-second t2))
383 (= (time-nanosecond t1) (time-nanosecond t2))
384 (eq? (time-type t1) (time-type t2))))
385
386 (define (time>? t1 t2)
387 (or (> (time-second t1) (time-second t2))
388 (and (= (time-second t1) (time-second t2))
389 (> (time-nanosecond t1) (time-nanosecond t2)))))
390
391 (define (time<? t1 t2)
392 (or (< (time-second t1) (time-second t2))
393 (and (= (time-second t1) (time-second t2))
394 (< (time-nanosecond t1) (time-nanosecond t2)))))
395
396 (define (time>=? t1 t2)
397 (or (> (time-second t1) (time-second t2))
398 (and (= (time-second t1) (time-second t2))
399 (>= (time-nanosecond t1) (time-nanosecond t2)))))
400
401 (define (time<=? t1 t2)
402 (or (< (time-second t1) (time-second t2))
403 (and (= (time-second t1) (time-second t2))
404 (<= (time-nanosecond t1) (time-nanosecond t2)))))
405
406 ;; -- Time arithmetic
407
408 (define (time-difference! time1 time2)
409 (let ((sec-diff (- (time-second time1) (time-second time2)))
410 (nsec-diff (- (time-nanosecond time1) (time-nanosecond time2))))
411 (set-time-type! time1 time-duration)
412 (set-time-second! time1 sec-diff)
413 (set-time-nanosecond! time1 nsec-diff)
414 (time-normalize! time1)))
415
416 (define (time-difference time1 time2)
417 (let ((result (copy-time time1)))
418 (time-difference! result time2)))
419
420 (define (add-duration! t duration)
421 (if (not (eq? (time-type duration) time-duration))
422 (time-error 'add-duration 'not-duration duration)
423 (let ((sec-plus (+ (time-second t) (time-second duration)))
424 (nsec-plus (+ (time-nanosecond t) (time-nanosecond duration))))
425 (set-time-second! t sec-plus)
426 (set-time-nanosecond! t nsec-plus)
427 (time-normalize! t))))
428
429 (define (add-duration t duration)
430 (let ((result (copy-time t)))
431 (add-duration! result duration)))
432
433 (define (subtract-duration! t duration)
434 (if (not (eq? (time-type duration) time-duration))
435 (time-error 'add-duration 'not-duration duration)
436 (let ((sec-minus (- (time-second t) (time-second duration)))
437 (nsec-minus (- (time-nanosecond t) (time-nanosecond duration))))
438 (set-time-second! t sec-minus)
439 (set-time-nanosecond! t nsec-minus)
440 (time-normalize! t))))
441
442 (define (subtract-duration time1 duration)
443 (let ((result (copy-time time1)))
444 (subtract-duration! result duration)))
445
446 ;; -- Converters between types.
447
448 (define (priv:time-tai->time-utc! time-in time-out caller)
449 (if (not (eq? (time-type time-in) time-tai))
450 (time-error caller 'incompatible-time-types time-in))
451 (set-time-type! time-out time-utc)
452 (set-time-nanosecond! time-out (time-nanosecond time-in))
453 (set-time-second! time-out (- (time-second time-in)
454 (leap-second-delta
455 (time-second time-in))))
456 time-out)
457
458 (define (time-tai->time-utc time-in)
459 (priv:time-tai->time-utc! time-in (make-time-unnormalized #f #f #f) 'time-tai->time-utc))
460
461
462 (define (time-tai->time-utc! time-in)
463 (priv:time-tai->time-utc! time-in time-in 'time-tai->time-utc!))
464
465 (define (priv:time-utc->time-tai! time-in time-out caller)
466 (if (not (eq? (time-type time-in) time-utc))
467 (time-error caller 'incompatible-time-types time-in))
468 (set-time-type! time-out time-tai)
469 (set-time-nanosecond! time-out (time-nanosecond time-in))
470 (set-time-second! time-out (+ (time-second time-in)
471 (leap-second-delta
472 (time-second time-in))))
473 time-out)
474
475 (define (time-utc->time-tai time-in)
476 (priv:time-utc->time-tai! time-in (make-time-unnormalized #f #f #f) 'time-utc->time-tai))
477
478 (define (time-utc->time-tai! time-in)
479 (priv:time-utc->time-tai! time-in time-in 'time-utc->time-tai!))
480
481 ;; -- these depend on time-monotonic having the same definition as time-tai!
482 (define (time-monotonic->time-utc time-in)
483 (if (not (eq? (time-type time-in) time-monotonic))
484 (time-error 'time-monotonic->time-utc
485 'incompatible-time-types time-in))
486 (let ((ntime (copy-time time-in)))
487 (set-time-type! ntime time-tai)
488 (priv:time-tai->time-utc! ntime ntime 'time-monotonic->time-utc)))
489
490 (define (time-monotonic->time-utc! time-in)
491 (if (not (eq? (time-type time-in) time-monotonic))
492 (time-error 'time-monotonic->time-utc!
493 'incompatible-time-types time-in))
494 (set-time-type! time-in time-tai)
495 (priv:time-tai->time-utc! time-in time-in 'time-monotonic->time-utc))
496
497 (define (time-monotonic->time-tai time-in)
498 (if (not (eq? (time-type time-in) time-monotonic))
499 (time-error 'time-monotonic->time-tai
500 'incompatible-time-types time-in))
501 (let ((ntime (copy-time time-in)))
502 (set-time-type! ntime time-tai)
503 ntime))
504
505 (define (time-monotonic->time-tai! time-in)
506 (if (not (eq? (time-type time-in) time-monotonic))
507 (time-error 'time-monotonic->time-tai!
508 'incompatible-time-types time-in))
509 (set-time-type! time-in time-tai)
510 time-in)
511
512 (define (time-utc->time-monotonic time-in)
513 (if (not (eq? (time-type time-in) time-utc))
514 (time-error 'time-utc->time-monotonic
515 'incompatible-time-types time-in))
516 (let ((ntime (priv:time-utc->time-tai! time-in (make-time-unnormalized #f #f #f)
517 'time-utc->time-monotonic)))
518 (set-time-type! ntime time-monotonic)
519 ntime))
520
521 (define (time-utc->time-monotonic! time-in)
522 (if (not (eq? (time-type time-in) time-utc))
523 (time-error 'time-utc->time-monotonic!
524 'incompatible-time-types time-in))
525 (let ((ntime (priv:time-utc->time-tai! time-in time-in
526 'time-utc->time-monotonic!)))
527 (set-time-type! ntime time-monotonic)
528 ntime))
529
530 (define (time-tai->time-monotonic time-in)
531 (if (not (eq? (time-type time-in) time-tai))
532 (time-error 'time-tai->time-monotonic
533 'incompatible-time-types time-in))
534 (let ((ntime (copy-time time-in)))
535 (set-time-type! ntime time-monotonic)
536 ntime))
537
538 (define (time-tai->time-monotonic! time-in)
539 (if (not (eq? (time-type time-in) time-tai))
540 (time-error 'time-tai->time-monotonic!
541 'incompatible-time-types time-in))
542 (set-time-type! time-in time-monotonic)
543 time-in)
544
545 ;; -- Date Structures
546
547 ;; FIXME: to be really safe, perhaps we should normalize the
548 ;; seconds/nanoseconds/minutes coming in to make-date...
549
550 (define-record-type date
551 (make-date nanosecond second minute
552 hour day month
553 year
554 zone-offset)
555 date?
556 (nanosecond date-nanosecond set-date-nanosecond!)
557 (second date-second set-date-second!)
558 (minute date-minute set-date-minute!)
559 (hour date-hour set-date-hour!)
560 (day date-day set-date-day!)
561 (month date-month set-date-month!)
562 (year date-year set-date-year!)
563 (zone-offset date-zone-offset set-date-zone-offset!))
564
565 ;; gives the julian day which starts at noon.
566 (define (encode-julian-day-number day month year)
567 (let* ((a (quotient (- 14 month) 12))
568 (y (- (+ year 4800) a (if (negative? year) -1 0)))
569 (m (- (+ month (* 12 a)) 3)))
570 (+ day
571 (quotient (+ (* 153 m) 2) 5)
572 (* 365 y)
573 (quotient y 4)
574 (- (quotient y 100))
575 (quotient y 400)
576 -32045)))
577
578 ;; gives the seconds/date/month/year
579 (define (decode-julian-day-number jdn)
580 (let* ((days (inexact->exact (truncate jdn)))
581 (a (+ days 32044))
582 (b (quotient (+ (* 4 a) 3) 146097))
583 (c (- a (quotient (* 146097 b) 4)))
584 (d (quotient (+ (* 4 c) 3) 1461))
585 (e (- c (quotient (* 1461 d) 4)))
586 (m (quotient (+ (* 5 e) 2) 153))
587 (y (+ (* 100 b) d -4800 (quotient m 10))))
588 (values ; seconds date month year
589 (* (- jdn days) sid)
590 (+ e (- (quotient (+ (* 153 m) 2) 5)) 1)
591 (+ m 3 (* -12 (quotient m 10)))
592 (if (>= 0 y) (- y 1) y))))
593
594 ;; relies on the fact that we named our time zone accessor
595 ;; differently from MzScheme's....
596 ;; This should be written to be OS specific.
597
598 (define (local-tz-offset utc-time)
599 ;; SRFI uses seconds West, but guile (and libc) use seconds East.
600 (- (tm:gmtoff (localtime (time-second utc-time)))))
601
602 ;; special thing -- ignores nanos
603 (define (time->julian-day-number seconds tz-offset)
604 (+ (/ (+ seconds tz-offset sihd)
605 sid)
606 tai-epoch-in-jd))
607
608 (define (leap-second? second)
609 (and (assoc second leap-second-table) #t))
610
611 (define (time-utc->date time . tz-offset)
612 (if (not (eq? (time-type time) time-utc))
613 (time-error 'time->date 'incompatible-time-types time))
614 (let* ((offset (if (null? tz-offset)
615 (local-tz-offset time)
616 (car tz-offset)))
617 (leap-second? (leap-second? (+ offset (time-second time))))
618 (jdn (time->julian-day-number (if leap-second?
619 (- (time-second time) 1)
620 (time-second time))
621 offset)))
622
623 (call-with-values (lambda () (decode-julian-day-number jdn))
624 (lambda (secs date month year)
625 ;; secs is a real because jdn is a real in Guile;
626 ;; but it is conceptionally an integer.
627 (let* ((int-secs (inexact->exact (round secs)))
628 (hours (quotient int-secs (* 60 60)))
629 (rem (remainder int-secs (* 60 60)))
630 (minutes (quotient rem 60))
631 (seconds (remainder rem 60)))
632 (make-date (time-nanosecond time)
633 (if leap-second? (+ seconds 1) seconds)
634 minutes
635 hours
636 date
637 month
638 year
639 offset))))))
640
641 (define (time-tai->date time . tz-offset)
642 (if (not (eq? (time-type time) time-tai))
643 (time-error 'time->date 'incompatible-time-types time))
644 (let* ((offset (if (null? tz-offset)
645 (local-tz-offset (time-tai->time-utc time))
646 (car tz-offset)))
647 (seconds (- (time-second time)
648 (leap-second-delta (time-second time))))
649 (leap-second? (leap-second? (+ offset seconds)))
650 (jdn (time->julian-day-number (if leap-second?
651 (- seconds 1)
652 seconds)
653 offset)))
654 (call-with-values (lambda () (decode-julian-day-number jdn))
655 (lambda (secs date month year)
656 ;; secs is a real because jdn is a real in Guile;
657 ;; but it is conceptionally an integer.
658 ;; adjust for leap seconds if necessary ...
659 (let* ((int-secs (inexact->exact (round secs)))
660 (hours (quotient int-secs (* 60 60)))
661 (rem (remainder int-secs (* 60 60)))
662 (minutes (quotient rem 60))
663 (seconds (remainder rem 60)))
664 (make-date (time-nanosecond time)
665 (if leap-second? (+ seconds 1) seconds)
666 minutes
667 hours
668 date
669 month
670 year
671 offset))))))
672
673 ;; this is the same as time-tai->date.
674 (define (time-monotonic->date time . tz-offset)
675 (if (not (eq? (time-type time) time-monotonic))
676 (time-error 'time->date 'incompatible-time-types time))
677 (let* ((offset (if (null? tz-offset)
678 (local-tz-offset (time-monotonic->time-utc time))
679 (car tz-offset)))
680 (seconds (- (time-second time)
681 (leap-second-delta (time-second time))))
682 (leap-second? (leap-second? (+ offset seconds)))
683 (jdn (time->julian-day-number (if leap-second?
684 (- seconds 1)
685 seconds)
686 offset)))
687 (call-with-values (lambda () (decode-julian-day-number jdn))
688 (lambda (secs date month year)
689 ;; secs is a real because jdn is a real in Guile;
690 ;; but it is conceptionally an integer.
691 ;; adjust for leap seconds if necessary ...
692 (let* ((int-secs (inexact->exact (round secs)))
693 (hours (quotient int-secs (* 60 60)))
694 (rem (remainder int-secs (* 60 60)))
695 (minutes (quotient rem 60))
696 (seconds (remainder rem 60)))
697 (make-date (time-nanosecond time)
698 (if leap-second? (+ seconds 1) seconds)
699 minutes
700 hours
701 date
702 month
703 year
704 offset))))))
705
706 (define (date->time-utc date)
707 (let* ((jdays (- (encode-julian-day-number (date-day date)
708 (date-month date)
709 (date-year date))
710 tai-epoch-in-jd))
711 ;; jdays is an integer plus 1/2,
712 (jdays-1/2 (inexact->exact (- jdays 1/2))))
713 (make-time
714 time-utc
715 (date-nanosecond date)
716 (+ (* jdays-1/2 24 60 60)
717 (* (date-hour date) 60 60)
718 (* (date-minute date) 60)
719 (date-second date)
720 (- (date-zone-offset date))))))
721
722 (define (date->time-tai date)
723 (time-utc->time-tai! (date->time-utc date)))
724
725 (define (date->time-monotonic date)
726 (time-utc->time-monotonic! (date->time-utc date)))
727
728 (define (leap-year? year)
729 (or (= (modulo year 400) 0)
730 (and (= (modulo year 4) 0) (not (= (modulo year 100) 0)))))
731
732 ;; Map 1-based month number M to number of days in the year before the
733 ;; start of month M (in a non-leap year).
734 (define month-assoc '((1 . 0) (2 . 31) (3 . 59) (4 . 90)
735 (5 . 120) (6 . 151) (7 . 181) (8 . 212)
736 (9 . 243) (10 . 273) (11 . 304) (12 . 334)))
737
738 (define (year-day day month year)
739 (let ((days-pr (assoc month month-assoc)))
740 (if (not days-pr)
741 (time-error 'date-year-day 'invalid-month-specification month))
742 (if (and (leap-year? year) (> month 2))
743 (+ day (cdr days-pr) 1)
744 (+ day (cdr days-pr)))))
745
746 (define (date-year-day date)
747 (year-day (date-day date) (date-month date) (date-year date)))
748
749 ;; from calendar faq
750 (define (week-day day month year)
751 (let* ((a (quotient (- 14 month) 12))
752 (y (- year a))
753 (m (+ month (* 12 a) -2)))
754 (modulo (+ day
755 y
756 (quotient y 4)
757 (- (quotient y 100))
758 (quotient y 400)
759 (quotient (* 31 m) 12))
760 7)))
761
762 (define (date-week-day date)
763 (week-day (date-day date) (date-month date) (date-year date)))
764
765 (define (days-before-first-week date day-of-week-starting-week)
766 (let* ((first-day (make-date 0 0 0 0
767 1
768 1
769 (date-year date)
770 #f))
771 (fdweek-day (date-week-day first-day)))
772 (modulo (- day-of-week-starting-week fdweek-day)
773 7)))
774
775 ;; The "-1" here is a fix for the reference implementation, to make a new
776 ;; week start on the given day-of-week-starting-week. date-year-day returns
777 ;; a day starting from 1 for 1st Jan.
778 ;;
779 (define (date-week-number date day-of-week-starting-week)
780 (quotient (- (date-year-day date)
781 1
782 (days-before-first-week date day-of-week-starting-week))
783 7))
784
785 (define (current-date . tz-offset)
786 (let ((time (current-time time-utc)))
787 (time-utc->date
788 time
789 (if (null? tz-offset)
790 (local-tz-offset time)
791 (car tz-offset)))))
792
793 ;; given a 'two digit' number, find the year within 50 years +/-
794 (define (natural-year n)
795 (let* ((current-year (date-year (current-date)))
796 (current-century (* (quotient current-year 100) 100)))
797 (cond
798 ((>= n 100) n)
799 ((< n 0) n)
800 ((<= (- (+ current-century n) current-year) 50) (+ current-century n))
801 (else (+ (- current-century 100) n)))))
802
803 (define (date->julian-day date)
804 (let ((nanosecond (date-nanosecond date))
805 (second (date-second date))
806 (minute (date-minute date))
807 (hour (date-hour date))
808 (day (date-day date))
809 (month (date-month date))
810 (year (date-year date))
811 (offset (date-zone-offset date)))
812 (+ (encode-julian-day-number day month year)
813 (- 1/2)
814 (+ (/ (+ (- offset)
815 (* hour 60 60)
816 (* minute 60)
817 second
818 (/ nanosecond nano))
819 sid)))))
820
821 (define (date->modified-julian-day date)
822 (- (date->julian-day date)
823 4800001/2))
824
825 (define (time-utc->julian-day time)
826 (if (not (eq? (time-type time) time-utc))
827 (time-error 'time->date 'incompatible-time-types time))
828 (+ (/ (+ (time-second time) (/ (time-nanosecond time) nano))
829 sid)
830 tai-epoch-in-jd))
831
832 (define (time-utc->modified-julian-day time)
833 (- (time-utc->julian-day time)
834 4800001/2))
835
836 (define (time-tai->julian-day time)
837 (if (not (eq? (time-type time) time-tai))
838 (time-error 'time->date 'incompatible-time-types time))
839 (+ (/ (+ (- (time-second time)
840 (leap-second-delta (time-second time)))
841 (/ (time-nanosecond time) nano))
842 sid)
843 tai-epoch-in-jd))
844
845 (define (time-tai->modified-julian-day time)
846 (- (time-tai->julian-day time)
847 4800001/2))
848
849 ;; this is the same as time-tai->julian-day
850 (define (time-monotonic->julian-day time)
851 (if (not (eq? (time-type time) time-monotonic))
852 (time-error 'time->date 'incompatible-time-types time))
853 (+ (/ (+ (- (time-second time)
854 (leap-second-delta (time-second time)))
855 (/ (time-nanosecond time) nano))
856 sid)
857 tai-epoch-in-jd))
858
859 (define (time-monotonic->modified-julian-day time)
860 (- (time-monotonic->julian-day time)
861 4800001/2))
862
863 (define (julian-day->time-utc jdn)
864 (let ((secs (* sid (- jdn tai-epoch-in-jd))))
865 (receive (seconds parts)
866 (split-real secs)
867 (make-time time-utc
868 (* parts nano)
869 seconds))))
870
871 (define (julian-day->time-tai jdn)
872 (time-utc->time-tai! (julian-day->time-utc jdn)))
873
874 (define (julian-day->time-monotonic jdn)
875 (time-utc->time-monotonic! (julian-day->time-utc jdn)))
876
877 (define (julian-day->date jdn . tz-offset)
878 (let* ((time (julian-day->time-utc jdn))
879 (offset (if (null? tz-offset)
880 (local-tz-offset time)
881 (car tz-offset))))
882 (time-utc->date time offset)))
883
884 (define (modified-julian-day->date jdn . tz-offset)
885 (apply julian-day->date (+ jdn 4800001/2)
886 tz-offset))
887
888 (define (modified-julian-day->time-utc jdn)
889 (julian-day->time-utc (+ jdn 4800001/2)))
890
891 (define (modified-julian-day->time-tai jdn)
892 (julian-day->time-tai (+ jdn 4800001/2)))
893
894 (define (modified-julian-day->time-monotonic jdn)
895 (julian-day->time-monotonic (+ jdn 4800001/2)))
896
897 (define (current-julian-day)
898 (time-utc->julian-day (current-time time-utc)))
899
900 (define (current-modified-julian-day)
901 (time-utc->modified-julian-day (current-time time-utc)))
902
903 ;; returns a string rep. of number N, of minimum LENGTH, padded with
904 ;; character PAD-WITH. If PAD-WITH is #f, no padding is done, and it's
905 ;; as if number->string was used. if string is longer than or equal
906 ;; in length to LENGTH, it's as if number->string was used.
907
908 (define (padding n pad-with length)
909 (let* ((str (number->string n))
910 (str-len (string-length str)))
911 (if (or (>= str-len length)
912 (not pad-with))
913 str
914 (string-append (make-string (- length str-len) pad-with) str))))
915
916 (define (last-n-digits i n)
917 (abs (remainder i (expt 10 n))))
918
919 (define (locale-abbr-weekday n) (locale-day-short (+ 1 n)))
920 (define (locale-long-weekday n) (locale-day (+ 1 n)))
921 (define locale-abbr-month locale-month-short)
922 (define locale-long-month locale-month)
923
924 (define (date-reverse-lookup needle haystack-ref haystack-len
925 same?)
926 ;; Lookup NEEDLE (a string) using HAYSTACK-REF (a one argument procedure
927 ;; that returns a string corresponding to the given index) by passing it
928 ;; indices lower than HAYSTACK-LEN.
929 (let loop ((index 1))
930 (cond ((> index haystack-len) #f)
931 ((same? needle (haystack-ref index))
932 index)
933 (else (loop (+ index 1))))))
934
935 (define (locale-abbr-weekday->index string)
936 (date-reverse-lookup string locale-day-short 7 string=?))
937
938 (define (locale-long-weekday->index string)
939 (date-reverse-lookup string locale-day 7 string=?))
940
941 (define (locale-abbr-month->index string)
942 (date-reverse-lookup string locale-abbr-month 12 string=?))
943
944 (define (locale-long-month->index string)
945 (date-reverse-lookup string locale-long-month 12 string=?))
946
947
948 ;; FIXME: mkoeppe: Put a symbolic time zone in the date structs.
949 ;; Print it here instead of the numerical offset if available.
950 (define (locale-print-time-zone date port)
951 (tz-printer (date-zone-offset date) port))
952
953 (define (locale-am-string/pm hr)
954 (if (> hr 11) (locale-pm-string) (locale-am-string)))
955
956 (define (tz-printer offset port)
957 (cond
958 ((= offset 0) (display "Z" port))
959 ((negative? offset) (display "-" port))
960 (else (display "+" port)))
961 (if (not (= offset 0))
962 (let ((hours (abs (quotient offset (* 60 60))))
963 (minutes (abs (quotient (remainder offset (* 60 60)) 60))))
964 (display (padding hours #\0 2) port)
965 (display (padding minutes #\0 2) port))))
966
967 ;; A table of output formatting directives.
968 ;; the first time is the format char.
969 ;; the second is a procedure that takes the date, a padding character
970 ;; (which might be #f), and the output port.
971 ;;
972 (define directives
973 (list
974 (cons #\~ (lambda (date pad-with port)
975 (display #\~ port)))
976 (cons #\a (lambda (date pad-with port)
977 (display (locale-abbr-weekday (date-week-day date))
978 port)))
979 (cons #\A (lambda (date pad-with port)
980 (display (locale-long-weekday (date-week-day date))
981 port)))
982 (cons #\b (lambda (date pad-with port)
983 (display (locale-abbr-month (date-month date))
984 port)))
985 (cons #\B (lambda (date pad-with port)
986 (display (locale-long-month (date-month date))
987 port)))
988 (cons #\c (lambda (date pad-with port)
989 (display (date->string date locale-date-time-format) port)))
990 (cons #\d (lambda (date pad-with port)
991 (display (padding (date-day date)
992 #\0 2)
993 port)))
994 (cons #\D (lambda (date pad-with port)
995 (display (date->string date "~m/~d/~y") port)))
996 (cons #\e (lambda (date pad-with port)
997 (display (padding (date-day date)
998 #\Space 2)
999 port)))
1000 (cons #\f (lambda (date pad-with port)
1001 (if (> (date-nanosecond date)
1002 nano)
1003 (display (padding (+ (date-second date) 1)
1004 pad-with 2)
1005 port)
1006 (display (padding (date-second date)
1007 pad-with 2)
1008 port))
1009 (receive (i f)
1010 (split-real (/
1011 (date-nanosecond date)
1012 nano 1.0))
1013 (let* ((ns (number->string f))
1014 (le (string-length ns)))
1015 (if (> le 2)
1016 (begin
1017 (display (locale-decimal-point) port)
1018 (display (substring ns 2 le) port)))))))
1019 (cons #\h (lambda (date pad-with port)
1020 (display (date->string date "~b") port)))
1021 (cons #\H (lambda (date pad-with port)
1022 (display (padding (date-hour date)
1023 pad-with 2)
1024 port)))
1025 (cons #\I (lambda (date pad-with port)
1026 (let ((hr (date-hour date)))
1027 (if (> hr 12)
1028 (display (padding (- hr 12)
1029 pad-with 2)
1030 port)
1031 (display (padding hr
1032 pad-with 2)
1033 port)))))
1034 (cons #\j (lambda (date pad-with port)
1035 (display (padding (date-year-day date)
1036 pad-with 3)
1037 port)))
1038 (cons #\k (lambda (date pad-with port)
1039 (display (padding (date-hour date)
1040 #\Space 2)
1041 port)))
1042 (cons #\l (lambda (date pad-with port)
1043 (let ((hr (if (> (date-hour date) 12)
1044 (- (date-hour date) 12) (date-hour date))))
1045 (display (padding hr #\Space 2)
1046 port))))
1047 (cons #\m (lambda (date pad-with port)
1048 (display (padding (date-month date)
1049 pad-with 2)
1050 port)))
1051 (cons #\M (lambda (date pad-with port)
1052 (display (padding (date-minute date)
1053 pad-with 2)
1054 port)))
1055 (cons #\n (lambda (date pad-with port)
1056 (newline port)))
1057 (cons #\N (lambda (date pad-with port)
1058 (display (padding (date-nanosecond date)
1059 pad-with 7)
1060 port)))
1061 (cons #\p (lambda (date pad-with port)
1062 (display (locale-am-string/pm (date-hour date)) port)))
1063 (cons #\r (lambda (date pad-with port)
1064 (display (date->string date "~I:~M:~S ~p") port)))
1065 (cons #\s (lambda (date pad-with port)
1066 (display (time-second (date->time-utc date)) port)))
1067 (cons #\S (lambda (date pad-with port)
1068 (if (> (date-nanosecond date)
1069 nano)
1070 (display (padding (+ (date-second date) 1)
1071 pad-with 2)
1072 port)
1073 (display (padding (date-second date)
1074 pad-with 2)
1075 port))))
1076 (cons #\t (lambda (date pad-with port)
1077 (display #\Tab port)))
1078 (cons #\T (lambda (date pad-with port)
1079 (display (date->string date "~H:~M:~S") port)))
1080 (cons #\U (lambda (date pad-with port)
1081 (if (> (days-before-first-week date 0) 0)
1082 (display (padding (+ (date-week-number date 0) 1)
1083 #\0 2) port)
1084 (display (padding (date-week-number date 0)
1085 #\0 2) port))))
1086 (cons #\V (lambda (date pad-with port)
1087 (display (padding (date-week-number date 1)
1088 #\0 2) port)))
1089 (cons #\w (lambda (date pad-with port)
1090 (display (date-week-day date) port)))
1091 (cons #\x (lambda (date pad-with port)
1092 (display (date->string date locale-short-date-format) port)))
1093 (cons #\X (lambda (date pad-with port)
1094 (display (date->string date locale-time-format) port)))
1095 (cons #\W (lambda (date pad-with port)
1096 (if (> (days-before-first-week date 1) 0)
1097 (display (padding (+ (date-week-number date 1) 1)
1098 #\0 2) port)
1099 (display (padding (date-week-number date 1)
1100 #\0 2) port))))
1101 (cons #\y (lambda (date pad-with port)
1102 (display (padding (last-n-digits
1103 (date-year date) 2)
1104 pad-with
1105 2)
1106 port)))
1107 (cons #\Y (lambda (date pad-with port)
1108 (display (date-year date) port)))
1109 (cons #\z (lambda (date pad-with port)
1110 (tz-printer (date-zone-offset date) port)))
1111 (cons #\Z (lambda (date pad-with port)
1112 (locale-print-time-zone date port)))
1113 (cons #\1 (lambda (date pad-with port)
1114 (display (date->string date "~Y-~m-~d") port)))
1115 (cons #\2 (lambda (date pad-with port)
1116 (display (date->string date "~H:~M:~S~z") port)))
1117 (cons #\3 (lambda (date pad-with port)
1118 (display (date->string date "~H:~M:~S") port)))
1119 (cons #\4 (lambda (date pad-with port)
1120 (display (date->string date "~Y-~m-~dT~H:~M:~S~z") port)))
1121 (cons #\5 (lambda (date pad-with port)
1122 (display (date->string date "~Y-~m-~dT~H:~M:~S") port)))))
1123
1124
1125 (define (get-formatter char)
1126 (let ((associated (assoc char directives)))
1127 (if associated (cdr associated) #f)))
1128
1129 (define (date-printer date index format-string str-len port)
1130 (if (< index str-len)
1131 (let ((current-char (string-ref format-string index)))
1132 (if (not (char=? current-char #\~))
1133 (begin
1134 (display current-char port)
1135 (date-printer date (+ index 1) format-string str-len port))
1136 (if (= (+ index 1) str-len) ; bad format string.
1137 (time-error 'date-printer 'bad-date-format-string
1138 format-string)
1139 (let ((pad-char? (string-ref format-string (+ index 1))))
1140 (cond
1141 ((char=? pad-char? #\-)
1142 (if (= (+ index 2) str-len) ; bad format string.
1143 (time-error 'date-printer
1144 'bad-date-format-string
1145 format-string)
1146 (let ((formatter (get-formatter
1147 (string-ref format-string
1148 (+ index 2)))))
1149 (if (not formatter)
1150 (time-error 'date-printer
1151 'bad-date-format-string
1152 format-string)
1153 (begin
1154 (formatter date #f port)
1155 (date-printer date
1156 (+ index 3)
1157 format-string
1158 str-len
1159 port))))))
1160
1161 ((char=? pad-char? #\_)
1162 (if (= (+ index 2) str-len) ; bad format string.
1163 (time-error 'date-printer
1164 'bad-date-format-string
1165 format-string)
1166 (let ((formatter (get-formatter
1167 (string-ref format-string
1168 (+ index 2)))))
1169 (if (not formatter)
1170 (time-error 'date-printer
1171 'bad-date-format-string
1172 format-string)
1173 (begin
1174 (formatter date #\Space port)
1175 (date-printer date
1176 (+ index 3)
1177 format-string
1178 str-len
1179 port))))))
1180 (else
1181 (let ((formatter (get-formatter
1182 (string-ref format-string
1183 (+ index 1)))))
1184 (if (not formatter)
1185 (time-error 'date-printer
1186 'bad-date-format-string
1187 format-string)
1188 (begin
1189 (formatter date #\0 port)
1190 (date-printer date
1191 (+ index 2)
1192 format-string
1193 str-len
1194 port))))))))))))
1195
1196
1197 (define (date->string date . format-string)
1198 (let ((str-port (open-output-string))
1199 (fmt-str (if (null? format-string) "~c" (car format-string))))
1200 (date-printer date 0 fmt-str (string-length fmt-str) str-port)
1201 (get-output-string str-port)))
1202
1203 (define (char->int ch)
1204 (case ch
1205 ((#\0) 0)
1206 ((#\1) 1)
1207 ((#\2) 2)
1208 ((#\3) 3)
1209 ((#\4) 4)
1210 ((#\5) 5)
1211 ((#\6) 6)
1212 ((#\7) 7)
1213 ((#\8) 8)
1214 ((#\9) 9)
1215 (else (time-error 'char->int 'bad-date-template-string
1216 (list "Non-integer character" ch)))))
1217
1218 ;; read an integer upto n characters long on port; upto -> #f is any length
1219 (define (integer-reader upto port)
1220 (let loop ((accum 0) (nchars 0))
1221 (let ((ch (peek-char port)))
1222 (if (or (eof-object? ch)
1223 (not (char-numeric? ch))
1224 (and upto (>= nchars upto)))
1225 accum
1226 (loop (+ (* accum 10) (char->int (read-char port)))
1227 (+ nchars 1))))))
1228
1229 (define (make-integer-reader upto)
1230 (lambda (port)
1231 (integer-reader upto port)))
1232
1233 ;; read *exactly* n characters and convert to integer; could be padded
1234 (define (integer-reader-exact n port)
1235 (let ((padding-ok #t))
1236 (define (accum-int port accum nchars)
1237 (let ((ch (peek-char port)))
1238 (cond
1239 ((>= nchars n) accum)
1240 ((eof-object? ch)
1241 (time-error 'string->date 'bad-date-template-string
1242 "Premature ending to integer read."))
1243 ((char-numeric? ch)
1244 (set! padding-ok #f)
1245 (accum-int port
1246 (+ (* accum 10) (char->int (read-char port)))
1247 (+ nchars 1)))
1248 (padding-ok
1249 (read-char port) ; consume padding
1250 (accum-int port accum (+ nchars 1)))
1251 (else ; padding where it shouldn't be
1252 (time-error 'string->date 'bad-date-template-string
1253 "Non-numeric characters in integer read.")))))
1254 (accum-int port 0 0)))
1255
1256
1257 (define (make-integer-exact-reader n)
1258 (lambda (port)
1259 (integer-reader-exact n port)))
1260
1261 (define (zone-reader port)
1262 (let ((offset 0)
1263 (positive? #f))
1264 (let ((ch (read-char port)))
1265 (if (eof-object? ch)
1266 (time-error 'string->date 'bad-date-template-string
1267 (list "Invalid time zone +/-" ch)))
1268 (if (or (char=? ch #\Z) (char=? ch #\z))
1269 0
1270 (begin
1271 (cond
1272 ((char=? ch #\+) (set! positive? #t))
1273 ((char=? ch #\-) (set! positive? #f))
1274 (else
1275 (time-error 'string->date 'bad-date-template-string
1276 (list "Invalid time zone +/-" ch))))
1277 (let ((ch (read-char port)))
1278 (if (eof-object? ch)
1279 (time-error 'string->date 'bad-date-template-string
1280 (list "Invalid time zone number" ch)))
1281 (set! offset (* (char->int ch)
1282 10 60 60)))
1283 (let ((ch (read-char port)))
1284 (if (eof-object? ch)
1285 (time-error 'string->date 'bad-date-template-string
1286 (list "Invalid time zone number" ch)))
1287 (set! offset (+ offset (* (char->int ch)
1288 60 60))))
1289 (let ((ch (read-char port)))
1290 (if (eof-object? ch)
1291 (time-error 'string->date 'bad-date-template-string
1292 (list "Invalid time zone number" ch)))
1293 (set! offset (+ offset (* (char->int ch)
1294 10 60))))
1295 (let ((ch (read-char port)))
1296 (if (eof-object? ch)
1297 (time-error 'string->date 'bad-date-template-string
1298 (list "Invalid time zone number" ch)))
1299 (set! offset (+ offset (* (char->int ch)
1300 60))))
1301 (if positive? offset (- offset)))))))
1302
1303 ;; looking at a char, read the char string, run thru indexer, return index
1304 (define (locale-reader port indexer)
1305
1306 (define (read-char-string result)
1307 (let ((ch (peek-char port)))
1308 (if (char-alphabetic? ch)
1309 (read-char-string (cons (read-char port) result))
1310 (list->string (reverse! result)))))
1311
1312 (let* ((str (read-char-string '()))
1313 (index (indexer str)))
1314 (if index index (time-error 'string->date
1315 'bad-date-template-string
1316 (list "Invalid string for " indexer)))))
1317
1318 (define (make-locale-reader indexer)
1319 (lambda (port)
1320 (locale-reader port indexer)))
1321
1322 (define (make-char-id-reader char)
1323 (lambda (port)
1324 (if (char=? char (read-char port))
1325 char
1326 (time-error 'string->date
1327 'bad-date-template-string
1328 "Invalid character match."))))
1329
1330 ;; A List of formatted read directives.
1331 ;; Each entry is a list.
1332 ;; 1. the character directive;
1333 ;; a procedure, which takes a character as input & returns
1334 ;; 2. #t as soon as a character on the input port is acceptable
1335 ;; for input,
1336 ;; 3. a port reader procedure that knows how to read the current port
1337 ;; for a value. Its one parameter is the port.
1338 ;; 4. an optional action procedure, that takes the value (from 3.) and
1339 ;; some object (here, always the date) and (probably) side-effects it.
1340 ;; If no action is required, as with ~A, this element may be #f.
1341
1342 (define read-directives
1343 (let ((ireader4 (make-integer-reader 4))
1344 (ireader2 (make-integer-reader 2))
1345 (eireader2 (make-integer-exact-reader 2))
1346 (locale-reader-abbr-weekday (make-locale-reader
1347 locale-abbr-weekday->index))
1348 (locale-reader-long-weekday (make-locale-reader
1349 locale-long-weekday->index))
1350 (locale-reader-abbr-month (make-locale-reader
1351 locale-abbr-month->index))
1352 (locale-reader-long-month (make-locale-reader
1353 locale-long-month->index))
1354 (char-fail (lambda (ch) #t)))
1355
1356 (list
1357 (list #\~ char-fail (make-char-id-reader #\~) #f)
1358 (list #\a char-alphabetic? locale-reader-abbr-weekday #f)
1359 (list #\A char-alphabetic? locale-reader-long-weekday #f)
1360 (list #\b char-alphabetic? locale-reader-abbr-month
1361 (lambda (val object)
1362 (set-date-month! object val)))
1363 (list #\B char-alphabetic? locale-reader-long-month
1364 (lambda (val object)
1365 (set-date-month! object val)))
1366 (list #\d char-numeric? ireader2 (lambda (val object)
1367 (set-date-day!
1368 object val)))
1369 (list #\e char-fail eireader2 (lambda (val object)
1370 (set-date-day! object val)))
1371 (list #\h char-alphabetic? locale-reader-abbr-month
1372 (lambda (val object)
1373 (set-date-month! object val)))
1374 (list #\H char-numeric? ireader2 (lambda (val object)
1375 (set-date-hour! object val)))
1376 (list #\k char-fail eireader2 (lambda (val object)
1377 (set-date-hour! object val)))
1378 (list #\m char-numeric? ireader2 (lambda (val object)
1379 (set-date-month! object val)))
1380 (list #\M char-numeric? ireader2 (lambda (val object)
1381 (set-date-minute!
1382 object val)))
1383 (list #\S char-numeric? ireader2 (lambda (val object)
1384 (set-date-second! object val)))
1385 (list #\y char-fail eireader2
1386 (lambda (val object)
1387 (set-date-year! object (natural-year val))))
1388 (list #\Y char-numeric? ireader4 (lambda (val object)
1389 (set-date-year! object val)))
1390 (list #\z (lambda (c)
1391 (or (char=? c #\Z)
1392 (char=? c #\z)
1393 (char=? c #\+)
1394 (char=? c #\-)))
1395 zone-reader (lambda (val object)
1396 (set-date-zone-offset! object val))))))
1397
1398 (define (priv:string->date date index format-string str-len port template-string)
1399 (define (skip-until port skipper)
1400 (let ((ch (peek-char port)))
1401 (if (eof-object? ch)
1402 (time-error 'string->date 'bad-date-format-string template-string)
1403 (if (not (skipper ch))
1404 (begin (read-char port) (skip-until port skipper))))))
1405 (if (< index str-len)
1406 (let ((current-char (string-ref format-string index)))
1407 (if (not (char=? current-char #\~))
1408 (let ((port-char (read-char port)))
1409 (if (or (eof-object? port-char)
1410 (not (char=? current-char port-char)))
1411 (time-error 'string->date
1412 'bad-date-format-string template-string))
1413 (priv:string->date date
1414 (+ index 1)
1415 format-string
1416 str-len
1417 port
1418 template-string))
1419 ;; otherwise, it's an escape, we hope
1420 (if (> (+ index 1) str-len)
1421 (time-error 'string->date
1422 'bad-date-format-string template-string)
1423 (let* ((format-char (string-ref format-string (+ index 1)))
1424 (format-info (assoc format-char read-directives)))
1425 (if (not format-info)
1426 (time-error 'string->date
1427 'bad-date-format-string template-string)
1428 (begin
1429 (let ((skipper (cadr format-info))
1430 (reader (caddr format-info))
1431 (actor (cadddr format-info)))
1432 (skip-until port skipper)
1433 (let ((val (reader port)))
1434 (if (eof-object? val)
1435 (time-error 'string->date
1436 'bad-date-format-string
1437 template-string)
1438 (if actor (actor val date))))
1439 (priv:string->date date
1440 (+ index 2)
1441 format-string
1442 str-len
1443 port
1444 template-string))))))))))
1445
1446 (define (string->date input-string template-string)
1447 (define (date-ok? date)
1448 (and (date-nanosecond date)
1449 (date-second date)
1450 (date-minute date)
1451 (date-hour date)
1452 (date-day date)
1453 (date-month date)
1454 (date-year date)
1455 (date-zone-offset date)))
1456 (let ((newdate (make-date 0 0 0 0 #f #f #f #f)))
1457 (priv:string->date newdate
1458 0
1459 template-string
1460 (string-length template-string)
1461 (open-input-string input-string)
1462 template-string)
1463 (if (not (date-zone-offset newdate))
1464 (begin
1465 ;; this is necessary to get DST right -- as far as we can
1466 ;; get it right (think of the double/missing hour in the
1467 ;; night when we are switching between normal time and DST).
1468 (set-date-zone-offset! newdate
1469 (local-tz-offset
1470 (make-time time-utc 0 0)))
1471 (set-date-zone-offset! newdate
1472 (local-tz-offset
1473 (date->time-utc newdate)))))
1474 (if (date-ok? newdate)
1475 newdate
1476 (time-error
1477 'string->date
1478 'bad-date-format-string
1479 (list "Incomplete date read. " newdate template-string)))))
1480
1481 ;;; srfi-19.scm ends here