Commit | Line | Data |
---|---|---|
6be07c52 TTN |
1 | ;;; srfi-19.scm --- Time/Date Library |
2 | ||
1b09b607 | 3 | ;; Copyright (C) 2001, 2002, 2003, 2005, 2006 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))) | |
196 | (if (not (eq? line eof)) | |
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 | |
353 | (quotient run-time internal-time-units-per-second) | |
354 | (* (remainder run-time internal-time-units-per-second) | |
355 | priv:ns-per-guile-tick)))) | |
356 | ||
357 | (define (priv:current-time-process) | |
358 | (let ((run-time (get-internal-run-time))) | |
359 | (list | |
360 | 'time-process | |
361 | (* (remainder run-time internal-time-units-per-second) | |
362 | priv:ns-per-guile-tick) | |
363 | (quotient run-time internal-time-units-per-second)))) | |
364 | ||
365 | ;;(define (priv:current-time-gc) | |
366 | ;; (priv:current-time-ms-time time-gc current-gc-milliseconds)) | |
367 | ||
368 | (define (current-time . clock-type) | |
5e1fb41f | 369 | (let ((clock-type (if (null? clock-type) time-utc (car clock-type)))) |
5bbfe8cb RB |
370 | (cond |
371 | ((eq? clock-type time-tai) (priv:current-time-tai)) | |
372 | ((eq? clock-type time-utc) (priv:current-time-utc)) | |
373 | ((eq? clock-type time-monotonic) (priv:current-time-monotonic)) | |
374 | ((eq? clock-type time-thread) (priv:current-time-thread)) | |
375 | ((eq? clock-type time-process) (priv:current-time-process)) | |
376 | ;; ((eq? clock-type time-gc) (priv:current-time-gc)) | |
377 | (else (priv:time-error 'current-time 'invalid-clock-type clock-type))))) | |
378 | ||
379 | ;; -- Time Resolution | |
380 | ;; This is the resolution of the clock in nanoseconds. | |
381 | ;; This will be implementation specific. | |
382 | ||
383 | (define (time-resolution . clock-type) | |
5e1fb41f | 384 | (let ((clock-type (if (null? clock-type) time-utc (car clock-type)))) |
5bbfe8cb RB |
385 | (case clock-type |
386 | ((time-tai) 1000) | |
387 | ((time-utc) 1000) | |
388 | ((time-monotonic) 1000) | |
389 | ((time-process) priv:ns-per-guile-tick) | |
390 | ;; ((eq? clock-type time-thread) 1000) | |
391 | ;; ((eq? clock-type time-gc) 10000) | |
392 | (else (priv:time-error 'time-resolution 'invalid-clock-type clock-type))))) | |
393 | ||
394 | ;; -- Time comparisons | |
395 | ||
396 | (define (time=? t1 t2) | |
397 | ;; Arrange tests for speed and presume that t1 and t2 are actually times. | |
398 | ;; also presume it will be rare to check two times of different types. | |
399 | (and (= (time-second t1) (time-second t2)) | |
4549ba4a | 400 | (= (time-nanosecond t1) (time-nanosecond t2)) |
5bbfe8cb RB |
401 | (eq? (time-type t1) (time-type t2)))) |
402 | ||
403 | (define (time>? t1 t2) | |
404 | (or (> (time-second t1) (time-second t2)) | |
405 | (and (= (time-second t1) (time-second t2)) | |
406 | (> (time-nanosecond t1) (time-nanosecond t2))))) | |
407 | ||
408 | (define (time<? t1 t2) | |
409 | (or (< (time-second t1) (time-second t2)) | |
410 | (and (= (time-second t1) (time-second t2)) | |
411 | (< (time-nanosecond t1) (time-nanosecond t2))))) | |
412 | ||
413 | (define (time>=? t1 t2) | |
414 | (or (> (time-second t1) (time-second t2)) | |
415 | (and (= (time-second t1) (time-second t2)) | |
416 | (>= (time-nanosecond t1) (time-nanosecond t2))))) | |
417 | ||
418 | (define (time<=? t1 t2) | |
4549ba4a MV |
419 | (or (< (time-second t1) (time-second t2)) |
420 | (and (= (time-second t1) (time-second t2)) | |
421 | (<= (time-nanosecond t1) (time-nanosecond t2))))) | |
5bbfe8cb RB |
422 | |
423 | ;; -- Time arithmetic | |
424 | ||
425 | (define (time-difference! time1 time2) | |
426 | (let ((sec-diff (- (time-second time1) (time-second time2))) | |
427 | (nsec-diff (- (time-nanosecond time1) (time-nanosecond time2)))) | |
428 | (set-time-type! time1 time-duration) | |
429 | (set-time-second! time1 sec-diff) | |
430 | (set-time-nanosecond! time1 nsec-diff) | |
431 | (priv:time-normalize! time1))) | |
432 | ||
433 | (define (time-difference time1 time2) | |
434 | (let ((result (copy-time time1))) | |
435 | (time-difference! result time2))) | |
436 | ||
437 | (define (add-duration! t duration) | |
438 | (if (not (eq? (time-type duration) time-duration)) | |
439 | (priv:time-error 'add-duration 'not-duration duration) | |
440 | (let ((sec-plus (+ (time-second t) (time-second duration))) | |
441 | (nsec-plus (+ (time-nanosecond t) (time-nanosecond duration)))) | |
442 | (set-time-second! t sec-plus) | |
443 | (set-time-nanosecond! t nsec-plus) | |
444 | (priv:time-normalize! t)))) | |
445 | ||
4549ba4a | 446 | (define (add-duration t duration) |
5bbfe8cb | 447 | (let ((result (copy-time t))) |
afb47f6d | 448 | (add-duration! result duration))) |
5bbfe8cb RB |
449 | |
450 | (define (subtract-duration! t duration) | |
451 | (if (not (eq? (time-type duration) time-duration)) | |
452 | (priv:time-error 'add-duration 'not-duration duration) | |
453 | (let ((sec-minus (- (time-second t) (time-second duration))) | |
454 | (nsec-minus (- (time-nanosecond t) (time-nanosecond duration)))) | |
455 | (set-time-second! t sec-minus) | |
456 | (set-time-nanosecond! t nsec-minus) | |
457 | (priv:time-normalize! t)))) | |
458 | ||
459 | (define (subtract-duration time1 duration) | |
460 | (let ((result (copy-time time1))) | |
461 | (subtract-duration! result duration))) | |
462 | ||
463 | ;; -- Converters between types. | |
464 | ||
465 | (define (priv:time-tai->time-utc! time-in time-out caller) | |
466 | (if (not (eq? (time-type time-in) time-tai)) | |
467 | (priv:time-error caller 'incompatible-time-types time-in)) | |
468 | (set-time-type! time-out time-utc) | |
469 | (set-time-nanosecond! time-out (time-nanosecond time-in)) | |
470 | (set-time-second! time-out (- (time-second time-in) | |
afb47f6d | 471 | (priv:leap-second-delta |
5bbfe8cb RB |
472 | (time-second time-in)))) |
473 | time-out) | |
474 | ||
475 | (define (time-tai->time-utc time-in) | |
4549ba4a | 476 | (priv:time-tai->time-utc! time-in (make-time-unnormalized #f #f #f) 'time-tai->time-utc)) |
5bbfe8cb RB |
477 | |
478 | ||
479 | (define (time-tai->time-utc! time-in) | |
480 | (priv:time-tai->time-utc! time-in time-in 'time-tai->time-utc!)) | |
481 | ||
482 | (define (priv:time-utc->time-tai! time-in time-out caller) | |
483 | (if (not (eq? (time-type time-in) time-utc)) | |
484 | (priv:time-error caller 'incompatible-time-types time-in)) | |
485 | (set-time-type! time-out time-tai) | |
486 | (set-time-nanosecond! time-out (time-nanosecond time-in)) | |
487 | (set-time-second! time-out (+ (time-second time-in) | |
afb47f6d | 488 | (priv:leap-second-delta |
5bbfe8cb RB |
489 | (time-second time-in)))) |
490 | time-out) | |
491 | ||
492 | (define (time-utc->time-tai time-in) | |
4549ba4a | 493 | (priv:time-utc->time-tai! time-in (make-time-unnormalized #f #f #f) 'time-utc->time-tai)) |
5bbfe8cb RB |
494 | |
495 | (define (time-utc->time-tai! time-in) | |
496 | (priv:time-utc->time-tai! time-in time-in 'time-utc->time-tai!)) | |
497 | ||
498 | ;; -- these depend on time-monotonic having the same definition as time-tai! | |
499 | (define (time-monotonic->time-utc time-in) | |
500 | (if (not (eq? (time-type time-in) time-monotonic)) | |
501 | (priv:time-error caller 'incompatible-time-types time-in)) | |
502 | (let ((ntime (copy-time time-in))) | |
503 | (set-time-type! ntime time-tai) | |
504 | (priv:time-tai->time-utc! ntime ntime 'time-monotonic->time-utc))) | |
505 | ||
506 | (define (time-monotonic->time-utc! time-in) | |
507 | (if (not (eq? (time-type time-in) time-monotonic)) | |
508 | (priv:time-error caller 'incompatible-time-types time-in)) | |
509 | (set-time-type! time-in time-tai) | |
510 | (priv:time-tai->time-utc! ntime ntime 'time-monotonic->time-utc)) | |
511 | ||
512 | (define (time-monotonic->time-tai time-in) | |
513 | (if (not (eq? (time-type time-in) time-monotonic)) | |
514 | (priv:time-error caller 'incompatible-time-types time-in)) | |
515 | (let ((ntime (copy-time time-in))) | |
516 | (set-time-type! ntime time-tai) | |
517 | ntime)) | |
518 | ||
519 | (define (time-monotonic->time-tai! time-in) | |
520 | (if (not (eq? (time-type time-in) time-monotonic)) | |
521 | (priv:time-error caller 'incompatible-time-types time-in)) | |
522 | (set-time-type! time-in time-tai) | |
523 | time-in) | |
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)) | |
4549ba4a | 528 | (let ((ntime (priv:time-utc->time-tai! time-in (make-time-unnormalized #f #f #f) |
5bbfe8cb RB |
529 | 'time-utc->time-monotonic))) |
530 | (set-time-type! ntime time-monotonic) | |
531 | ntime)) | |
532 | ||
533 | (define (time-utc->time-monotonic! time-in) | |
534 | (if (not (eq? (time-type time-in) time-utc)) | |
535 | (priv:time-error caller 'incompatible-time-types time-in)) | |
536 | (let ((ntime (priv:time-utc->time-tai! time-in time-in | |
537 | 'time-utc->time-monotonic!))) | |
538 | (set-time-type! ntime time-monotonic) | |
539 | ntime)) | |
540 | ||
541 | (define (time-tai->time-monotonic time-in) | |
542 | (if (not (eq? (time-type time-in) time-tai)) | |
543 | (priv:time-error caller 'incompatible-time-types time-in)) | |
544 | (let ((ntime (copy-time time-in))) | |
545 | (set-time-type! ntime time-monotonic) | |
546 | ntime)) | |
547 | ||
548 | (define (time-tai->time-monotonic! time-in) | |
549 | (if (not (eq? (time-type time-in) time-tai)) | |
550 | (priv:time-error caller 'incompatible-time-types time-in)) | |
551 | (set-time-type! time-in time-monotonic) | |
552 | time-in) | |
553 | ||
554 | ;; -- Date Structures | |
555 | ||
5e1fb41f RB |
556 | ;; FIXME: to be really safe, perhaps we should normalize the |
557 | ;; seconds/nanoseconds/minutes coming in to make-date... | |
558 | ||
5bbfe8cb | 559 | (define-record-type date |
5e1fb41f RB |
560 | (make-date nanosecond second minute |
561 | hour day month | |
562 | year | |
563 | zone-offset) | |
5bbfe8cb | 564 | date? |
4549ba4a MV |
565 | (nanosecond date-nanosecond set-date-nanosecond!) |
566 | (second date-second set-date-second!) | |
567 | (minute date-minute set-date-minute!) | |
568 | (hour date-hour set-date-hour!) | |
569 | (day date-day set-date-day!) | |
570 | (month date-month set-date-month!) | |
571 | (year date-year set-date-year!) | |
572 | (zone-offset date-zone-offset set-date-zone-offset!)) | |
5e1fb41f | 573 | |
5bbfe8cb RB |
574 | ;; gives the julian day which starts at noon. |
575 | (define (priv:encode-julian-day-number day month year) | |
576 | (let* ((a (quotient (- 14 month) 12)) | |
577 | (y (- (+ year 4800) a (if (negative? year) -1 0))) | |
578 | (m (- (+ month (* 12 a)) 3))) | |
579 | (+ day | |
580 | (quotient (+ (* 153 m) 2) 5) | |
581 | (* 365 y) | |
582 | (quotient y 4) | |
583 | (- (quotient y 100)) | |
584 | (quotient y 400) | |
585 | -32045))) | |
586 | ||
afb47f6d | 587 | ;; gives the seconds/date/month/year |
5bbfe8cb | 588 | (define (priv:decode-julian-day-number jdn) |
5e1fb41f | 589 | (let* ((days (inexact->exact (truncate jdn))) |
5bbfe8cb RB |
590 | (a (+ days 32044)) |
591 | (b (quotient (+ (* 4 a) 3) 146097)) | |
592 | (c (- a (quotient (* 146097 b) 4))) | |
593 | (d (quotient (+ (* 4 c) 3) 1461)) | |
594 | (e (- c (quotient (* 1461 d) 4))) | |
595 | (m (quotient (+ (* 5 e) 2) 153)) | |
596 | (y (+ (* 100 b) d -4800 (quotient m 10)))) | |
597 | (values ; seconds date month year | |
598 | (* (- jdn days) priv:sid) | |
599 | (+ e (- (quotient (+ (* 153 m) 2) 5)) 1) | |
600 | (+ m 3 (* -12 (quotient m 10))) | |
601 | (if (>= 0 y) (- y 1) y)))) | |
602 | ||
603 | ;; relies on the fact that we named our time zone accessor | |
604 | ;; differently from MzScheme's.... | |
605 | ;; This should be written to be OS specific. | |
606 | ||
4549ba4a | 607 | (define (priv:local-tz-offset utc-time) |
5bbfe8cb | 608 | ;; SRFI uses seconds West, but guile (and libc) use seconds East. |
4549ba4a | 609 | (- (tm:gmtoff (localtime (time-second utc-time))))) |
5bbfe8cb RB |
610 | |
611 | ;; special thing -- ignores nanos | |
612 | (define (priv:time->julian-day-number seconds tz-offset) | |
613 | (+ (/ (+ seconds tz-offset priv:sihd) | |
614 | priv:sid) | |
615 | priv:tai-epoch-in-jd)) | |
616 | ||
617 | (define (priv:leap-second? second) | |
618 | (and (assoc second priv:leap-second-table) #t)) | |
619 | ||
620 | (define (time-utc->date time . tz-offset) | |
621 | (if (not (eq? (time-type time) time-utc)) | |
622 | (priv:time-error 'time->date 'incompatible-time-types time)) | |
4549ba4a | 623 | (let* ((offset (if (null? tz-offset) |
afb47f6d | 624 | (priv:local-tz-offset time) |
4549ba4a | 625 | (car tz-offset))) |
5bbfe8cb RB |
626 | (leap-second? (priv:leap-second? (+ offset (time-second time)))) |
627 | (jdn (priv:time->julian-day-number (if leap-second? | |
628 | (- (time-second time) 1) | |
629 | (time-second time)) | |
630 | offset))) | |
631 | ||
632 | (call-with-values (lambda () (priv:decode-julian-day-number jdn)) | |
633 | (lambda (secs date month year) | |
4549ba4a MV |
634 | ;; secs is a real because jdn is a real in Guile; |
635 | ;; but it is conceptionally an integer. | |
636 | (let* ((int-secs (inexact->exact (round secs))) | |
5e1fb41f RB |
637 | (hours (quotient int-secs (* 60 60))) |
638 | (rem (remainder int-secs (* 60 60))) | |
5bbfe8cb RB |
639 | (minutes (quotient rem 60)) |
640 | (seconds (remainder rem 60))) | |
641 | (make-date (time-nanosecond time) | |
642 | (if leap-second? (+ seconds 1) seconds) | |
643 | minutes | |
644 | hours | |
645 | date | |
646 | month | |
647 | year | |
648 | offset)))))) | |
649 | ||
650 | (define (time-tai->date time . tz-offset) | |
651 | (if (not (eq? (time-type time) time-tai)) | |
652 | (priv:time-error 'time->date 'incompatible-time-types time)) | |
4549ba4a MV |
653 | (let* ((offset (if (null? tz-offset) |
654 | (priv:local-tz-offset (time-tai->time-utc time)) | |
655 | (car tz-offset))) | |
5bbfe8cb RB |
656 | (seconds (- (time-second time) |
657 | (priv:leap-second-delta (time-second time)))) | |
658 | (leap-second? (priv:leap-second? (+ offset seconds))) | |
659 | (jdn (priv:time->julian-day-number (if leap-second? | |
660 | (- seconds 1) | |
661 | seconds) | |
662 | offset))) | |
663 | (call-with-values (lambda () (priv:decode-julian-day-number jdn)) | |
664 | (lambda (secs date month year) | |
4549ba4a MV |
665 | ;; secs is a real because jdn is a real in Guile; |
666 | ;; but it is conceptionally an integer. | |
5bbfe8cb | 667 | ;; adjust for leap seconds if necessary ... |
4549ba4a MV |
668 | (let* ((int-secs (inexact->exact (round secs))) |
669 | (hours (quotient int-secs (* 60 60))) | |
670 | (rem (remainder int-secs (* 60 60))) | |
5bbfe8cb RB |
671 | (minutes (quotient rem 60)) |
672 | (seconds (remainder rem 60))) | |
673 | (make-date (time-nanosecond time) | |
674 | (if leap-second? (+ seconds 1) seconds) | |
675 | minutes | |
676 | hours | |
677 | date | |
678 | month | |
679 | year | |
680 | offset)))))) | |
681 | ||
682 | ;; this is the same as time-tai->date. | |
683 | (define (time-monotonic->date time . tz-offset) | |
684 | (if (not (eq? (time-type time) time-monotonic)) | |
685 | (priv:time-error 'time->date 'incompatible-time-types time)) | |
4549ba4a MV |
686 | (let* ((offset (if (null? tz-offset) |
687 | (priv:local-tz-offset (time-monotonic->time-utc time)) | |
688 | (car tz-offset))) | |
5bbfe8cb RB |
689 | (seconds (- (time-second time) |
690 | (priv:leap-second-delta (time-second time)))) | |
691 | (leap-second? (priv:leap-second? (+ offset seconds))) | |
692 | (jdn (priv:time->julian-day-number (if leap-second? | |
693 | (- seconds 1) | |
694 | seconds) | |
695 | offset))) | |
696 | (call-with-values (lambda () (priv:decode-julian-day-number jdn)) | |
697 | (lambda (secs date month year) | |
4549ba4a MV |
698 | ;; secs is a real because jdn is a real in Guile; |
699 | ;; but it is conceptionally an integer. | |
5bbfe8cb | 700 | ;; adjust for leap seconds if necessary ... |
4549ba4a MV |
701 | (let* ((int-secs (inexact->exact (round secs))) |
702 | (hours (quotient int-secs (* 60 60))) | |
703 | (rem (remainder int-secs (* 60 60))) | |
5bbfe8cb RB |
704 | (minutes (quotient rem 60)) |
705 | (seconds (remainder rem 60))) | |
706 | (make-date (time-nanosecond time) | |
707 | (if leap-second? (+ seconds 1) seconds) | |
708 | minutes | |
709 | hours | |
710 | date | |
711 | month | |
712 | year | |
713 | offset)))))) | |
714 | ||
715 | (define (date->time-utc date) | |
4549ba4a | 716 | (let* ((jdays (- (priv:encode-julian-day-number (date-day date) |
5bbfe8cb RB |
717 | (date-month date) |
718 | (date-year date)) | |
4549ba4a MV |
719 | priv:tai-epoch-in-jd)) |
720 | ;; jdays is an integer plus 1/2, | |
721 | (jdays-1/2 (inexact->exact (- jdays 1/2)))) | |
afb47f6d | 722 | (make-time |
5bbfe8cb RB |
723 | time-utc |
724 | (date-nanosecond date) | |
4549ba4a | 725 | (+ (* jdays-1/2 24 60 60) |
5bbfe8cb RB |
726 | (* (date-hour date) 60 60) |
727 | (* (date-minute date) 60) | |
4549ba4a MV |
728 | (date-second date) |
729 | (- (date-zone-offset date)))))) | |
5bbfe8cb RB |
730 | |
731 | (define (date->time-tai date) | |
732 | (time-utc->time-tai! (date->time-utc date))) | |
733 | ||
734 | (define (date->time-monotonic date) | |
735 | (time-utc->time-monotonic! (date->time-utc date))) | |
736 | ||
737 | (define (priv:leap-year? year) | |
738 | (or (= (modulo year 400) 0) | |
739 | (and (= (modulo year 4) 0) (not (= (modulo year 100) 0))))) | |
740 | ||
741 | (define (leap-year? date) | |
742 | (priv:leap-year? (date-year date))) | |
743 | ||
359b471e NJ |
744 | ;; Map 1-based month number M to number of days in the year before the |
745 | ;; start of month M (in a non-leap year). | |
746 | (define priv:month-assoc '((1 . 0) (2 . 31) (3 . 59) (4 . 90) | |
747 | (5 . 120) (6 . 151) (7 . 181) (8 . 212) | |
748 | (9 . 243) (10 . 273) (11 . 304) (12 . 334))) | |
5bbfe8cb RB |
749 | |
750 | (define (priv:year-day day month year) | |
cd328b4f | 751 | (let ((days-pr (assoc month priv:month-assoc))) |
5bbfe8cb RB |
752 | (if (not days-pr) |
753 | (priv:error 'date-year-day 'invalid-month-specification month)) | |
754 | (if (and (priv:leap-year? year) (> month 2)) | |
755 | (+ day (cdr days-pr) 1) | |
756 | (+ day (cdr days-pr))))) | |
757 | ||
758 | (define (date-year-day date) | |
759 | (priv:year-day (date-day date) (date-month date) (date-year date))) | |
760 | ||
afb47f6d | 761 | ;; from calendar faq |
5bbfe8cb RB |
762 | (define (priv:week-day day month year) |
763 | (let* ((a (quotient (- 14 month) 12)) | |
764 | (y (- year a)) | |
765 | (m (+ month (* 12 a) -2))) | |
766 | (modulo (+ day | |
767 | y | |
768 | (quotient y 4) | |
769 | (- (quotient y 100)) | |
770 | (quotient y 400) | |
771 | (quotient (* 31 m) 12)) | |
772 | 7))) | |
773 | ||
774 | (define (date-week-day date) | |
775 | (priv:week-day (date-day date) (date-month date) (date-year date))) | |
776 | ||
777 | (define (priv:days-before-first-week date day-of-week-starting-week) | |
778 | (let* ((first-day (make-date 0 0 0 0 | |
779 | 1 | |
780 | 1 | |
781 | (date-year date) | |
782 | #f)) | |
783 | (fdweek-day (date-week-day first-day))) | |
784 | (modulo (- day-of-week-starting-week fdweek-day) | |
785 | 7))) | |
786 | ||
f3cc3dab KR |
787 | ;; The "-1" here is a fix for the reference implementation, to make a new |
788 | ;; week start on the given day-of-week-starting-week. date-year-day returns | |
789 | ;; a day starting from 1 for 1st Jan. | |
790 | ;; | |
5bbfe8cb RB |
791 | (define (date-week-number date day-of-week-starting-week) |
792 | (quotient (- (date-year-day date) | |
f3cc3dab | 793 | 1 |
5bbfe8cb RB |
794 | (priv:days-before-first-week date day-of-week-starting-week)) |
795 | 7)) | |
796 | ||
afb47f6d | 797 | (define (current-date . tz-offset) |
4549ba4a MV |
798 | (let ((time (current-time time-utc))) |
799 | (time-utc->date | |
800 | time | |
801 | (if (null? tz-offset) | |
802 | (priv:local-tz-offset time) | |
803 | (car tz-offset))))) | |
5bbfe8cb RB |
804 | |
805 | ;; given a 'two digit' number, find the year within 50 years +/- | |
806 | (define (priv:natural-year n) | |
807 | (let* ((current-year (date-year (current-date))) | |
808 | (current-century (* (quotient current-year 100) 100))) | |
809 | (cond | |
810 | ((>= n 100) n) | |
811 | ((< n 0) n) | |
812 | ((<= (- (+ current-century n) current-year) 50) (+ current-century n)) | |
813 | (else (+ (- current-century 100) n))))) | |
814 | ||
815 | (define (date->julian-day date) | |
816 | (let ((nanosecond (date-nanosecond date)) | |
817 | (second (date-second date)) | |
818 | (minute (date-minute date)) | |
819 | (hour (date-hour date)) | |
820 | (day (date-day date)) | |
821 | (month (date-month date)) | |
822 | (year (date-year date))) | |
823 | (+ (priv:encode-julian-day-number day month year) | |
824 | (- 1/2) | |
825 | (+ (/ (+ (* hour 60 60) | |
826 | (* minute 60) | |
827 | second | |
828 | (/ nanosecond priv:nano)) | |
829 | priv:sid))))) | |
830 | ||
831 | (define (date->modified-julian-day date) | |
832 | (- (date->julian-day date) | |
833 | 4800001/2)) | |
834 | ||
835 | (define (time-utc->julian-day time) | |
836 | (if (not (eq? (time-type time) time-utc)) | |
837 | (priv:time-error 'time->date 'incompatible-time-types time)) | |
838 | (+ (/ (+ (time-second time) (/ (time-nanosecond time) priv:nano)) | |
839 | priv:sid) | |
840 | priv:tai-epoch-in-jd)) | |
841 | ||
842 | (define (time-utc->modified-julian-day time) | |
843 | (- (time-utc->julian-day time) | |
844 | 4800001/2)) | |
845 | ||
846 | (define (time-tai->julian-day time) | |
847 | (if (not (eq? (time-type time) time-tai)) | |
848 | (priv:time-error 'time->date 'incompatible-time-types time)) | |
afb47f6d | 849 | (+ (/ (+ (- (time-second time) |
5bbfe8cb RB |
850 | (priv:leap-second-delta (time-second time))) |
851 | (/ (time-nanosecond time) priv:nano)) | |
852 | priv:sid) | |
853 | priv:tai-epoch-in-jd)) | |
854 | ||
855 | (define (time-tai->modified-julian-day time) | |
856 | (- (time-tai->julian-day time) | |
857 | 4800001/2)) | |
858 | ||
859 | ;; this is the same as time-tai->julian-day | |
860 | (define (time-monotonic->julian-day time) | |
861 | (if (not (eq? (time-type time) time-monotonic)) | |
862 | (priv:time-error 'time->date 'incompatible-time-types time)) | |
afb47f6d | 863 | (+ (/ (+ (- (time-second time) |
5bbfe8cb RB |
864 | (priv:leap-second-delta (time-second time))) |
865 | (/ (time-nanosecond time) priv:nano)) | |
866 | priv:sid) | |
867 | priv:tai-epoch-in-jd)) | |
868 | ||
869 | (define (time-monotonic->modified-julian-day time) | |
870 | (- (time-monotonic->julian-day time) | |
871 | 4800001/2)) | |
872 | ||
873 | (define (julian-day->time-utc jdn) | |
874 | (let ((secs (* priv:sid (- jdn priv:tai-epoch-in-jd)))) | |
875 | (receive (seconds parts) | |
4549ba4a | 876 | (priv:split-real secs) |
afb47f6d | 877 | (make-time time-utc |
4549ba4a MV |
878 | (* parts priv:nano) |
879 | seconds)))) | |
5bbfe8cb RB |
880 | |
881 | (define (julian-day->time-tai jdn) | |
882 | (time-utc->time-tai! (julian-day->time-utc jdn))) | |
883 | ||
884 | (define (julian-day->time-monotonic jdn) | |
885 | (time-utc->time-monotonic! (julian-day->time-utc jdn))) | |
886 | ||
887 | (define (julian-day->date jdn . tz-offset) | |
4549ba4a MV |
888 | (let* ((time (julian-day->time-utc jdn)) |
889 | (offset (if (null? tz-offset) | |
890 | (priv:local-tz-offset time) | |
891 | (car tz-offset)))) | |
892 | (time-utc->date time offset))) | |
5bbfe8cb RB |
893 | |
894 | (define (modified-julian-day->date jdn . tz-offset) | |
4549ba4a MV |
895 | (apply julian-day->date (+ jdn 4800001/2) |
896 | tz-offset)) | |
5bbfe8cb RB |
897 | |
898 | (define (modified-julian-day->time-utc jdn) | |
899 | (julian-day->time-utc (+ jdn 4800001/2))) | |
900 | ||
901 | (define (modified-julian-day->time-tai jdn) | |
902 | (julian-day->time-tai (+ jdn 4800001/2))) | |
903 | ||
904 | (define (modified-julian-day->time-monotonic jdn) | |
905 | (julian-day->time-monotonic (+ jdn 4800001/2))) | |
906 | ||
907 | (define (current-julian-day) | |
908 | (time-utc->julian-day (current-time time-utc))) | |
909 | ||
910 | (define (current-modified-julian-day) | |
911 | (time-utc->modified-julian-day (current-time time-utc))) | |
912 | ||
913 | ;; returns a string rep. of number N, of minimum LENGTH, padded with | |
914 | ;; character PAD-WITH. If PAD-WITH is #f, no padding is done, and it's | |
915 | ;; as if number->string was used. if string is longer than or equal | |
916 | ;; in length to LENGTH, it's as if number->string was used. | |
917 | ||
918 | (define (priv:padding n pad-with length) | |
919 | (let* ((str (number->string n)) | |
920 | (str-len (string-length str))) | |
921 | (if (or (>= str-len length) | |
922 | (not pad-with)) | |
923 | str | |
924 | (string-append (make-string (- length str-len) pad-with) str)))) | |
925 | ||
926 | (define (priv:last-n-digits i n) | |
927 | (abs (remainder i (expt 10 n)))) | |
928 | ||
a2f00b9b LC |
929 | (define priv:locale-abbr-weekday locale-day-short) |
930 | (define priv:locale-long-weekday locale-day) | |
931 | (define priv:locale-abbr-month locale-month-short) | |
932 | (define priv:locale-long-month locale-month) | |
933 | ||
934 | (define (priv:date-reverse-lookup needle haystack-ref haystack-len | |
935 | same?) | |
936 | ;; Lookup NEEDLE (a string) using HAYSTACK-REF (a one argument procedure | |
937 | ;; that returns a string corresponding to the given index) by passing it | |
938 | ;; indices lower than HAYSTACK-LEN. | |
939 | (let loop ((index 1)) | |
940 | (cond ((> index haystack-len) #f) | |
941 | ((same? needle (haystack-ref index)) | |
942 | index) | |
943 | (else (loop (+ index 1)))))) | |
5bbfe8cb RB |
944 | |
945 | (define (priv:locale-abbr-weekday->index string) | |
a2f00b9b | 946 | (priv:date-reverse-lookup string priv:locale-abbr-weekday 7 string=?)) |
5bbfe8cb RB |
947 | |
948 | (define (priv:locale-long-weekday->index string) | |
a2f00b9b | 949 | (priv:date-reverse-lookup string priv:locale-long-weekday 7 string=?)) |
5bbfe8cb RB |
950 | |
951 | (define (priv:locale-abbr-month->index string) | |
a2f00b9b | 952 | (priv:date-reverse-lookup string priv:locale-abbr-month 12 string=?)) |
5bbfe8cb RB |
953 | |
954 | (define (priv:locale-long-month->index string) | |
a2f00b9b | 955 | (priv:date-reverse-lookup string priv:locale-long-month 12 string=?)) |
5bbfe8cb RB |
956 | |
957 | ||
4549ba4a MV |
958 | ;; FIXME: mkoeppe: Put a symbolic time zone in the date structs. |
959 | ;; Print it here instead of the numerical offset if available. | |
5bbfe8cb | 960 | (define (priv:locale-print-time-zone date port) |
4549ba4a | 961 | (priv:tz-printer (date-zone-offset date) port)) |
5bbfe8cb | 962 | |
5bbfe8cb | 963 | (define (priv:locale-am/pm hr) |
a2f00b9b | 964 | (if (> hr 11) (priv:locale-pm) (priv:locale-am))) |
5bbfe8cb RB |
965 | |
966 | (define (priv:tz-printer offset port) | |
967 | (cond | |
968 | ((= offset 0) (display "Z" port)) | |
969 | ((negative? offset) (display "-" port)) | |
970 | (else (display "+" port))) | |
971 | (if (not (= offset 0)) | |
972 | (let ((hours (abs (quotient offset (* 60 60)))) | |
973 | (minutes (abs (quotient (remainder offset (* 60 60)) 60)))) | |
974 | (display (priv:padding hours #\0 2) port) | |
975 | (display (priv:padding minutes #\0 2) port)))) | |
976 | ||
5bbfe8cb RB |
977 | ;; A table of output formatting directives. |
978 | ;; the first time is the format char. | |
979 | ;; the second is a procedure that takes the date, a padding character | |
980 | ;; (which might be #f), and the output port. | |
981 | ;; | |
afb47f6d | 982 | (define priv:directives |
5bbfe8cb RB |
983 | (list |
984 | (cons #\~ (lambda (date pad-with port) | |
985 | (display #\~ port))) | |
986 | (cons #\a (lambda (date pad-with port) | |
987 | (display (priv:locale-abbr-weekday (date-week-day date)) | |
988 | port))) | |
989 | (cons #\A (lambda (date pad-with port) | |
990 | (display (priv:locale-long-weekday (date-week-day date)) | |
991 | port))) | |
992 | (cons #\b (lambda (date pad-with port) | |
993 | (display (priv:locale-abbr-month (date-month date)) | |
994 | port))) | |
995 | (cons #\B (lambda (date pad-with port) | |
996 | (display (priv:locale-long-month (date-month date)) | |
997 | port))) | |
998 | (cons #\c (lambda (date pad-with port) | |
999 | (display (date->string date priv:locale-date-time-format) port))) | |
1000 | (cons #\d (lambda (date pad-with port) | |
1001 | (display (priv:padding (date-day date) | |
1002 | #\0 2) | |
1003 | port))) | |
1004 | (cons #\D (lambda (date pad-with port) | |
1005 | (display (date->string date "~m/~d/~y") port))) | |
1006 | (cons #\e (lambda (date pad-with port) | |
1007 | (display (priv:padding (date-day date) | |
1008 | #\Space 2) | |
1009 | port))) | |
1010 | (cons #\f (lambda (date pad-with port) | |
1011 | (if (> (date-nanosecond date) | |
1012 | priv:nano) | |
1013 | (display (priv:padding (+ (date-second date) 1) | |
1014 | pad-with 2) | |
1015 | port) | |
1016 | (display (priv:padding (date-second date) | |
1017 | pad-with 2) | |
1018 | port)) | |
afb47f6d TTN |
1019 | (receive (i f) |
1020 | (priv:split-real (/ | |
5bbfe8cb RB |
1021 | (date-nanosecond date) |
1022 | priv:nano 1.0)) | |
1023 | (let* ((ns (number->string f)) | |
1024 | (le (string-length ns))) | |
1025 | (if (> le 2) | |
1026 | (begin | |
a2f00b9b | 1027 | (display (priv:locale-number-separator) port) |
5bbfe8cb RB |
1028 | (display (substring ns 2 le) port))))))) |
1029 | (cons #\h (lambda (date pad-with port) | |
1030 | (display (date->string date "~b") port))) | |
1031 | (cons #\H (lambda (date pad-with port) | |
1032 | (display (priv:padding (date-hour date) | |
1033 | pad-with 2) | |
1034 | port))) | |
1035 | (cons #\I (lambda (date pad-with port) | |
1036 | (let ((hr (date-hour date))) | |
1037 | (if (> hr 12) | |
1038 | (display (priv:padding (- hr 12) | |
1039 | pad-with 2) | |
1040 | port) | |
1041 | (display (priv:padding hr | |
1042 | pad-with 2) | |
1043 | port))))) | |
1044 | (cons #\j (lambda (date pad-with port) | |
1045 | (display (priv:padding (date-year-day date) | |
1046 | pad-with 3) | |
1047 | port))) | |
1048 | (cons #\k (lambda (date pad-with port) | |
1049 | (display (priv:padding (date-hour date) | |
1050 | #\Space 2) | |
1051 | port))) | |
1052 | (cons #\l (lambda (date pad-with port) | |
1053 | (let ((hr (if (> (date-hour date) 12) | |
1054 | (- (date-hour date) 12) (date-hour date)))) | |
1055 | (display (priv:padding hr #\Space 2) | |
1056 | port)))) | |
1057 | (cons #\m (lambda (date pad-with port) | |
1058 | (display (priv:padding (date-month date) | |
1059 | pad-with 2) | |
1060 | port))) | |
1061 | (cons #\M (lambda (date pad-with port) | |
1062 | (display (priv:padding (date-minute date) | |
1063 | pad-with 2) | |
1064 | port))) | |
1065 | (cons #\n (lambda (date pad-with port) | |
1066 | (newline port))) | |
1067 | (cons #\N (lambda (date pad-with port) | |
1068 | (display (priv:padding (date-nanosecond date) | |
1069 | pad-with 7) | |
1070 | port))) | |
1071 | (cons #\p (lambda (date pad-with port) | |
1072 | (display (priv:locale-am/pm (date-hour date)) port))) | |
1073 | (cons #\r (lambda (date pad-with port) | |
1074 | (display (date->string date "~I:~M:~S ~p") port))) | |
1075 | (cons #\s (lambda (date pad-with port) | |
1076 | (display (time-second (date->time-utc date)) port))) | |
1077 | (cons #\S (lambda (date pad-with port) | |
1078 | (if (> (date-nanosecond date) | |
1079 | priv:nano) | |
1080 | (display (priv:padding (+ (date-second date) 1) | |
1081 | pad-with 2) | |
1082 | port) | |
1083 | (display (priv:padding (date-second date) | |
1084 | pad-with 2) | |
1085 | port)))) | |
1086 | (cons #\t (lambda (date pad-with port) | |
1087 | (display #\Tab port))) | |
1088 | (cons #\T (lambda (date pad-with port) | |
1089 | (display (date->string date "~H:~M:~S") port))) | |
1090 | (cons #\U (lambda (date pad-with port) | |
1091 | (if (> (priv:days-before-first-week date 0) 0) | |
1092 | (display (priv:padding (+ (date-week-number date 0) 1) | |
1093 | #\0 2) port) | |
1094 | (display (priv:padding (date-week-number date 0) | |
1095 | #\0 2) port)))) | |
1096 | (cons #\V (lambda (date pad-with port) | |
1097 | (display (priv:padding (date-week-number date 1) | |
1098 | #\0 2) port))) | |
1099 | (cons #\w (lambda (date pad-with port) | |
1100 | (display (date-week-day date) port))) | |
1101 | (cons #\x (lambda (date pad-with port) | |
1102 | (display (date->string date priv:locale-short-date-format) port))) | |
1103 | (cons #\X (lambda (date pad-with port) | |
1104 | (display (date->string date priv:locale-time-format) port))) | |
1105 | (cons #\W (lambda (date pad-with port) | |
1106 | (if (> (priv:days-before-first-week date 1) 0) | |
1107 | (display (priv:padding (+ (date-week-number date 1) 1) | |
1108 | #\0 2) port) | |
1109 | (display (priv:padding (date-week-number date 1) | |
1110 | #\0 2) port)))) | |
1111 | (cons #\y (lambda (date pad-with port) | |
afb47f6d | 1112 | (display (priv:padding (priv:last-n-digits |
5bbfe8cb RB |
1113 | (date-year date) 2) |
1114 | pad-with | |
1115 | 2) | |
1116 | port))) | |
1117 | (cons #\Y (lambda (date pad-with port) | |
1118 | (display (date-year date) port))) | |
1119 | (cons #\z (lambda (date pad-with port) | |
1120 | (priv:tz-printer (date-zone-offset date) port))) | |
1121 | (cons #\Z (lambda (date pad-with port) | |
1122 | (priv:locale-print-time-zone date port))) | |
1123 | (cons #\1 (lambda (date pad-with port) | |
1124 | (display (date->string date "~Y-~m-~d") port))) | |
1125 | (cons #\2 (lambda (date pad-with port) | |
1126 | (display (date->string date "~k:~M:~S~z") port))) | |
1127 | (cons #\3 (lambda (date pad-with port) | |
1128 | (display (date->string date "~k:~M:~S") port))) | |
1129 | (cons #\4 (lambda (date pad-with port) | |
1130 | (display (date->string date "~Y-~m-~dT~k:~M:~S~z") port))) | |
1131 | (cons #\5 (lambda (date pad-with port) | |
1132 | (display (date->string date "~Y-~m-~dT~k:~M:~S") port))))) | |
1133 | ||
1134 | ||
1135 | (define (priv:get-formatter char) | |
1136 | (let ((associated (assoc char priv:directives))) | |
1137 | (if associated (cdr associated) #f))) | |
1138 | ||
1139 | (define (priv:date-printer date index format-string str-len port) | |
1140 | (if (>= index str-len) | |
1141 | (values) | |
1142 | (let ((current-char (string-ref format-string index))) | |
1143 | (if (not (char=? current-char #\~)) | |
1144 | (begin | |
1145 | (display current-char port) | |
1146 | (priv:date-printer date (+ index 1) format-string str-len port)) | |
1147 | (if (= (+ index 1) str-len) ; bad format string. | |
afb47f6d | 1148 | (priv:time-error 'priv:date-printer 'bad-date-format-string |
5bbfe8cb RB |
1149 | format-string) |
1150 | (let ((pad-char? (string-ref format-string (+ index 1)))) | |
1151 | (cond | |
1152 | ((char=? pad-char? #\-) | |
1153 | (if (= (+ index 2) str-len) ; bad format string. | |
1154 | (priv:time-error 'priv:date-printer | |
afb47f6d | 1155 | 'bad-date-format-string |
5bbfe8cb | 1156 | format-string) |
afb47f6d | 1157 | (let ((formatter (priv:get-formatter |
5bbfe8cb RB |
1158 | (string-ref format-string |
1159 | (+ index 2))))) | |
1160 | (if (not formatter) | |
1161 | (priv:time-error 'priv:date-printer | |
afb47f6d | 1162 | 'bad-date-format-string |
5bbfe8cb RB |
1163 | format-string) |
1164 | (begin | |
1165 | (formatter date #f port) | |
1166 | (priv:date-printer date | |
1167 | (+ index 3) | |
1168 | format-string | |
1169 | str-len | |
1170 | port)))))) | |
afb47f6d | 1171 | |
5bbfe8cb RB |
1172 | ((char=? pad-char? #\_) |
1173 | (if (= (+ index 2) str-len) ; bad format string. | |
1174 | (priv:time-error 'priv:date-printer | |
afb47f6d | 1175 | 'bad-date-format-string |
5bbfe8cb | 1176 | format-string) |
afb47f6d | 1177 | (let ((formatter (priv:get-formatter |
5bbfe8cb RB |
1178 | (string-ref format-string |
1179 | (+ index 2))))) | |
1180 | (if (not formatter) | |
1181 | (priv:time-error 'priv:date-printer | |
afb47f6d | 1182 | 'bad-date-format-string |
5bbfe8cb RB |
1183 | format-string) |
1184 | (begin | |
1185 | (formatter date #\Space port) | |
1186 | (priv:date-printer date | |
1187 | (+ index 3) | |
1188 | format-string | |
1189 | str-len | |
1190 | port)))))) | |
1191 | (else | |
afb47f6d | 1192 | (let ((formatter (priv:get-formatter |
5bbfe8cb RB |
1193 | (string-ref format-string |
1194 | (+ index 1))))) | |
1195 | (if (not formatter) | |
1196 | (priv:time-error 'priv:date-printer | |
afb47f6d | 1197 | 'bad-date-format-string |
5bbfe8cb RB |
1198 | format-string) |
1199 | (begin | |
1200 | (formatter date #\0 port) | |
1201 | (priv:date-printer date | |
1202 | (+ index 2) | |
1203 | format-string | |
1204 | str-len | |
1205 | port)))))))))))) | |
1206 | ||
1207 | ||
1208 | (define (date->string date . format-string) | |
1209 | (let ((str-port (open-output-string)) | |
5e1fb41f | 1210 | (fmt-str (if (null? format-string) "~c" (car format-string)))) |
5bbfe8cb RB |
1211 | (priv:date-printer date 0 fmt-str (string-length fmt-str) str-port) |
1212 | (get-output-string str-port))) | |
1213 | ||
1214 | (define (priv:char->int ch) | |
1215 | (case ch | |
1216 | ((#\0) 0) | |
1217 | ((#\1) 1) | |
1218 | ((#\2) 2) | |
1219 | ((#\3) 3) | |
1220 | ((#\4) 4) | |
1221 | ((#\5) 5) | |
1222 | ((#\6) 6) | |
1223 | ((#\7) 7) | |
1224 | ((#\8) 8) | |
1225 | ((#\9) 9) | |
1226 | (else (priv:time-error 'bad-date-template-string | |
1227 | (list "Non-integer character" ch i))))) | |
1228 | ||
1229 | ;; read an integer upto n characters long on port; upto -> #f is any length | |
1230 | (define (priv:integer-reader upto port) | |
1231 | (let loop ((accum 0) (nchars 0)) | |
1232 | (let ((ch (peek-char port))) | |
1233 | (if (or (eof-object? ch) | |
1234 | (not (char-numeric? ch)) | |
1235 | (and upto (>= nchars upto))) | |
1236 | accum | |
4549ba4a | 1237 | (loop (+ (* accum 10) (priv:char->int (read-char port))) |
5bbfe8cb RB |
1238 | (+ nchars 1)))))) |
1239 | ||
1240 | (define (priv:make-integer-reader upto) | |
1241 | (lambda (port) | |
1242 | (priv:integer-reader upto port))) | |
1243 | ||
1244 | ;; read *exactly* n characters and convert to integer; could be padded | |
1245 | (define (priv:integer-reader-exact n port) | |
1246 | (let ((padding-ok #t)) | |
1247 | (define (accum-int port accum nchars) | |
1248 | (let ((ch (peek-char port))) | |
5a1920de RB |
1249 | (cond |
1250 | ((>= nchars n) accum) | |
afb47f6d TTN |
1251 | ((eof-object? ch) |
1252 | (priv:time-error 'string->date 'bad-date-template-string | |
5bbfe8cb | 1253 | "Premature ending to integer read.")) |
5a1920de RB |
1254 | ((char-numeric? ch) |
1255 | (set! padding-ok #f) | |
1256 | (accum-int port | |
1257 | (+ (* accum 10) (priv:char->int (read-char port))) | |
1258 | (+ nchars 1))) | |
1259 | (padding-ok | |
1260 | (read-char port) ; consume padding | |
1261 | (accum-int port accum (+ nchars 1))) | |
1262 | (else ; padding where it shouldn't be | |
afb47f6d | 1263 | (priv:time-error 'string->date 'bad-date-template-string |
5bbfe8cb RB |
1264 | "Non-numeric characters in integer read."))))) |
1265 | (accum-int port 0 0))) | |
1266 | ||
1267 | ||
1268 | (define (priv:make-integer-exact-reader n) | |
1269 | (lambda (port) | |
1270 | (priv:integer-reader-exact n port))) | |
1271 | ||
afb47f6d TTN |
1272 | (define (priv:zone-reader port) |
1273 | (let ((offset 0) | |
5bbfe8cb RB |
1274 | (positive? #f)) |
1275 | (let ((ch (read-char port))) | |
1276 | (if (eof-object? ch) | |
1277 | (priv:time-error 'string->date 'bad-date-template-string | |
1278 | (list "Invalid time zone +/-" ch))) | |
1279 | (if (or (char=? ch #\Z) (char=? ch #\z)) | |
1280 | 0 | |
1281 | (begin | |
1282 | (cond | |
1283 | ((char=? ch #\+) (set! positive? #t)) | |
1284 | ((char=? ch #\-) (set! positive? #f)) | |
1285 | (else | |
1286 | (priv:time-error 'string->date 'bad-date-template-string | |
1287 | (list "Invalid time zone +/-" ch)))) | |
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 (* (priv:char->int ch) | |
1293 | 10 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 | 60 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 | 10 60)))) | |
1306 | (let ((ch (read-char port))) | |
1307 | (if (eof-object? ch) | |
1308 | (priv:time-error 'string->date 'bad-date-template-string | |
1309 | (list "Invalid time zone number" ch))) | |
1310 | (set! offset (+ offset (* (priv:char->int ch) | |
1311 | 60)))) | |
1312 | (if positive? offset (- offset))))))) | |
1313 | ||
1314 | ;; looking at a char, read the char string, run thru indexer, return index | |
1315 | (define (priv:locale-reader port indexer) | |
b4d2a48e RB |
1316 | |
1317 | (define (read-char-string result) | |
1318 | (let ((ch (peek-char port))) | |
1319 | (if (char-alphabetic? ch) | |
1320 | (read-char-string (cons (read-char port) result)) | |
1321 | (list->string (reverse! result))))) | |
afb47f6d TTN |
1322 | |
1323 | (let* ((str (read-char-string '())) | |
b4d2a48e RB |
1324 | (index (indexer str))) |
1325 | (if index index (priv:time-error 'string->date | |
1326 | 'bad-date-template-string | |
1327 | (list "Invalid string for " indexer))))) | |
5bbfe8cb RB |
1328 | |
1329 | (define (priv:make-locale-reader indexer) | |
1330 | (lambda (port) | |
1331 | (priv:locale-reader port indexer))) | |
1332 | ||
1333 | (define (priv:make-char-id-reader char) | |
1334 | (lambda (port) | |
1335 | (if (char=? char (read-char port)) | |
1336 | char | |
1337 | (priv:time-error 'string->date | |
1338 | 'bad-date-template-string | |
1339 | "Invalid character match.")))) | |
1340 | ||
1341 | ;; A List of formatted read directives. | |
1342 | ;; Each entry is a list. | |
afb47f6d | 1343 | ;; 1. the character directive; |
5bbfe8cb RB |
1344 | ;; a procedure, which takes a character as input & returns |
1345 | ;; 2. #t as soon as a character on the input port is acceptable | |
1346 | ;; for input, | |
1347 | ;; 3. a port reader procedure that knows how to read the current port | |
1348 | ;; for a value. Its one parameter is the port. | |
1349 | ;; 4. a action procedure, that takes the value (from 3.) and some | |
1350 | ;; object (here, always the date) and (probably) side-effects it. | |
1351 | ;; In some cases (e.g., ~A) the action is to do nothing | |
1352 | ||
afb47f6d | 1353 | (define priv:read-directives |
5bbfe8cb RB |
1354 | (let ((ireader4 (priv:make-integer-reader 4)) |
1355 | (ireader2 (priv:make-integer-reader 2)) | |
1356 | (ireaderf (priv:make-integer-reader #f)) | |
1357 | (eireader2 (priv:make-integer-exact-reader 2)) | |
1358 | (eireader4 (priv:make-integer-exact-reader 4)) | |
1359 | (locale-reader-abbr-weekday (priv:make-locale-reader | |
1360 | priv:locale-abbr-weekday->index)) | |
1361 | (locale-reader-long-weekday (priv:make-locale-reader | |
1362 | priv:locale-long-weekday->index)) | |
1363 | (locale-reader-abbr-month (priv:make-locale-reader | |
1364 | priv:locale-abbr-month->index)) | |
1365 | (locale-reader-long-month (priv:make-locale-reader | |
1366 | priv:locale-long-month->index)) | |
1367 | (char-fail (lambda (ch) #t)) | |
1368 | (do-nothing (lambda (val object) (values)))) | |
afb47f6d | 1369 | |
5bbfe8cb RB |
1370 | (list |
1371 | (list #\~ char-fail (priv:make-char-id-reader #\~) do-nothing) | |
1372 | (list #\a char-alphabetic? locale-reader-abbr-weekday do-nothing) | |
1373 | (list #\A char-alphabetic? locale-reader-long-weekday do-nothing) | |
1374 | (list #\b char-alphabetic? locale-reader-abbr-month | |
1375 | (lambda (val object) | |
4549ba4a | 1376 | (set-date-month! object val))) |
5bbfe8cb RB |
1377 | (list #\B char-alphabetic? locale-reader-long-month |
1378 | (lambda (val object) | |
4549ba4a | 1379 | (set-date-month! object val))) |
5bbfe8cb | 1380 | (list #\d char-numeric? ireader2 (lambda (val object) |
4549ba4a | 1381 | (set-date-day! |
5bbfe8cb RB |
1382 | object val))) |
1383 | (list #\e char-fail eireader2 (lambda (val object) | |
4549ba4a | 1384 | (set-date-day! object val))) |
5bbfe8cb RB |
1385 | (list #\h char-alphabetic? locale-reader-abbr-month |
1386 | (lambda (val object) | |
4549ba4a | 1387 | (set-date-month! object val))) |
5bbfe8cb | 1388 | (list #\H char-numeric? ireader2 (lambda (val object) |
4549ba4a | 1389 | (set-date-hour! object val))) |
5bbfe8cb | 1390 | (list #\k char-fail eireader2 (lambda (val object) |
4549ba4a | 1391 | (set-date-hour! object val))) |
5bbfe8cb | 1392 | (list #\m char-numeric? ireader2 (lambda (val object) |
4549ba4a | 1393 | (set-date-month! object val))) |
5bbfe8cb | 1394 | (list #\M char-numeric? ireader2 (lambda (val object) |
4549ba4a | 1395 | (set-date-minute! |
5bbfe8cb RB |
1396 | object val))) |
1397 | (list #\S char-numeric? ireader2 (lambda (val object) | |
4549ba4a | 1398 | (set-date-second! object val))) |
afb47f6d | 1399 | (list #\y char-fail eireader2 |
5bbfe8cb | 1400 | (lambda (val object) |
4549ba4a | 1401 | (set-date-year! object (priv:natural-year val)))) |
5bbfe8cb | 1402 | (list #\Y char-numeric? ireader4 (lambda (val object) |
4549ba4a | 1403 | (set-date-year! object val))) |
5bbfe8cb RB |
1404 | (list #\z (lambda (c) |
1405 | (or (char=? c #\Z) | |
1406 | (char=? c #\z) | |
1407 | (char=? c #\+) | |
1408 | (char=? c #\-))) | |
1409 | priv:zone-reader (lambda (val object) | |
4549ba4a | 1410 | (set-date-zone-offset! object val)))))) |
5bbfe8cb RB |
1411 | |
1412 | (define (priv:string->date date index format-string str-len port template-string) | |
1413 | (define (skip-until port skipper) | |
1414 | (let ((ch (peek-char port))) | |
1415 | (if (eof-object? port) | |
1416 | (priv:time-error 'string->date 'bad-date-format-string template-string) | |
1417 | (if (not (skipper ch)) | |
1418 | (begin (read-char port) (skip-until port skipper)))))) | |
1419 | (if (>= index str-len) | |
afb47f6d | 1420 | (begin |
5bbfe8cb RB |
1421 | (values)) |
1422 | (let ((current-char (string-ref format-string index))) | |
1423 | (if (not (char=? current-char #\~)) | |
1424 | (let ((port-char (read-char port))) | |
1425 | (if (or (eof-object? port-char) | |
1426 | (not (char=? current-char port-char))) | |
1427 | (priv:time-error 'string->date | |
1428 | 'bad-date-format-string template-string)) | |
1429 | (priv:string->date date | |
1430 | (+ index 1) | |
1431 | format-string | |
1432 | str-len | |
1433 | port | |
1434 | template-string)) | |
1435 | ;; otherwise, it's an escape, we hope | |
1436 | (if (> (+ index 1) str-len) | |
1437 | (priv:time-error 'string->date | |
1438 | 'bad-date-format-string template-string) | |
1439 | (let* ((format-char (string-ref format-string (+ index 1))) | |
1440 | (format-info (assoc format-char priv:read-directives))) | |
1441 | (if (not format-info) | |
1442 | (priv:time-error 'string->date | |
1443 | 'bad-date-format-string template-string) | |
1444 | (begin | |
1445 | (let ((skipper (cadr format-info)) | |
1446 | (reader (caddr format-info)) | |
1447 | (actor (cadddr format-info))) | |
1448 | (skip-until port skipper) | |
1449 | (let ((val (reader port))) | |
1450 | (if (eof-object? val) | |
1451 | (priv:time-error 'string->date | |
1452 | 'bad-date-format-string | |
1453 | template-string) | |
1454 | (actor val date))) | |
1455 | (priv:string->date date | |
1456 | (+ index 2) | |
afb47f6d | 1457 | format-string |
5bbfe8cb RB |
1458 | str-len |
1459 | port | |
1460 | template-string)))))))))) | |
1461 | ||
1462 | (define (string->date input-string template-string) | |
1463 | (define (priv:date-ok? date) | |
1464 | (and (date-nanosecond date) | |
1465 | (date-second date) | |
1466 | (date-minute date) | |
1467 | (date-hour date) | |
1468 | (date-day date) | |
1469 | (date-month date) | |
1470 | (date-year date) | |
1471 | (date-zone-offset date))) | |
4549ba4a | 1472 | (let ((newdate (make-date 0 0 0 0 #f #f #f #f))) |
5bbfe8cb RB |
1473 | (priv:string->date newdate |
1474 | 0 | |
1475 | template-string | |
1476 | (string-length template-string) | |
1477 | (open-input-string input-string) | |
1478 | template-string) | |
4549ba4a MV |
1479 | (if (not (date-zone-offset newdate)) |
1480 | (begin | |
1481 | ;; this is necessary to get DST right -- as far as we can | |
1482 | ;; get it right (think of the double/missing hour in the | |
1483 | ;; night when we are switching between normal time and DST). | |
1484 | (set-date-zone-offset! newdate | |
afb47f6d | 1485 | (priv:local-tz-offset |
4549ba4a MV |
1486 | (make-time time-utc 0 0))) |
1487 | (set-date-zone-offset! newdate | |
afb47f6d | 1488 | (priv:local-tz-offset |
4549ba4a | 1489 | (date->time-utc newdate))))) |
5bbfe8cb RB |
1490 | (if (priv:date-ok? newdate) |
1491 | newdate | |
1492 | (priv:time-error | |
1493 | 'string->date | |
1494 | 'bad-date-format-string | |
1495 | (list "Incomplete date read. " newdate template-string))))) | |
afb47f6d TTN |
1496 | |
1497 | ;;; srfi-19.scm ends here |