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