Manipulate GOOPS vtable flags from Scheme, for speed
[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,
4 ;; 2011, 2014 Free Software Foundation, Inc.
5 ;;
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.
10 ;;
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.
15 ;;
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
19
20 ;;; Author: Rob Browning <rlb@cs.utexas.edu>
21 ;;; Originally from SRFI reference implementation by Will Fitzgerald.
22
23 ;;; Commentary:
24
25 ;; This module is fully documented in the Guile Reference Manual.
26
27 ;;; Code:
28
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".
34 ;;
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.
41
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)
49 :export (;; Constants
50 time-duration
51 time-monotonic
52 time-process
53 time-tai
54 time-thread
55 time-utc
56 ;; Current time and clock resolution
57 current-date
58 current-julian-day
59 current-modified-julian-day
60 time-resolution
61 ;; Time object and accessors
62 make-time
63 time?
64 time-type
65 time-nanosecond
66 time-second
67 set-time-type!
68 set-time-nanosecond!
69 set-time-second!
70 copy-time
71 ;; Time comparison procedures
72 time<=?
73 time<?
74 time=?
75 time>=?
76 time>?
77 ;; Time arithmetic procedures
78 time-difference
79 time-difference!
80 add-duration
81 add-duration!
82 subtract-duration
83 subtract-duration!
84 ;; Date object and accessors
85 make-date
86 date?
87 date-nanosecond
88 date-second
89 date-minute
90 date-hour
91 date-day
92 date-month
93 date-year
94 date-zone-offset
95 date-year-day
96 date-week-day
97 date-week-number
98 ;; Time/Date/Julian Day/Modified Julian Day converters
99 date->julian-day
100 date->modified-julian-day
101 date->time-monotonic
102 date->time-tai
103 date->time-utc
104 julian-day->date
105 julian-day->time-monotonic
106 julian-day->time-tai
107 julian-day->time-utc
108 modified-julian-day->date
109 modified-julian-day->time-monotonic
110 modified-julian-day->time-tai
111 modified-julian-day->time-utc
112 time-monotonic->date
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!
119 time-tai->date
120 time-tai->julian-day
121 time-tai->modified-julian-day
122 time-tai->time-monotonic
123 time-tai->time-monotonic!
124 time-tai->time-utc
125 time-tai->time-utc!
126 time-utc->date
127 time-utc->julian-day
128 time-utc->modified-julian-day
129 time-utc->time-monotonic
130 time-utc->time-monotonic!
131 time-utc->time-tai
132 time-utc->time-tai!
133 ;; Date to string/string to date converters.
134 date->string
135 string->date))
136
137 (cond-expand-provide (current-module) '(srfi-19))
138
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)
145
146 ;; FIXME: do we want to add gc time?
147 ;; (define time-gc 'time-gc)
148
149 ;;-- LOCALE dependent constants
150
151 ;; See date->string
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")
156
157 ;;-- Miscellaneous Constants.
158 ;;-- only the tai-epoch-in-jd might need changing if
159 ;; a different epoch is used.
160
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'
165
166 ;; FIXME: should this be something other than misc-error?
167 (define (time-error caller type value)
168 (if 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)))
171
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"))
180
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))
187 (table '()))
188 (let loop ((line (read-line port)))
189 (if (not (eof-object? line))
190 (begin
191 (let* ((data (read (open-input-string
192 (string-append "(" line ")"))))
193 (year (car data))
194 (jd (cadddr (cdr data)))
195 (secs (cadddr (cdddr data))))
196 (if (>= year 1972)
197 (set! table (cons
198 (cons (convert-jd jd) (convert-sec secs))
199 table)))
200 (loop (read-line port))))))
201 table))
202
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
206 '((1341100800 . 35)
207 (1230768000 . 34)
208 (1136073600 . 33)
209 (915148800 . 32)
210 (867715200 . 31)
211 (820454400 . 30)
212 (773020800 . 29)
213 (741484800 . 28)
214 (709948800 . 27)
215 (662688000 . 26)
216 (631152000 . 25)
217 (567993600 . 24)
218 (489024000 . 23)
219 (425865600 . 22)
220 (394329600 . 21)
221 (362793600 . 20)
222 (315532800 . 19)
223 (283996800 . 18)
224 (252460800 . 17)
225 (220924800 . 16)
226 (189302400 . 15)
227 (157766400 . 14)
228 (126230400 . 13)
229 (94694400 . 12)
230 (78796800 . 11)
231 (63072000 . 10)))
232
233 (define (read-leap-second-table filename)
234 (set! leap-second-table (read-tai-utc-data filename)))
235
236
237 (define (leap-second-delta utc-seconds)
238 (letrec ((lsd (lambda (table)
239 (cond ((>= utc-seconds (caar table))
240 (cdar table))
241 (else (lsd (cdr table)))))))
242 (if (< utc-seconds (* (- 1972 1970) 365 sid)) 0
243 (lsd leap-second-table))))
244
245
246 ;;; the TIME structure; creates the accessors, too.
247
248 (define-record-type time
249 (make-time-unnormalized type nanosecond second)
250 time?
251 (type time-type set-time-type!)
252 (nanosecond time-nanosecond set-time-nanosecond!)
253 (second time-second set-time-second!))
254
255 (define (copy-time time)
256 (make-time (time-type time) (time-nanosecond time) (time-second time)))
257
258 (define (split-real r)
259 (if (integer? r)
260 (values (inexact->exact r) 0)
261 (let ((l (truncate r)))
262 (values (inexact->exact l) (- r l)))))
263
264 (define (time-normalize! t)
265 (if (>= (abs (time-nanosecond t)) 1000000000)
266 (receive (int frac)
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)
271 frac))))
272 (if (and (positive? (time-second t))
273 (negative? (time-nanosecond t)))
274 (begin
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)))
279 (begin
280 (set-time-second! t (+ (time-second t) 1))
281 (set-time-nanosecond! t (+ 1000000000 (time-nanosecond t))))))
282 t)
283
284 (define (make-time type nanosecond second)
285 (time-normalize! (make-time-unnormalized type nanosecond second)))
286
287 ;; Helpers
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
292 ;; set-tm:sec?
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)))
303 result))
304
305 ;;; current-time
306
307 ;;; specific time getters.
308
309 (define (current-time-utc)
310 ;; Resolution is microseconds.
311 (let ((tod (gettimeofday)))
312 (make-time time-utc (* (cdr tod) 1000) (car tod))))
313
314 (define (current-time-tai)
315 ;; Resolution is microseconds.
316 (let* ((tod (gettimeofday))
317 (sec (car tod))
318 (usec (cdr tod)))
319 (make-time time-tai
320 (* usec 1000)
321 (+ (car tod) (leap-second-delta sec)))))
322
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))))
328
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,
332 ;; of course.
333
334 (define (current-time-monotonic)
335 ;; Resolution is microseconds.
336 (current-time-tai))
337
338 (define (current-time-thread)
339 (time-error 'current-time 'unsupported-clock-type 'time-thread))
340
341 (define ns-per-guile-tick (/ 1000000000 internal-time-units-per-second))
342
343 (define (current-time-process)
344 (let ((run-time (get-internal-run-time)))
345 (make-time
346 time-process
347 (* (remainder run-time internal-time-units-per-second)
348 ns-per-guile-tick)
349 (quotient run-time internal-time-units-per-second))))
350
351 ;;(define (current-time-gc)
352 ;; (current-time-ms-time time-gc current-gc-milliseconds))
353
354 (define (current-time . clock-type)
355 (let ((clock-type (if (null? clock-type) time-utc (car clock-type))))
356 (cond
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)))))
364
365 ;; -- Time Resolution
366 ;; This is the resolution of the clock in nanoseconds.
367 ;; This will be implementation specific.
368
369 (define (time-resolution . clock-type)
370 (let ((clock-type (if (null? clock-type) time-utc (car clock-type))))
371 (case clock-type
372 ((time-tai) 1000)
373 ((time-utc) 1000)
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)))))
379
380 ;; -- Time comparisons
381
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))))
388
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)))))
393
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)))))
398
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)))))
403
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)))))
408
409 ;; -- Time arithmetic
410
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)))
418
419 (define (time-difference time1 time2)
420 (let ((result (copy-time time1)))
421 (time-difference! result time2)))
422
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))))
431
432 (define (add-duration t duration)
433 (let ((result (copy-time t)))
434 (add-duration! result duration)))
435
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))))
444
445 (define (subtract-duration time1 duration)
446 (let ((result (copy-time time1)))
447 (subtract-duration! result duration)))
448
449 ;; -- Converters between types.
450
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)
457 (leap-second-delta
458 (time-second time-in))))
459 time-out)
460
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))
463
464
465 (define (time-tai->time-utc! time-in)
466 (priv:time-tai->time-utc! time-in time-in 'time-tai->time-utc!))
467
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)
474 (leap-second-delta
475 (time-second time-in))))
476 time-out)
477
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))
480
481 (define (time-utc->time-tai! time-in)
482 (priv:time-utc->time-tai! time-in time-in 'time-utc->time-tai!))
483
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)))
492
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))
499
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)
506 ntime))
507
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)
513 time-in)
514
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)
522 ntime))
523
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)
531 ntime))
532
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)
539 ntime))
540
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)
546 time-in)
547
548 ;; -- Date Structures
549
550 ;; FIXME: to be really safe, perhaps we should normalize the
551 ;; seconds/nanoseconds/minutes coming in to make-date...
552
553 (define-record-type date
554 (make-date nanosecond second minute
555 hour day month
556 year
557 zone-offset)
558 date?
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!))
567
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)))
573 (+ day
574 (quotient (+ (* 153 m) 2) 5)
575 (* 365 y)
576 (quotient y 4)
577 (- (quotient y 100))
578 (quotient y 400)
579 -32045)))
580
581 ;; gives the seconds/date/month/year
582 (define (decode-julian-day-number jdn)
583 (let* ((days (inexact->exact (truncate jdn)))
584 (a (+ days 32044))
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
592 (* (- jdn days) sid)
593 (+ e (- (quotient (+ (* 153 m) 2) 5)) 1)
594 (+ m 3 (* -12 (quotient m 10)))
595 (if (>= 0 y) (- y 1) y))))
596
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.
600
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)))))
604
605 ;; special thing -- ignores nanos
606 (define (time->julian-day-number seconds tz-offset)
607 (+ (/ (+ seconds tz-offset sihd)
608 sid)
609 tai-epoch-in-jd))
610
611 (define (leap-second? second)
612 (and (assoc second leap-second-table) #t))
613
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)
619 (car tz-offset)))
620 (leap-second? (leap-second? (+ offset (time-second time))))
621 (jdn (time->julian-day-number (if leap-second?
622 (- (time-second time) 1)
623 (time-second time))
624 offset)))
625
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)
637 minutes
638 hours
639 date
640 month
641 year
642 offset))))))
643
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))
649 (car tz-offset)))
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?
654 (- seconds 1)
655 seconds)
656 offset)))
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)
669 minutes
670 hours
671 date
672 month
673 year
674 offset))))))
675
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))
682 (car tz-offset)))
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?
687 (- seconds 1)
688 seconds)
689 offset)))
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)
702 minutes
703 hours
704 date
705 month
706 year
707 offset))))))
708
709 (define (date->time-utc date)
710 (let* ((jdays (- (encode-julian-day-number (date-day date)
711 (date-month date)
712 (date-year date))
713 tai-epoch-in-jd))
714 ;; jdays is an integer plus 1/2,
715 (jdays-1/2 (inexact->exact (- jdays 1/2))))
716 (make-time
717 time-utc
718 (date-nanosecond date)
719 (+ (* jdays-1/2 24 60 60)
720 (* (date-hour date) 60 60)
721 (* (date-minute date) 60)
722 (date-second date)
723 (- (date-zone-offset date))))))
724
725 (define (date->time-tai date)
726 (time-utc->time-tai! (date->time-utc date)))
727
728 (define (date->time-monotonic date)
729 (time-utc->time-monotonic! (date->time-utc date)))
730
731 (define (leap-year? year)
732 (or (= (modulo year 400) 0)
733 (and (= (modulo year 4) 0) (not (= (modulo year 100) 0)))))
734
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)))
740
741 (define (year-day day month year)
742 (let ((days-pr (assoc month month-assoc)))
743 (if (not days-pr)
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)))))
748
749 (define (date-year-day date)
750 (year-day (date-day date) (date-month date) (date-year date)))
751
752 ;; from calendar faq
753 (define (week-day day month year)
754 (let* ((a (quotient (- 14 month) 12))
755 (y (- year a))
756 (m (+ month (* 12 a) -2)))
757 (modulo (+ day
758 y
759 (quotient y 4)
760 (- (quotient y 100))
761 (quotient y 400)
762 (quotient (* 31 m) 12))
763 7)))
764
765 (define (date-week-day date)
766 (week-day (date-day date) (date-month date) (date-year date)))
767
768 (define (days-before-first-week date day-of-week-starting-week)
769 (let* ((first-day (make-date 0 0 0 0
770 1
771 1
772 (date-year date)
773 #f))
774 (fdweek-day (date-week-day first-day)))
775 (modulo (- day-of-week-starting-week fdweek-day)
776 7)))
777
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.
781 ;;
782 (define (date-week-number date day-of-week-starting-week)
783 (quotient (- (date-year-day date)
784 1
785 (days-before-first-week date day-of-week-starting-week))
786 7))
787
788 (define (current-date . tz-offset)
789 (let ((time (current-time time-utc)))
790 (time-utc->date
791 time
792 (if (null? tz-offset)
793 (local-tz-offset time)
794 (car tz-offset)))))
795
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)))
800 (cond
801 ((>= n 100) n)
802 ((< n 0) n)
803 ((<= (- (+ current-century n) current-year) 50) (+ current-century n))
804 (else (+ (- current-century 100) n)))))
805
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)
816 (- 1/2)
817 (+ (/ (+ (- offset)
818 (* hour 60 60)
819 (* minute 60)
820 second
821 (/ nanosecond nano))
822 sid)))))
823
824 (define (date->modified-julian-day date)
825 (- (date->julian-day date)
826 4800001/2))
827
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))
832 sid)
833 tai-epoch-in-jd))
834
835 (define (time-utc->modified-julian-day time)
836 (- (time-utc->julian-day time)
837 4800001/2))
838
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))
845 sid)
846 tai-epoch-in-jd))
847
848 (define (time-tai->modified-julian-day time)
849 (- (time-tai->julian-day time)
850 4800001/2))
851
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))
859 sid)
860 tai-epoch-in-jd))
861
862 (define (time-monotonic->modified-julian-day time)
863 (- (time-monotonic->julian-day time)
864 4800001/2))
865
866 (define (julian-day->time-utc jdn)
867 (let ((secs (* sid (- jdn tai-epoch-in-jd))))
868 (receive (seconds parts)
869 (split-real secs)
870 (make-time time-utc
871 (* parts nano)
872 seconds))))
873
874 (define (julian-day->time-tai jdn)
875 (time-utc->time-tai! (julian-day->time-utc jdn)))
876
877 (define (julian-day->time-monotonic jdn)
878 (time-utc->time-monotonic! (julian-day->time-utc jdn)))
879
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)
884 (car tz-offset))))
885 (time-utc->date time offset)))
886
887 (define (modified-julian-day->date jdn . tz-offset)
888 (apply julian-day->date (+ jdn 4800001/2)
889 tz-offset))
890
891 (define (modified-julian-day->time-utc jdn)
892 (julian-day->time-utc (+ jdn 4800001/2)))
893
894 (define (modified-julian-day->time-tai jdn)
895 (julian-day->time-tai (+ jdn 4800001/2)))
896
897 (define (modified-julian-day->time-monotonic jdn)
898 (julian-day->time-monotonic (+ jdn 4800001/2)))
899
900 (define (current-julian-day)
901 (time-utc->julian-day (current-time time-utc)))
902
903 (define (current-modified-julian-day)
904 (time-utc->modified-julian-day (current-time time-utc)))
905
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.
910
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)
915 (not pad-with))
916 str
917 (string-append (make-string (- length str-len) pad-with) str))))
918
919 (define (last-n-digits i n)
920 (abs (remainder i (expt 10 n))))
921
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)
926
927 (define (date-reverse-lookup needle haystack-ref haystack-len
928 same?)
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))
935 index)
936 (else (loop (+ index 1))))))
937
938 (define (locale-abbr-weekday->index string)
939 (date-reverse-lookup string locale-day-short 7 string=?))
940
941 (define (locale-long-weekday->index string)
942 (date-reverse-lookup string locale-day 7 string=?))
943
944 (define (locale-abbr-month->index string)
945 (date-reverse-lookup string locale-abbr-month 12 string=?))
946
947 (define (locale-long-month->index string)
948 (date-reverse-lookup string locale-long-month 12 string=?))
949
950
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))
955
956 (define (locale-am-string/pm hr)
957 (if (> hr 11) (locale-pm-string) (locale-am-string)))
958
959 (define (tz-printer offset port)
960 (cond
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))))
969
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.
974 ;;
975 (define directives
976 (list
977 (cons #\~ (lambda (date pad-with port)
978 (display #\~ port)))
979 (cons #\a (lambda (date pad-with port)
980 (display (locale-abbr-weekday (date-week-day date))
981 port)))
982 (cons #\A (lambda (date pad-with port)
983 (display (locale-long-weekday (date-week-day date))
984 port)))
985 (cons #\b (lambda (date pad-with port)
986 (display (locale-abbr-month (date-month date))
987 port)))
988 (cons #\B (lambda (date pad-with port)
989 (display (locale-long-month (date-month date))
990 port)))
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)
995 #\0 2)
996 port)))
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)
1001 #\Space 2)
1002 port)))
1003 (cons #\f (lambda (date pad-with port)
1004 (if (> (date-nanosecond date)
1005 nano)
1006 (display (padding (+ (date-second date) 1)
1007 pad-with 2)
1008 port)
1009 (display (padding (date-second date)
1010 pad-with 2)
1011 port))
1012 (receive (i f)
1013 (split-real (/
1014 (date-nanosecond date)
1015 nano 1.0))
1016 (let* ((ns (number->string f))
1017 (le (string-length ns)))
1018 (if (> le 2)
1019 (begin
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)
1026 pad-with 2)
1027 port)))
1028 (cons #\I (lambda (date pad-with port)
1029 (let ((hr (date-hour date)))
1030 (if (> hr 12)
1031 (display (padding (- hr 12)
1032 pad-with 2)
1033 port)
1034 (display (padding hr
1035 pad-with 2)
1036 port)))))
1037 (cons #\j (lambda (date pad-with port)
1038 (display (padding (date-year-day date)
1039 pad-with 3)
1040 port)))
1041 (cons #\k (lambda (date pad-with port)
1042 (display (padding (date-hour date)
1043 #\Space 2)
1044 port)))
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)
1049 port))))
1050 (cons #\m (lambda (date pad-with port)
1051 (display (padding (date-month date)
1052 pad-with 2)
1053 port)))
1054 (cons #\M (lambda (date pad-with port)
1055 (display (padding (date-minute date)
1056 pad-with 2)
1057 port)))
1058 (cons #\n (lambda (date pad-with port)
1059 (newline port)))
1060 (cons #\N (lambda (date pad-with port)
1061 (display (padding (date-nanosecond date)
1062 pad-with 7)
1063 port)))
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)
1072 nano)
1073 (display (padding (+ (date-second date) 1)
1074 pad-with 2)
1075 port)
1076 (display (padding (date-second date)
1077 pad-with 2)
1078 port))))
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)
1086 #\0 2) port)
1087 (display (padding (date-week-number date 0)
1088 #\0 2) port))))
1089 (cons #\V (lambda (date pad-with port)
1090 (display (padding (date-week-number date 1)
1091 #\0 2) port)))
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)
1101 #\0 2) port)
1102 (display (padding (date-week-number date 1)
1103 #\0 2) port))))
1104 (cons #\y (lambda (date pad-with port)
1105 (display (padding (last-n-digits
1106 (date-year date) 2)
1107 pad-with
1108 2)
1109 port)))
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)))))
1126
1127
1128 (define (get-formatter char)
1129 (let ((associated (assoc char directives)))
1130 (if associated (cdr associated) #f)))
1131
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 #\~))
1136 (begin
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
1141 format-string)
1142 (let ((pad-char? (string-ref format-string (+ index 1))))
1143 (cond
1144 ((char=? pad-char? #\-)
1145 (if (= (+ index 2) str-len) ; bad format string.
1146 (time-error 'date-printer
1147 'bad-date-format-string
1148 format-string)
1149 (let ((formatter (get-formatter
1150 (string-ref format-string
1151 (+ index 2)))))
1152 (if (not formatter)
1153 (time-error 'date-printer
1154 'bad-date-format-string
1155 format-string)
1156 (begin
1157 (formatter date #f port)
1158 (date-printer date
1159 (+ index 3)
1160 format-string
1161 str-len
1162 port))))))
1163
1164 ((char=? pad-char? #\_)
1165 (if (= (+ index 2) str-len) ; bad format string.
1166 (time-error 'date-printer
1167 'bad-date-format-string
1168 format-string)
1169 (let ((formatter (get-formatter
1170 (string-ref format-string
1171 (+ index 2)))))
1172 (if (not formatter)
1173 (time-error 'date-printer
1174 'bad-date-format-string
1175 format-string)
1176 (begin
1177 (formatter date #\Space port)
1178 (date-printer date
1179 (+ index 3)
1180 format-string
1181 str-len
1182 port))))))
1183 (else
1184 (let ((formatter (get-formatter
1185 (string-ref format-string
1186 (+ index 1)))))
1187 (if (not formatter)
1188 (time-error 'date-printer
1189 'bad-date-format-string
1190 format-string)
1191 (begin
1192 (formatter date #\0 port)
1193 (date-printer date
1194 (+ index 2)
1195 format-string
1196 str-len
1197 port))))))))))))
1198
1199
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)))
1205
1206 (define (char->int ch)
1207 (case ch
1208 ((#\0) 0)
1209 ((#\1) 1)
1210 ((#\2) 2)
1211 ((#\3) 3)
1212 ((#\4) 4)
1213 ((#\5) 5)
1214 ((#\6) 6)
1215 ((#\7) 7)
1216 ((#\8) 8)
1217 ((#\9) 9)
1218 (else (time-error 'char->int 'bad-date-template-string
1219 (list "Non-integer character" ch)))))
1220
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)))
1228 accum
1229 (loop (+ (* accum 10) (char->int (read-char port)))
1230 (+ nchars 1))))))
1231
1232 (define (make-integer-reader upto)
1233 (lambda (port)
1234 (integer-reader upto port)))
1235
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)))
1241 (cond
1242 ((>= nchars n) accum)
1243 ((eof-object? ch)
1244 (time-error 'string->date 'bad-date-template-string
1245 "Premature ending to integer read."))
1246 ((char-numeric? ch)
1247 (set! padding-ok #f)
1248 (accum-int port
1249 (+ (* accum 10) (char->int (read-char port)))
1250 (+ nchars 1)))
1251 (padding-ok
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)))
1258
1259
1260 (define (make-integer-exact-reader n)
1261 (lambda (port)
1262 (integer-reader-exact n port)))
1263
1264 (define (zone-reader port)
1265 (let ((offset 0)
1266 (positive? #f))
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))
1272 0
1273 (begin
1274 (cond
1275 ((char=? ch #\+) (set! positive? #t))
1276 ((char=? ch #\-) (set! positive? #f))
1277 (else
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)
1285 10 60 60)))
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)
1291 60 60))))
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)
1297 10 60))))
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)
1303 60))))
1304 (if positive? offset (- offset)))))))
1305
1306 ;; looking at a char, read the char string, run thru indexer, return index
1307 (define (locale-reader port indexer)
1308
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)))))
1314
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)))))
1320
1321 (define (make-locale-reader indexer)
1322 (lambda (port)
1323 (locale-reader port indexer)))
1324
1325 (define (make-char-id-reader char)
1326 (lambda (port)
1327 (if (char=? char (read-char port))
1328 char
1329 (time-error 'string->date
1330 'bad-date-template-string
1331 "Invalid character match."))))
1332
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
1338 ;; for input,
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.
1344
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)))
1358
1359 (list
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)
1370 (set-date-day!
1371 object val)))
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)
1384 (set-date-minute!
1385 object val)))
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)
1394 (or (char=? c #\Z)
1395 (char=? c #\z)
1396 (char=? c #\+)
1397 (char=? c #\-)))
1398 zone-reader (lambda (val object)
1399 (set-date-zone-offset! object val))))))
1400
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
1417 (+ index 1)
1418 format-string
1419 str-len
1420 port
1421 template-string))
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)
1431 (begin
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
1440 template-string)
1441 (if actor (actor val date))))
1442 (priv:string->date date
1443 (+ index 2)
1444 format-string
1445 str-len
1446 port
1447 template-string))))))))))
1448
1449 (define (string->date input-string template-string)
1450 (define (date-ok? date)
1451 (and (date-nanosecond date)
1452 (date-second date)
1453 (date-minute date)
1454 (date-hour date)
1455 (date-day date)
1456 (date-month date)
1457 (date-year date)
1458 (date-zone-offset date)))
1459 (let ((newdate (make-date 0 0 0 0 #f #f #f #f)))
1460 (priv:string->date newdate
1461 0
1462 template-string
1463 (string-length template-string)
1464 (open-input-string input-string)
1465 template-string)
1466 (if (not (date-zone-offset newdate))
1467 (begin
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
1472 (local-tz-offset
1473 (make-time time-utc 0 0)))
1474 (set-date-zone-offset! newdate
1475 (local-tz-offset
1476 (date->time-utc newdate)))))
1477 (if (date-ok? newdate)
1478 newdate
1479 (time-error
1480 'string->date
1481 'bad-date-format-string
1482 (list "Incomplete date read. " newdate template-string)))))
1483
1484 ;;; srfi-19.scm ends here