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