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