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