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