| 1 |
;; SRFI-19: Time Data Types and Procedures. |
| 2 |
;; |
| 3 |
;; Copyright (C) I/NET, Inc. (2000, 2002, 2003). All Rights Reserved. |
| 4 |
;; |
| 5 |
;; Permission is hereby granted, free of charge, to any person obtaining |
| 6 |
;; a copy of this software and associated documentation files (the |
| 7 |
;; "Software"), to deal in the Software without restriction, including |
| 8 |
;; without limitation the rights to use, copy, modify, merge, publish, |
| 9 |
;; distribute, sublicense, and/or sell copies of the Software, and to |
| 10 |
;; permit persons to whom the Software is furnished to do so, subject to |
| 11 |
;; the following conditions: |
| 12 |
;; |
| 13 |
;; The above copyright notice and this permission notice shall be |
| 14 |
;; included in all copies or substantial portions of the Software. |
| 15 |
;; |
| 16 |
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 17 |
;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 18 |
;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 19 |
;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 20 |
;; LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 21 |
;; OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 22 |
;; WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 23 |
|
| 24 |
;;; Modified for Gauche by Shiro Kawai, [email protected] |
| 25 |
|
| 26 |
(define-module srfi.19 |
| 27 |
(use scheme.list) |
| 28 |
(use gauche.sequence) |
| 29 |
(use srfi.13) |
| 30 |
(export time-tai time-utc time-monotonic time-thread |
| 31 |
time-process time-duration current-time time-resolution |
| 32 |
make-time time? time-type time-second time-nanosecond |
| 33 |
set-time-type! set-time-second! set-time-nanosecond! copy-time |
| 34 |
time=? time<? time<=? time>? time>=? |
| 35 |
time-difference time-difference! add-duration add-duration! |
| 36 |
subtract-duration subtract-duration! |
| 37 |
make-date date? date-nanosecond date-second date-minute |
| 38 |
date-hour date-day date-month date-year date-zone-offset |
| 39 |
date-year-day date-week-day date-week-number current-date |
| 40 |
current-julian-day current-modified-julian-day |
| 41 |
date->julian-day date->modified-julian-day date->time-monotonic |
| 42 |
date->time-tai date->time-utc |
| 43 |
julian-day->date julian-day->time-monotonic |
| 44 |
julian-day->time-tai julian-day->time-utc |
| 45 |
modified-julian-day->date modified-julian-day->time-monotonic |
| 46 |
modified-julian-day->time-tai modified-julian-day->time-utc |
| 47 |
time-monotonic->date time-monotonic->julian-day |
| 48 |
time-monotonic->modified-julian-day |
| 49 |
time-monotonic->time-tai time-monotonic->time-tai! |
| 50 |
time-monotonic->time-utc time-monotonic->time-utc! |
| 51 |
time-utc->date time-utc->julian-day |
| 52 |
time-utc->modified-julian-day |
| 53 |
time-utc->time-monotonic time-utc->time-monotonic! |
| 54 |
time-utc->time-tai time-utc->time-tai! |
| 55 |
time-tai->date time-tai->julian-day |
| 56 |
time-tai->modified-julian-day |
| 57 |
time-tai->time-monotonic time-tai->time-monotonic! |
| 58 |
time-tai->time-utc time-tai->time-utc! |
| 59 |
date->string string->date <date> |
| 60 |
) |
| 61 |
) |
| 62 |
(select-module srfi.19) |
| 63 |
|
| 64 |
;;;---------------------------------------------------------- |
| 65 |
;;; Constants |
| 66 |
;;; |
| 67 |
|
| 68 |
(define-constant time-tai 'time-tai) |
| 69 |
(define-constant time-utc 'time-utc) |
| 70 |
(define-constant time-monotonic 'time-monotonic) |
| 71 |
(define-constant time-thread 'time-thread) |
| 72 |
(define-constant time-process 'time-process) |
| 73 |
(define-constant time-duration 'time-duration) |
| 74 |
|
| 75 |
;; example of extension (MZScheme specific) |
| 76 |
;(define time-gc 'time-gc) |
| 77 |
|
| 78 |
;;-- Miscellaneous Constants. |
| 79 |
;;-- only the tm:tai-epoch-in-jd might need changing if |
| 80 |
;; a different epoch is used. |
| 81 |
|
| 82 |
;; NB: If the constant TM_EXACT_CALC is true, the calculation of julian-day |
| 83 |
;; and modified-julian-day is done in exact calculation. It would have some |
| 84 |
;; penalty. Here's some small benchmark results of round-trip conversion from |
| 85 |
;; time-utc -> julian-day -> time-utc, repeating 100000 times on 0.8.12. |
| 86 |
;; |
| 87 |
;; current-time day-boundary half-day-boundary |
| 88 |
;; exact 4.67 3.17 2.76 |
| 89 |
;; inexact 1.67 1.95 1.94 |
| 90 |
;; |
| 91 |
;; 'day-boundary' uses (make-time time-utc 0 86400) |
| 92 |
;; 'half-day-boundary' uses (make-time time-utc 0 43200) |
| 93 |
;; |
| 94 |
;; It does slow down the calculation, but I'd say it's not so bad. I'll |
| 95 |
;; go for exact calculation for now. |
| 96 |
(define-constant TM_EXACT_CALC #t) |
| 97 |
|
| 98 |
(define-constant tm:nano 1000000000) |
| 99 |
(define-constant tm:nano/i (if TM_EXACT_CALC tm:nano (exact->inexact tm:nano))) |
| 100 |
(define-constant tm:sid 86400) ; seconds in a day |
| 101 |
(define-constant tm:sihd 43200) ; seconds in a half day |
| 102 |
(define-constant tm:tai-epoch-in-jd ; julian day number for 'the epoch' |
| 103 |
(let1 off 4881175/2 |
| 104 |
(if TM_EXACT_CALC off (exact->inexact off)))) |
| 105 |
|
| 106 |
;; each entry is ( tai seconds since epoch . # seconds to subtract for utc ) |
| 107 |
;; note they go higher to lower, and end in 1972. |
| 108 |
;; See srfi-19/read-tai.scm to update this list. |
| 109 |
(define-constant tm:leap-second-table |
| 110 |
'((1483228800 . 37) |
| 111 |
(1435708800 . 36) |
| 112 |
(1341100800 . 35) |
| 113 |
(1230768000 . 34) |
| 114 |
(1136073600 . 33) |
| 115 |
(915148800 . 32) |
| 116 |
(867715200 . 31) |
| 117 |
(820454400 . 30) |
| 118 |
(773020800 . 29) |
| 119 |
(741484800 . 28) |
| 120 |
(709948800 . 27) |
| 121 |
(662688000 . 26) |
| 122 |
(631152000 . 25) |
| 123 |
(567993600 . 24) |
| 124 |
(489024000 . 23) |
| 125 |
(425865600 . 22) |
| 126 |
(394329600 . 21) |
| 127 |
(362793600 . 20) |
| 128 |
(315532800 . 19) |
| 129 |
(283996800 . 18) |
| 130 |
(252460800 . 17) |
| 131 |
(220924800 . 16) |
| 132 |
(189302400 . 15) |
| 133 |
(157766400 . 14) |
| 134 |
(126230400 . 13) |
| 135 |
(94694400 . 12) |
| 136 |
(78796800 . 11) |
| 137 |
(63072000 . 10))) |
| 138 |
|
| 139 |
(define-constant tm:leap-second-base |
| 140 |
(* (- 1972 1970) 365 tm:sid)) |
| 141 |
|
| 142 |
(define (tm:leap-second-delta utc-seconds) |
| 143 |
(if (< utc-seconds tm:leap-second-base) |
| 144 |
0 |
| 145 |
(let loop ((table tm:leap-second-table)) |
| 146 |
(if (>= utc-seconds (caar table)) |
| 147 |
(cdar table) |
| 148 |
(loop (cdr table)))))) |
| 149 |
|
| 150 |
;;;---------------------------------------------------------- |
| 151 |
;;; TIME strcture interface |
| 152 |
;;; The <time> class is built-in. We just define some APIs. |
| 153 |
|
| 154 |
(define-method time-type ((t <time>)) (slot-ref t 'type)) |
| 155 |
(define-method time-second ((t <time>)) (slot-ref t 'second)) |
| 156 |
(define-method time-nanosecond ((t <time>)) (slot-ref t 'nanosecond)) |
| 157 |
|
| 158 |
(define-method set-time-type! ((t <time>) s) |
| 159 |
(slot-set! t 'type s)) |
| 160 |
(define-method set-time-second! ((t <time>) s) |
| 161 |
(slot-set! t 'second s)) |
| 162 |
(define-method set-time-nanosecond! ((t <time>) s) |
| 163 |
(slot-set! t 'nanosecond s)) |
| 164 |
|
| 165 |
(define (make-time type nanosecond second) |
| 166 |
(unless (<= 0 nanosecond 999999999) |
| 167 |
(error "make-time: nanosecond out of range:" nanosecond)) |
| 168 |
(make <time> :type type :second second :nanosecond nanosecond)) |
| 169 |
|
| 170 |
(define (copy-time time) |
| 171 |
(make <time> |
| 172 |
:type (time-type time) |
| 173 |
:second (time-second time) |
| 174 |
:nanosecond (time-nanosecond time))) |
| 175 |
|
| 176 |
;;;---------------------------------------------------------- |
| 177 |
;;; Error check routine |
| 178 |
;;; |
| 179 |
|
| 180 |
(define-syntax tm:check-time-type |
| 181 |
(syntax-rules () |
| 182 |
[(_ time type caller) |
| 183 |
(unless (eq? (time-type time) type) |
| 184 |
(errorf "~a: incompatible time type: ~a type required, but got ~a" |
| 185 |
caller type time))])) |
| 186 |
|
| 187 |
;;;---------------------------------------------------------- |
| 188 |
;;; Current-time |
| 189 |
;;; |
| 190 |
|
| 191 |
(define (tm:make-time-usec type sec usec) |
| 192 |
(make-time type (* usec 1000) sec)) |
| 193 |
|
| 194 |
(define (tm:current-time-process type) |
| 195 |
(let* ([times (sys-times)] |
| 196 |
[cpu (+ (car times) (cadr times))] |
| 197 |
[tick (list-ref times 4)] |
| 198 |
[sec (quotient cpu tick)] |
| 199 |
[nsec (* (/. tm:nano tick) (remainder cpu tick))]) |
| 200 |
(make-time type nsec sec))) |
| 201 |
|
| 202 |
(define (tm:current-time-tai type) |
| 203 |
(let* ([now (with-module gauche (current-time))] |
| 204 |
[sec (slot-ref now 'second)]) |
| 205 |
(make <time> :type type :second (+ sec (tm:leap-second-delta sec)) |
| 206 |
:nanosecond (slot-ref now 'nanosecond)))) |
| 207 |
|
| 208 |
|
| 209 |
;; redefine built-in current-time |
| 210 |
(define (current-time :optional (clock-type 'time-utc)) |
| 211 |
(case clock-type |
| 212 |
[(time-tai) (tm:current-time-tai clock-type)] |
| 213 |
[(time-utc) (with-module gauche (current-time))] |
| 214 |
[(time-monotonic) (tm:current-time-tai clock-type)] |
| 215 |
[(time-thread) (tm:current-time-process 'time-thread)] |
| 216 |
[(time-process) (tm:current-time-process 'time-process)] |
| 217 |
[else (error "current-time: invalid-clock-type" clock-type)])) |
| 218 |
|
| 219 |
;; -- Time Resolution |
| 220 |
;; This is the resolution of the clock in nanoseconds. |
| 221 |
|
| 222 |
;; We don't really know ... for now, just return 10ms. |
| 223 |
(define (time-resolution . args) |
| 224 |
10000000) |
| 225 |
|
| 226 |
;; -- Time comparisons |
| 227 |
;; [SK] we can use builtin comparison. |
| 228 |
(define-syntax define-tm:cmp |
| 229 |
(syntax-rules () |
| 230 |
[(_ (name time1 time2) expr) |
| 231 |
(define (name time1 time2) |
| 232 |
(unless (time? time1) |
| 233 |
(errorf "~a: time objects are required, but got ~s" 'name time1)) |
| 234 |
(unless (time? time2) |
| 235 |
(errorf "~a: time objects are required, but got ~s" 'name time2)) |
| 236 |
expr)])) |
| 237 |
|
| 238 |
(define-tm:cmp (time=? time1 time2) (equal? time1 time2)) |
| 239 |
(define-tm:cmp (time>? time1 time2) (> (compare time1 time2) 0)) |
| 240 |
(define-tm:cmp (time<? time1 time2) (< (compare time1 time2) 0)) |
| 241 |
(define-tm:cmp (time>=? time1 time2) (>= (compare time1 time2) 0)) |
| 242 |
(define-tm:cmp (time<=? time1 time2) (<= (compare time1 time2) 0)) |
| 243 |
|
| 244 |
;; -- Time arithmetic |
| 245 |
|
| 246 |
(define (tm:time-difference time1 time2 time3) |
| 247 |
(if (or (not (and (time? time1) (time? time2))) |
| 248 |
(not (eq? (time-type time1) (time-type time2)))) |
| 249 |
(errorf "time-difference: incompatible time types: ~s, ~s" time1 time2) |
| 250 |
(let ([sec-diff (- (time-second time1) (time-second time2))] |
| 251 |
[nsec-diff (- (time-nanosecond time1) (time-nanosecond time2))]) |
| 252 |
(set-time-type! time3 time-duration) |
| 253 |
(if (negative? nsec-diff) |
| 254 |
(begin |
| 255 |
(set-time-second! time3 (- sec-diff 1)) |
| 256 |
(set-time-nanosecond! time3 (+ tm:nano nsec-diff))) |
| 257 |
(begin |
| 258 |
(set-time-second! time3 sec-diff) |
| 259 |
(set-time-nanosecond! time3 nsec-diff))) |
| 260 |
time3))) |
| 261 |
|
| 262 |
(define (time-difference time1 time2) |
| 263 |
(tm:time-difference time1 time2 (make <time>))) |
| 264 |
|
| 265 |
(define (time-difference! time1 time2) |
| 266 |
(tm:time-difference time1 time2 time1)) |
| 267 |
|
| 268 |
(define (tm:add-duration time1 duration time3) |
| 269 |
(when (not (and (time? time1) (time? duration))) |
| 270 |
(errorf "add-duration: incompatible type types: ~a ~a" |
| 271 |
time1 duration)) |
| 272 |
(tm:check-time-type duration 'time-duration 'add-duration) |
| 273 |
(let ([sec-plus (+ (time-second time1) (time-second duration))] |
| 274 |
[nsec-plus (+ (time-nanosecond time1) (time-nanosecond duration))]) |
| 275 |
(let ([r (remainder nsec-plus tm:nano)] |
| 276 |
[q (quotient nsec-plus tm:nano)]) |
| 277 |
(if (negative? r) |
| 278 |
(begin |
| 279 |
(set-time-second! time3 (+ sec-plus q -1)) |
| 280 |
(set-time-nanosecond! time3 (+ tm:nano r))) |
| 281 |
(begin |
| 282 |
(set-time-second! time3 (+ sec-plus q)) |
| 283 |
(set-time-nanosecond! time3 r))) |
| 284 |
time3))) |
| 285 |
|
| 286 |
(define (add-duration time1 duration) |
| 287 |
(tm:add-duration time1 duration (make <time> :type (time-type time1)))) |
| 288 |
|
| 289 |
(define (add-duration! time1 duration) |
| 290 |
(tm:add-duration time1 duration time1)) |
| 291 |
|
| 292 |
(define (tm:subtract-duration time1 duration time3) |
| 293 |
(when (not (and (time? time1) (time? duration))) |
| 294 |
(errorf "subtract-duration: incompatible type types: ~a ~a" |
| 295 |
time1 duration)) |
| 296 |
(tm:check-time-type duration 'time-duration 'subtract-duration) |
| 297 |
(let ([sec-minus (- (time-second time1) (time-second duration))] |
| 298 |
[nsec-minus (- (time-nanosecond time1) (time-nanosecond duration))]) |
| 299 |
(let ([r (remainder nsec-minus tm:nano)] |
| 300 |
[q (quotient nsec-minus tm:nano)]) |
| 301 |
(if (negative? r) |
| 302 |
(begin |
| 303 |
(set-time-second! time3 (- sec-minus q 1)) |
| 304 |
(set-time-nanosecond! time3 (+ tm:nano r))) |
| 305 |
(begin |
| 306 |
(set-time-second! time3 (- sec-minus q)) |
| 307 |
(set-time-nanosecond! time3 r))) |
| 308 |
time3))) |
| 309 |
|
| 310 |
(define (subtract-duration time1 duration) |
| 311 |
(tm:subtract-duration time1 duration (make <time> :type (time-type time1)))) |
| 312 |
|
| 313 |
(define (subtract-duration! time1 duration) |
| 314 |
(tm:subtract-duration time1 duration time1)) |
| 315 |
|
| 316 |
;; -- Converters between types. |
| 317 |
|
| 318 |
(define (tm:time-tai->time-utc! time-in time-out caller) |
| 319 |
(tm:check-time-type time-in 'time-tai caller) |
| 320 |
(set-time-type! time-out time-utc) |
| 321 |
(set-time-nanosecond! time-out (time-nanosecond time-in)) |
| 322 |
(set-time-second! time-out (- (time-second time-in) |
| 323 |
(tm:leap-second-delta |
| 324 |
(time-second time-in)))) |
| 325 |
time-out) |
| 326 |
|
| 327 |
(define (time-tai->time-utc time-in) |
| 328 |
(tm:time-tai->time-utc! time-in (make <time>) 'time-tai->time-utc)) |
| 329 |
|
| 330 |
(define (time-tai->time-utc! time-in) |
| 331 |
(tm:time-tai->time-utc! time-in time-in 'time-tai->time-utc!)) |
| 332 |
|
| 333 |
|
| 334 |
(define (tm:time-utc->time-tai! time-in time-out caller) |
| 335 |
(tm:check-time-type time-in 'time-utc caller) |
| 336 |
(set-time-type! time-out time-tai) |
| 337 |
(set-time-nanosecond! time-out (time-nanosecond time-in)) |
| 338 |
(set-time-second! time-out (+ (time-second time-in) |
| 339 |
(tm:leap-second-delta |
| 340 |
(time-second time-in)))) |
| 341 |
time-out) |
| 342 |
|
| 343 |
(define (time-utc->time-tai time-in) |
| 344 |
(tm:time-utc->time-tai! time-in (make <time>) 'time-utc->time-tai)) |
| 345 |
|
| 346 |
(define (time-utc->time-tai! time-in) |
| 347 |
(tm:time-utc->time-tai! time-in time-in 'time-utc->time-tai!)) |
| 348 |
|
| 349 |
;; -- these depend on time-monotonic having the same definition as time-tai! |
| 350 |
(define (time-monotonic->time-utc time-in) |
| 351 |
(tm:check-time-type time-in 'time-monotonic 'time-monotonic->time-utc) |
| 352 |
(let1 ntime (copy-time time-in) |
| 353 |
(set-time-type! ntime time-tai) |
| 354 |
(tm:time-tai->time-utc! ntime ntime 'time-monotonic->time-utc))) |
| 355 |
|
| 356 |
(define (time-monotonic->time-utc! time-in) |
| 357 |
(tm:check-time-type time-in 'time-monotonic 'time-monotonic->time-utc!) |
| 358 |
(set-time-type! time-in time-tai) |
| 359 |
(tm:time-tai->time-utc! time-in time-in 'time-monotonic->time-utc)) |
| 360 |
|
| 361 |
(define (time-monotonic->time-tai time-in) |
| 362 |
(tm:check-time-type time-in 'time-monotonic 'time-monotonic->time-tai) |
| 363 |
(rlet1 ntime (copy-time time-in) |
| 364 |
(set-time-type! ntime time-tai))) |
| 365 |
|
| 366 |
(define (time-monotonic->time-tai! time-in) |
| 367 |
(tm:check-time-type time-in 'time-monotonic 'time-monotonic->time-tai!) |
| 368 |
(set-time-type! time-in time-tai) |
| 369 |
time-in) |
| 370 |
|
| 371 |
(define (time-utc->time-monotonic time-in) |
| 372 |
(tm:check-time-type time-in 'time-utc 'time-utc->time-monotonic) |
| 373 |
(rlet1 ntime (tm:time-utc->time-tai! time-in (make <time>) |
| 374 |
'time-utc->time-monotonic) |
| 375 |
(set-time-type! ntime time-monotonic))) |
| 376 |
|
| 377 |
(define (time-utc->time-monotonic! time-in) |
| 378 |
(tm:check-time-type time-in 'time-utc 'time-utc->time-monotonic!) |
| 379 |
(rlet1 ntime (tm:time-utc->time-tai! time-in time-in |
| 380 |
'time-utc->time-monotonic!) |
| 381 |
(set-time-type! ntime time-monotonic))) |
| 382 |
|
| 383 |
(define (time-tai->time-monotonic time-in) |
| 384 |
(tm:check-time-type time-in 'time-tai 'time-tai->time-monotonic) |
| 385 |
(rlet1 ntime (copy-time time-in) |
| 386 |
(set-time-type! ntime time-monotonic))) |
| 387 |
|
| 388 |
(define (time-tai->time-monotonic! time-in) |
| 389 |
(tm:check-time-type time-in 'time-tai 'time-tai->time-monotonic!) |
| 390 |
(set-time-type! time-in time-monotonic) |
| 391 |
time-in) |
| 392 |
|
| 393 |
;; -- Date Structures |
| 394 |
|
| 395 |
(define-class <date> () |
| 396 |
((nanosecond :init-keyword :nanosecond :getter date-nanosecond) |
| 397 |
(second :init-keyword :second :getter date-second) |
| 398 |
(minute :init-keyword :minute :getter date-minute) |
| 399 |
(hour :init-keyword :hour :getter date-hour) |
| 400 |
(day :init-keyword :day :getter date-day) |
| 401 |
(month :init-keyword :month :getter date-month) |
| 402 |
(year :init-keyword :year :getter date-year) |
| 403 |
(zone-offset :init-keyword :zone-offset :getter date-zone-offset))) |
| 404 |
|
| 405 |
(define-method object-equal? ((a <date>) (b <date>)) |
| 406 |
(every (^[slot] (= (~ a slot) (~ b slot))) |
| 407 |
'(nanosecond second minute hour day month year zone-offset))) |
| 408 |
|
| 409 |
(define-method object-compare ((a <date>) (b <date>)) |
| 410 |
(compare (date->time-monotonic a) (date->time-monotonic b))) |
| 411 |
|
| 412 |
(define (date? obj) (is-a? obj <date>)) |
| 413 |
|
| 414 |
(define (make-date nanosecond second minute hour day month year zone-offset) |
| 415 |
(unless (<= 0 nanosecond 999999999) |
| 416 |
(error "make-date: nanosecond out of range:" nanosecond)) |
| 417 |
(unless (<= 0 second 60) |
| 418 |
(error "make-date: second out of range:" second)) |
| 419 |
(unless (<= 0 minute 59) |
| 420 |
(error "make-date: minute out of range:" minute)) |
| 421 |
(unless (<= 0 hour 23) |
| 422 |
(error "make-date: hour out of range:" hour)) |
| 423 |
(unless (<= 0 day 31) |
| 424 |
(error "make-date: day out of range:" day)) |
| 425 |
(unless (<= 1 month 12) |
| 426 |
(error "make-date: month out of range:" month)) |
| 427 |
(unless (exact-integer? year) |
| 428 |
(error "make-date: bad object for year:" year)) |
| 429 |
(make <date> |
| 430 |
:nanosecond nanosecond :second second :minute minute :hour hour |
| 431 |
:day day :month month :year year :zone-offset zone-offset)) |
| 432 |
|
| 433 |
(define-method write-object ((obj <date>) port) |
| 434 |
(format port "#<date ~d/~2,'0d/~2,'0d ~2,'0d:~2,'0d:~2,'0d.~9,'0d (~a)>" |
| 435 |
(date-year obj) (date-month obj) (date-day obj) |
| 436 |
(date-hour obj) (date-minute obj) (date-second obj) |
| 437 |
(date-nanosecond obj) (date-zone-offset obj))) |
| 438 |
|
| 439 |
;; gives the julian day which starts at noon. |
| 440 |
(define (tm:encode-julian-day-number day month year) |
| 441 |
(let* ([a (quotient (- 14 month) 12)] |
| 442 |
[y (- (+ year 4800) a (if (negative? year) -1 0))] |
| 443 |
[m (- (+ month (* 12 a)) 3)]) |
| 444 |
(+ day |
| 445 |
(quotient (+ (* 153 m) 2) 5) |
| 446 |
(* 365 y) |
| 447 |
(quotient y 4) |
| 448 |
(- (quotient y 100)) |
| 449 |
(quotient y 400) |
| 450 |
-32045))) |
| 451 |
|
| 452 |
;; Gives the seconds/date/month/year |
| 453 |
(define (tm:decode-julian-day-number jdn) |
| 454 |
(let* ([days (truncate->exact jdn)] |
| 455 |
[a (+ days 32044)] |
| 456 |
[b (quotient (+ (* 4 a) 3) 146097)] |
| 457 |
[c (- a (quotient (* 146097 b) 4))] |
| 458 |
[d (quotient (+ (* 4 c) 3) 1461)] |
| 459 |
[e (- c (quotient (* 1461 d) 4))] |
| 460 |
[m (quotient (+ (* 5 e) 2) 153)] |
| 461 |
[y (+ (* 100 b) d -4800 (quotient m 10))]) |
| 462 |
(values ; seconds date month year |
| 463 |
(round->exact (* (- jdn days) tm:sid)) |
| 464 |
(+ e (- (quotient (+ (* 153 m) 2) 5)) 1) |
| 465 |
(+ m 3 (* -12 (quotient m 10))) |
| 466 |
(if (>= 0 y) (- y 1) y)) |
| 467 |
)) |
| 468 |
|
| 469 |
;; Offset of local timezone in seconds. |
| 470 |
;; System-dependent. |
| 471 |
|
| 472 |
(define (tm:local-tz-offset) |
| 473 |
(define (tm->seconds-in-year tm) |
| 474 |
(+ (cond [(assv (+ (slot-ref tm 'mon) 1) tm:month-assoc) |
| 475 |
=> (^p (* (+ (cdr p) |
| 476 |
(slot-ref tm 'mday) |
| 477 |
(if (and (> (car p) 2) |
| 478 |
(tm:leap-year? (slot-ref tm 'year))) |
| 479 |
1 0)) |
| 480 |
3600 24))] |
| 481 |
[else (error "something wrong")]) |
| 482 |
(* (slot-ref tm 'hour) 3600) |
| 483 |
(* (slot-ref tm 'min) 60))) |
| 484 |
(let* ([now (sys-time)] |
| 485 |
[local (sys-localtime now)] |
| 486 |
[local-sec (tm->seconds-in-year local)] |
| 487 |
[local-yr (slot-ref local 'year)] |
| 488 |
[gm (sys-gmtime now)] |
| 489 |
[gm-sec (tm->seconds-in-year gm)] |
| 490 |
[gm-yr (slot-ref gm 'year)]) |
| 491 |
(cond [(= local-yr gm-yr) |
| 492 |
(- local-sec gm-sec)] |
| 493 |
;; The following two cases are very rare, when this function is |
| 494 |
;; called very close to the year boundary. |
| 495 |
[(< local-yr gm-yr) |
| 496 |
(- (- local-sec |
| 497 |
(if (tm:leap-year? (slot-ref local 'year)) 31622400 31536000)) |
| 498 |
gm-sec)] |
| 499 |
[else |
| 500 |
(- local-sec |
| 501 |
(- gm-sec |
| 502 |
(if (tm:leap-year? (slot-ref gm 'year)) 31622400 31536000)))] |
| 503 |
))) |
| 504 |
|
| 505 |
;; special thing -- ignores nanos |
| 506 |
(define (tm:time->julian-day-number seconds tz-offset) |
| 507 |
(/ (+ (* tm:tai-epoch-in-jd tm:sid) |
| 508 |
seconds tz-offset tm:sihd) |
| 509 |
tm:sid)) |
| 510 |
|
| 511 |
(define (tm:leap-second? second) |
| 512 |
(and (assoc second tm:leap-second-table) #t)) |
| 513 |
|
| 514 |
(define (time-utc->date time :optional (offset (tm:local-tz-offset))) |
| 515 |
(tm:check-time-type time 'time-utc 'time-utc->date) |
| 516 |
(let1 is-leap-second (tm:leap-second? (+ offset (time-second time))) |
| 517 |
(receive (secs date month year) |
| 518 |
(if is-leap-second |
| 519 |
(tm:decode-julian-day-number (tm:time->julian-day-number (- (time-second time) 1) offset)) |
| 520 |
(tm:decode-julian-day-number (tm:time->julian-day-number (time-second time) offset))) |
| 521 |
(let* ([hours (quotient secs (* 60 60))] |
| 522 |
[rem (remainder secs (* 60 60))] |
| 523 |
[minutes (quotient rem 60)] |
| 524 |
[seconds (remainder rem 60)]) |
| 525 |
(make-date (time-nanosecond time) |
| 526 |
(if is-leap-second (+ seconds 1) seconds) |
| 527 |
minutes |
| 528 |
hours |
| 529 |
date |
| 530 |
month |
| 531 |
year |
| 532 |
offset))))) |
| 533 |
|
| 534 |
(define (time-tai->date time :optional (offset (tm:local-tz-offset))) |
| 535 |
(tm:check-time-type time 'time-tai 'time-tai->date) |
| 536 |
(let* ([seconds (- (time-second time) (tm:leap-second-delta (time-second time)))] |
| 537 |
[is-leap-second (tm:leap-second? (+ offset seconds))]) |
| 538 |
(receive (secs date month year) |
| 539 |
(if is-leap-second |
| 540 |
(tm:decode-julian-day-number (tm:time->julian-day-number (- seconds 1) offset)) |
| 541 |
(tm:decode-julian-day-number (tm:time->julian-day-number seconds offset))) |
| 542 |
;; adjust for leap seconds if necessary ... |
| 543 |
(let* ([hours (quotient secs (* 60 60))] |
| 544 |
[rem (remainder secs (* 60 60))] |
| 545 |
[minutes (quotient rem 60)] |
| 546 |
[seconds (remainder rem 60)]) |
| 547 |
(make-date (time-nanosecond time) |
| 548 |
(if is-leap-second (+ seconds 1) seconds) |
| 549 |
minutes |
| 550 |
hours |
| 551 |
date |
| 552 |
month |
| 553 |
year |
| 554 |
offset))))) |
| 555 |
|
| 556 |
;; this is the same as time-tai->date. |
| 557 |
(define (time-monotonic->date time :optional (offset (tm:local-tz-offset))) |
| 558 |
(tm:check-time-type time 'time-monotonic 'time-monotonic->date) |
| 559 |
(let* ([seconds (- (time-second time) (tm:leap-second-delta (time-second time)))] |
| 560 |
[is-leap-second (tm:leap-second? (+ offset seconds))]) |
| 561 |
(receive (secs date month year) |
| 562 |
(if is-leap-second |
| 563 |
(tm:decode-julian-day-number (tm:time->julian-day-number (- seconds 1) offset)) |
| 564 |
(tm:decode-julian-day-number (tm:time->julian-day-number seconds offset))) |
| 565 |
;; adjust for leap seconds if necessary ... |
| 566 |
(let* ([hours (quotient secs (* 60 60))] |
| 567 |
[rem (remainder secs (* 60 60))] |
| 568 |
[minutes (quotient rem 60)] |
| 569 |
[seconds (remainder rem 60)]) |
| 570 |
(make-date (time-nanosecond time) |
| 571 |
(if is-leap-second (+ seconds 1) seconds) |
| 572 |
minutes |
| 573 |
hours |
| 574 |
date |
| 575 |
month |
| 576 |
year |
| 577 |
offset))))) |
| 578 |
|
| 579 |
(define (date->time-utc date) |
| 580 |
(let ([nanosecond (date-nanosecond date)] |
| 581 |
[second (date-second date)] |
| 582 |
[minute (date-minute date)] |
| 583 |
[hour (date-hour date)] |
| 584 |
[day (date-day date)] |
| 585 |
[month (date-month date)] |
| 586 |
[year (date-year date)] |
| 587 |
[offset (date-zone-offset date)]) |
| 588 |
(let1 jdays-1/2 (- (tm:encode-julian-day-number day month year) |
| 589 |
(+ tm:tai-epoch-in-jd 1/2)) |
| 590 |
(make-time time-utc |
| 591 |
nanosecond |
| 592 |
(+ (* 24 60 60 jdays-1/2) |
| 593 |
(* 60 60 hour) |
| 594 |
(* 60 minute) |
| 595 |
second |
| 596 |
(- offset)))))) |
| 597 |
|
| 598 |
(define (date->time-tai date) |
| 599 |
(time-utc->time-tai! (date->time-utc date))) |
| 600 |
|
| 601 |
(define (date->time-monotonic date) |
| 602 |
(time-utc->time-monotonic! (date->time-utc date))) |
| 603 |
|
| 604 |
(define (tm:leap-year? year) |
| 605 |
(or (= (modulo year 400) 0) |
| 606 |
(and (= (modulo year 4) 0) (not (= (modulo year 100) 0))))) |
| 607 |
|
| 608 |
(define (leap-year? date) |
| 609 |
(tm:leap-year? (date-year date))) |
| 610 |
|
| 611 |
(define tm:month-assoc '((1 . 0) (2 . 31) (3 . 59) (4 . 90) (5 . 120) |
| 612 |
(6 . 151) (7 . 181) (8 . 212) (9 . 243) |
| 613 |
(10 . 273) (11 . 304) (12 . 334))) |
| 614 |
|
| 615 |
(define (tm:year-day day month year) |
| 616 |
(let1 days-pr (assoc month tm:month-assoc) |
| 617 |
(when (not days-pr) (errorf "date-year-day: invalid month: ~a" month)) |
| 618 |
(if (and (tm:leap-year? year) (> month 2)) |
| 619 |
(+ day (cdr days-pr) 1) |
| 620 |
(+ day (cdr days-pr))))) |
| 621 |
|
| 622 |
(define (date-year-day date) |
| 623 |
(tm:year-day (date-day date) (date-month date) (date-year date))) |
| 624 |
|
| 625 |
;; from calendar faq |
| 626 |
(define (tm:week-day day month year) |
| 627 |
(let* ([a (quotient (- 14 month) 12)] |
| 628 |
[y (- year a)] |
| 629 |
[m (+ month (* 12 a) -2)]) |
| 630 |
(modulo (+ day y (quotient y 4) (- (quotient y 100)) |
| 631 |
(quotient y 400) (quotient (* 31 m) 12)) |
| 632 |
7))) |
| 633 |
|
| 634 |
(define (date-week-day date) |
| 635 |
(tm:week-day (date-day date) (date-month date) (date-year date))) |
| 636 |
|
| 637 |
(define (tm:days-before-first-week date day-of-week-starting-week) |
| 638 |
(let* ([first-day (make-date 0 0 0 0 |
| 639 |
1 |
| 640 |
1 |
| 641 |
(date-year date) |
| 642 |
#f)] |
| 643 |
[fdweek-day (date-week-day first-day)]) |
| 644 |
(modulo (- day-of-week-starting-week fdweek-day) |
| 645 |
7))) |
| 646 |
|
| 647 |
(define (date-week-number date day-of-week-starting-week) |
| 648 |
(quotient (- (date-year-day date) |
| 649 |
(tm:days-before-first-week date day-of-week-starting-week)) |
| 650 |
7)) |
| 651 |
|
| 652 |
(define (tm:date-week-number-iso date) |
| 653 |
;; The week with the year's first Thursday is week 01. |
| 654 |
(let* ([first-day-of-the-week (tm:week-day 1 1 (date-year date))] |
| 655 |
[offset (if (> first-day-of-the-week 4) 0 1)] |
| 656 |
;; -2: decrement one day to compensate 1-origin of date-year-day, |
| 657 |
;; and decrement one more day for Sunday belongs to the previous week. |
| 658 |
[w (+ (floor-quotient (+ (date-year-day date) first-day-of-the-week -2) |
| 659 |
7) |
| 660 |
offset)]) |
| 661 |
(cond [(zero? w) |
| 662 |
;; date belongs to the last week of the previous year |
| 663 |
(tm:date-week-number-iso (make-date 0 0 0 0 31 12 |
| 664 |
(- (date-year date) 1) #f))] |
| 665 |
[(and (= w 53) |
| 666 |
(<= (tm:week-day 1 1 (+ (date-year date) 1)) 4)) |
| 667 |
;; date belongs to the first week of the next year |
| 668 |
1] |
| 669 |
[else w]))) |
| 670 |
|
| 671 |
(define (current-date :optional (off (tm:local-tz-offset))) |
| 672 |
(time-utc->date (current-time time-utc) off)) |
| 673 |
|
| 674 |
;; given a 'two digit' number, find the year within 50 years +/- |
| 675 |
(define (tm:natural-year n) |
| 676 |
(let* ([current-year (date-year (current-date))] |
| 677 |
[current-century (* (quotient current-year 100) 100)]) |
| 678 |
(cond [(>= n 100) n] |
| 679 |
[(< n 0) n] |
| 680 |
[(<= (- (+ current-century n) current-year) 50) |
| 681 |
(+ current-century n)] |
| 682 |
[else (+ (- current-century 100) n)]))) |
| 683 |
|
| 684 |
(define (date->julian-day date) |
| 685 |
;; NB: we scale everything execpt nanoseconds first, so that we can |
| 686 |
;; avoid having intermediate results in ratnum. |
| 687 |
(/ (+ (* tm:sid tm:nano/i |
| 688 |
(tm:encode-julian-day-number (date-day date) |
| 689 |
(date-month date) |
| 690 |
(date-year date))) |
| 691 |
(+ (* tm:nano/i |
| 692 |
(+ (* 60 60 (date-hour date)) |
| 693 |
(* (date-minute date) 60) |
| 694 |
(date-second date) |
| 695 |
(- (date-zone-offset date)) |
| 696 |
(- tm:sihd))) |
| 697 |
(date-nanosecond date))) |
| 698 |
(* tm:sid tm:nano/i))) |
| 699 |
|
| 700 |
(define (date->modified-julian-day date) |
| 701 |
(- (date->julian-day date) 4800001/2)) |
| 702 |
|
| 703 |
(define (time-utc->julian-day time) |
| 704 |
(tm:check-time-type time 'time-utc 'time-utc->julian-day) |
| 705 |
;; NB: we scale first to avoid intermediate ratnum calculation. |
| 706 |
(/ (+ (time-nanosecond time) |
| 707 |
(* (time-second time) tm:nano/i) |
| 708 |
(* tm:tai-epoch-in-jd tm:sid tm:nano/i)) |
| 709 |
(* tm:sid tm:nano/i))) |
| 710 |
|
| 711 |
(define (time-utc->modified-julian-day time) |
| 712 |
(- (time-utc->julian-day time) 4800001/2)) |
| 713 |
|
| 714 |
(define (time-tai->julian-day time) |
| 715 |
(tm:check-time-type time 'time-tai 'time-tai->julian-day) |
| 716 |
;; NB: we scale first to avoid intermediate ratnum calculation. |
| 717 |
(/ (+ (time-nanosecond time) |
| 718 |
(* (- (time-second time) (tm:leap-second-delta (time-second time))) |
| 719 |
tm:nano/i) |
| 720 |
(* tm:tai-epoch-in-jd tm:sid tm:nano/i)) |
| 721 |
(* tm:sid tm:nano/i))) |
| 722 |
|
| 723 |
(define (time-tai->modified-julian-day time) |
| 724 |
(- (time-tai->julian-day time) 4800001/2)) |
| 725 |
|
| 726 |
;; this is the same as time-tai->julian-day |
| 727 |
(define (time-monotonic->julian-day time) |
| 728 |
(tm:check-time-type time 'time-monotonic 'time-monotonic->julian-day) |
| 729 |
;; NB: we scale first to avoid intermediate ratnum calculation. |
| 730 |
(/ (+ (time-nanosecond time) |
| 731 |
(* (- (time-second time) (tm:leap-second-delta (time-second time))) |
| 732 |
tm:nano/i) |
| 733 |
(* tm:tai-epoch-in-jd tm:sid tm:nano/i)) |
| 734 |
(* tm:sid tm:nano/i))) |
| 735 |
|
| 736 |
(define (time-monotonic->modified-julian-day time) |
| 737 |
(- (time-monotonic->julian-day time) 4800001/2)) |
| 738 |
|
| 739 |
(define (julian-day->time-utc jdn) |
| 740 |
(let1 nanosecs (- (* tm:nano/i tm:sid jdn) |
| 741 |
(* tm:nano/i tm:sid tm:tai-epoch-in-jd)) |
| 742 |
(receive (q r) (quotient&remainder nanosecs tm:nano) |
| 743 |
(make-time time-utc r q)))) |
| 744 |
|
| 745 |
(define (julian-day->time-tai jdn) |
| 746 |
(time-utc->time-tai! (julian-day->time-utc jdn))) |
| 747 |
|
| 748 |
(define (julian-day->time-monotonic jdn) |
| 749 |
(time-utc->time-monotonic! (julian-day->time-utc jdn))) |
| 750 |
|
| 751 |
(define (julian-day->date jdn :optional (offset (tm:local-tz-offset))) |
| 752 |
(time-utc->date (julian-day->time-utc jdn) offset)) |
| 753 |
|
| 754 |
(define (modified-julian-day->date jdn :optional (offset (tm:local-tz-offset))) |
| 755 |
(julian-day->date (+ jdn 4800001/2) offset)) |
| 756 |
|
| 757 |
(define (modified-julian-day->time-utc jdn) |
| 758 |
(julian-day->time-utc (+ jdn 4800001/2))) |
| 759 |
|
| 760 |
(define (modified-julian-day->time-tai jdn) |
| 761 |
(julian-day->time-tai (+ jdn 4800001/2))) |
| 762 |
|
| 763 |
(define (modified-julian-day->time-monotonic jdn) |
| 764 |
(julian-day->time-monotonic (+ jdn 4800001/2))) |
| 765 |
|
| 766 |
(define (current-julian-day) |
| 767 |
(time-utc->julian-day (current-time time-utc))) |
| 768 |
|
| 769 |
(define (current-modified-julian-day) |
| 770 |
(time-utc->modified-julian-day (current-time time-utc))) |
| 771 |
|
| 772 |
;; formatting stuff |
| 773 |
|
| 774 |
(define tm:locale-number-separator ".") |
| 775 |
|
| 776 |
(define tm:locale-abbr-weekday-vector |
| 777 |
'#("Sun" "Mon" "Tue" "Wed" "Thu" "Fri" "Sat")) |
| 778 |
(define tm:locale-long-weekday-vector |
| 779 |
'#("Sunday" "Monday" "Tuesday" "Wednesday" "Thursday" "Friday" "Saturday")) |
| 780 |
;; note empty string in 0th place. |
| 781 |
(define tm:locale-abbr-month-vector |
| 782 |
'#("" "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" |
| 783 |
"Aug" "Sep" "Oct" "Nov" "Dec")) |
| 784 |
(define tm:locale-long-month-vector |
| 785 |
'#("" "January" "February" "March" "April" "May" |
| 786 |
"June" "July" "August" "September" "October" "November" "December")) |
| 787 |
|
| 788 |
(define tm:locale-pm "PM") |
| 789 |
(define tm:locale-am "AM") |
| 790 |
|
| 791 |
;; See date->string |
| 792 |
(define tm:locale-date-time-format "~a ~b ~d ~H:~M:~S~z ~Y") |
| 793 |
(define tm:locale-short-date-format "~m/~d/~y") |
| 794 |
(define tm:locale-time-format "~H:~M:~S") |
| 795 |
(define tm:iso-8601-date-time-format "~Y-~m-~dT~H:~M:~S~z") |
| 796 |
|
| 797 |
;; returns a string rep. of number N, of minimum LENGTH, |
| 798 |
;; padded with character PAD-WITH. If PAD-WITH is #f, |
| 799 |
;; no padding is done, and it's as if number->string was used. |
| 800 |
;; if string is longer than LENGTH, it's as if number->string was used. |
| 801 |
|
| 802 |
(define (tm:padding n pad-with length) |
| 803 |
(if pad-with |
| 804 |
(format #f "~v,vd" length pad-with n) |
| 805 |
(number->string n))) |
| 806 |
|
| 807 |
(define (tm:last-n-digits i n) |
| 808 |
(abs (remainder i (expt 10 n)))) |
| 809 |
|
| 810 |
(define (tm:locale-abbr-weekday n) |
| 811 |
(vector-ref tm:locale-abbr-weekday-vector n)) |
| 812 |
|
| 813 |
(define (tm:locale-long-weekday n) |
| 814 |
(vector-ref tm:locale-long-weekday-vector n)) |
| 815 |
|
| 816 |
(define (tm:locale-abbr-month n) |
| 817 |
(vector-ref tm:locale-abbr-month-vector n)) |
| 818 |
|
| 819 |
(define (tm:locale-long-month n) |
| 820 |
(vector-ref tm:locale-long-month-vector n)) |
| 821 |
|
| 822 |
(define (tm:locale-abbr-weekday->index string) |
| 823 |
(find-index (cut string=? string <>) tm:locale-abbr-weekday-vector)) |
| 824 |
|
| 825 |
(define (tm:locale-long-weekday->index string) |
| 826 |
(find-index (cut string=? string <>) tm:locale-long-weekday-vector)) |
| 827 |
|
| 828 |
(define (tm:locale-abbr-month->index string) |
| 829 |
(find-index (cut string=? string <>) tm:locale-abbr-month-vector)) |
| 830 |
|
| 831 |
(define (tm:locale-long-month->index string) |
| 832 |
(find-index (cut string=? string <>) tm:locale-long-month-vector)) |
| 833 |
|
| 834 |
;; do nothing. |
| 835 |
;; Your implementation might want to do something... |
| 836 |
;; |
| 837 |
(define (tm:locale-print-time-zone date) |
| 838 |
(values)) |
| 839 |
|
| 840 |
;; Again, locale specific. |
| 841 |
(define (tm:locale-am/pm hr) |
| 842 |
(if (> hr 11) tm:locale-pm tm:locale-am)) |
| 843 |
|
| 844 |
(define (tm:tz-printer offset) |
| 845 |
(cond [(= offset 0) (display "Z")] |
| 846 |
[(negative? offset) (display "-")] |
| 847 |
[else (display "+")]) |
| 848 |
(unless (zero? offset) |
| 849 |
(let ([hours (abs (quotient offset (* 60 60)))] |
| 850 |
[minutes (abs (quotient (remainder offset (* 60 60)) 60))]) |
| 851 |
(format #t "~2,'0d~2,'0d" hours minutes)))) |
| 852 |
|
| 853 |
;; A table of output formatting directives. |
| 854 |
;; the first time is the format char. |
| 855 |
;; the second is a procedure that takes the date, a padding character |
| 856 |
;; (which might be #f), and the output port. |
| 857 |
;; |
| 858 |
(define tm:directives |
| 859 |
`((#\~ . ,(^[date pad-with] (display #\~))) |
| 860 |
(#\a . ,(^[date pad-with] |
| 861 |
(display (tm:locale-abbr-weekday (date-week-day date))))) |
| 862 |
(#\A . ,(^[date pad-with] |
| 863 |
(display (tm:locale-long-weekday (date-week-day date))))) |
| 864 |
(#\b . ,(^[date pad-with] |
| 865 |
(display (tm:locale-abbr-month (date-month date))))) |
| 866 |
(#\B . ,(^[date pad-with] |
| 867 |
(display (tm:locale-long-month (date-month date))))) |
| 868 |
(#\c . ,(^[date pad-with] |
| 869 |
(display (date->string date tm:locale-date-time-format)))) |
| 870 |
(#\d . ,(^[date pad-with] |
| 871 |
(format #t "~2,'0d" (date-day date)))) |
| 872 |
(#\D . ,(^[date pad-with] |
| 873 |
(display (date->string date "~m/~d/~y")))) |
| 874 |
(#\e . ,(^[date pad-with] |
| 875 |
(format #t "~2,' d" (date-day date)))) |
| 876 |
(#\f . ,(^[date pad-with] |
| 877 |
(display (tm:padding (date-second date) pad-with 2)) |
| 878 |
(display tm:locale-number-separator) |
| 879 |
(let1 nanostr (number->string (/. (date-nanosecond date) tm:nano/i)) |
| 880 |
(cond [(string-index nanostr #\.) |
| 881 |
=> (^i (display (string-drop nanostr (+ i 1))))])))) |
| 882 |
(#\h . ,(^[date pad-with] |
| 883 |
(display (date->string date "~b")))) |
| 884 |
(#\H . ,(^[date pad-with] |
| 885 |
(display (tm:padding (date-hour date) pad-with 2)))) |
| 886 |
(#\I . ,(^[date pad-with] |
| 887 |
(let1 hr (date-hour date) |
| 888 |
(if (> hr 12) |
| 889 |
(display (tm:padding (- hr 12) pad-with 2)) |
| 890 |
(display (tm:padding hr pad-with 2)))))) |
| 891 |
(#\j . ,(^[date pad-with] |
| 892 |
(display (tm:padding (date-year-day date) pad-with 3)))) |
| 893 |
(#\k . ,(^[date pad-with] |
| 894 |
(format #t "~2,' d" (date-hour date)))) |
| 895 |
(#\l . ,(^[date pad-with] |
| 896 |
(let1 hr (if (> (date-hour date) 12) |
| 897 |
(- (date-hour date) 12) |
| 898 |
(date-hour date)) |
| 899 |
(format #t "~2,' d" hr)))) |
| 900 |
(#\m . ,(^[date pad-with] |
| 901 |
(display (tm:padding (date-month date) pad-with 2)))) |
| 902 |
(#\M . ,(^[date pad-with] |
| 903 |
(display (tm:padding (date-minute date) pad-with 2)))) |
| 904 |
(#\n . ,(^[date pad-with] (newline))) |
| 905 |
(#\N . ,(^[date pad-with] |
| 906 |
(display (tm:padding (date-nanosecond date) pad-with 9)))) |
| 907 |
(#\p . ,(^[date pad-with] |
| 908 |
(display (tm:locale-am/pm (date-hour date))))) |
| 909 |
(#\r . ,(^[date pad-with] |
| 910 |
(display (date->string date "~I:~M:~S ~p")))) |
| 911 |
(#\s . ,(^[date pad-with] |
| 912 |
(display (time-second (date->time-utc date))))) |
| 913 |
(#\S . ,(^[date pad-with] |
| 914 |
(display (tm:padding (date-second date) pad-with 2)))) |
| 915 |
(#\t . ,(^[date pad-with] |
| 916 |
(display #\tab))) |
| 917 |
(#\T . ,(^[date pad-with] |
| 918 |
(display (date->string date "~H:~M:~S")))) |
| 919 |
(#\U . ,(^[date pad-with] |
| 920 |
(format #t "~2,'0d" |
| 921 |
(if (> (tm:days-before-first-week date 0) 0) |
| 922 |
(+ (date-week-number date 0) 1) |
| 923 |
(date-week-number date 0))))) |
| 924 |
(#\V . ,(^[date pad-with] |
| 925 |
(format #t "~2,'0d" (tm:date-week-number-iso date)))) |
| 926 |
(#\w . ,(^[date pad-with] |
| 927 |
(display (date-week-day date)))) |
| 928 |
(#\x . ,(^[date pad-with] |
| 929 |
(display (date->string date tm:locale-short-date-format)))) |
| 930 |
(#\X . ,(^[date pad-with] |
| 931 |
(display (date->string date tm:locale-time-format)))) |
| 932 |
(#\W . ,(^[date pad-with] |
| 933 |
(format #t "~2,'0d" |
| 934 |
(if (> (tm:days-before-first-week date 1) 0) |
| 935 |
(+ (date-week-number date 1) 1) |
| 936 |
(date-week-number date 1))))) |
| 937 |
(#\y . ,(^[date pad-with] |
| 938 |
(display (tm:padding (tm:last-n-digits (date-year date) 2) pad-with 2)))) |
| 939 |
(#\Y . ,(^[date pad-with] |
| 940 |
(display (date-year date)))) |
| 941 |
(#\z . ,(^[date pad-with] |
| 942 |
(tm:tz-printer (date-zone-offset date)))) |
| 943 |
(#\Z . ,(^[date pad-with] |
| 944 |
(tm:locale-print-time-zone date))) |
| 945 |
(#\1 . ,(^[date pad-with] |
| 946 |
(display (date->string date "~Y-~m-~d")))) |
| 947 |
(#\2 . ,(^[date pad-with] |
| 948 |
(display (date->string date "~H:~M:~S~z")))) |
| 949 |
(#\3 . ,(^[date pad-with] |
| 950 |
(display (date->string date "~H:~M:~S")))) |
| 951 |
(#\4 . ,(^[date pad-with] |
| 952 |
(display (date->string date "~Y-~m-~dT~H:~M:~S~z")))) |
| 953 |
(#\5 . ,(^[date pad-with] |
| 954 |
(display (date->string date "~Y-~m-~dT~H:~M:~S")))) |
| 955 |
)) |
| 956 |
|
| 957 |
(define (date->string date . maybe-fmtstr) |
| 958 |
(let1 format-string (get-optional maybe-fmtstr "~c") |
| 959 |
(with-input-from-string format-string |
| 960 |
(^[] |
| 961 |
(define (bad i) |
| 962 |
(errorf "date->string: bad date format string: \"~a >>>~a<<< ~a\"" |
| 963 |
(string-take format-string i) |
| 964 |
(substring format-string i (+ i 1)) |
| 965 |
(string-drop format-string (+ i 1)))) |
| 966 |
|
| 967 |
(define (call-formatter ch pad ind) |
| 968 |
(if-let1 fn (assv ch tm:directives) |
| 969 |
(begin ((cdr fn) date pad) (rec (read-char) (+ ind 1))) |
| 970 |
(bad ind))) |
| 971 |
|
| 972 |
(define (rec ch ind) |
| 973 |
(cond |
| 974 |
[(eof-object? ch)] |
| 975 |
[(not (char=? ch #\~)) |
| 976 |
(write-char ch) (rec (read-char) (+ ind 1))] |
| 977 |
[else |
| 978 |
(let1 ch2 (read-char) |
| 979 |
(cond |
| 980 |
[(eof-object? ch2) (write-char ch)] |
| 981 |
;; Gauche extension: ~@x calls the directive 'x' with locale |
| 982 |
;; set to C, so the caller can guarantee the output. Currently |
| 983 |
;; the library only supports the default locale, so we can simply |
| 984 |
;; ignore '@'. In future we'll add locale-sensitive stuff. |
| 985 |
[(char=? ch2 #\@) |
| 986 |
(call-formatter (read-char) #f (+ ind 2))] |
| 987 |
[else (call-formatter ch2 #\0 (+ ind 1))]))])) |
| 988 |
|
| 989 |
;; body |
| 990 |
(with-output-to-string |
| 991 |
(cut rec (read-char) 0)))))) |
| 992 |
|
| 993 |
(define (tm:char->int ch) |
| 994 |
(or (digit->integer ch) |
| 995 |
(errorf "bad date template string: non integer character: ~a" ch))) |
| 996 |
|
| 997 |
;; read an integer upto n characters long on port; upto -> #f if any length |
| 998 |
(define (tm:integer-reader upto port) |
| 999 |
(define (accum-int port accum nchars) |
| 1000 |
(let1 ch (peek-char port) |
| 1001 |
(if (or (eof-object? ch) |
| 1002 |
(not (char-numeric? ch)) |
| 1003 |
(and upto (>= nchars upto ))) |
| 1004 |
accum |
| 1005 |
(accum-int port |
| 1006 |
(+ (* accum 10) (tm:char->int (read-char port))) |
| 1007 |
(+ nchars 1))))) |
| 1008 |
(accum-int port 0 0)) |
| 1009 |
|
| 1010 |
(define (tm:make-integer-reader upto) |
| 1011 |
(^[port] (tm:integer-reader upto port))) |
| 1012 |
|
| 1013 |
;; read *exactly* n characters and convert to integer; could be padded |
| 1014 |
(define (tm:integer-reader-exact n port) |
| 1015 |
(let1 padding-ok #t |
| 1016 |
(define (accum-int port accum nchars) |
| 1017 |
(let1 ch (peek-char port) |
| 1018 |
(cond |
| 1019 |
[(>= nchars n) accum] |
| 1020 |
[(eof-object? ch) |
| 1021 |
(error "string->date: premature ending of integer read")] |
| 1022 |
[(char-numeric? ch) |
| 1023 |
(set! padding-ok #f) |
| 1024 |
(accum-int port |
| 1025 |
(+ (* accum 10) (tm:char->int (read-char port))) |
| 1026 |
(+ nchars 1))] |
| 1027 |
[padding-ok |
| 1028 |
(read-char port) ; consume padding |
| 1029 |
(accum-int port accum (+ nchars 1))] |
| 1030 |
[else ; padding where it shouldn't be |
| 1031 |
(error "string->date: Non-numeric characters in integer read.")] |
| 1032 |
))) |
| 1033 |
(accum-int port 0 0))) |
| 1034 |
|
| 1035 |
(define (tm:make-integer-exact-reader n) |
| 1036 |
(^[port] (tm:integer-reader-exact n port))) |
| 1037 |
|
| 1038 |
(define (tm:zone-reader port) |
| 1039 |
(let ([offset 0] |
| 1040 |
[positive? #f]) |
| 1041 |
(let1 ch (read-char port) |
| 1042 |
(when (eof-object? ch) |
| 1043 |
(errorf "string->date: invalid time zone +/-: ~s" ch)) |
| 1044 |
(if (or (char=? ch #\Z) (char=? ch #\z)) |
| 1045 |
0 |
| 1046 |
(begin |
| 1047 |
(cond |
| 1048 |
[(char=? ch #\+) (set! positive? #t)] |
| 1049 |
[(char=? ch #\-) (set! positive? #f)] |
| 1050 |
[else (errorf "string->date: invalid time zone +/-: ~s" ch)]) |
| 1051 |
(let1 ch (read-char port) |
| 1052 |
(when (eof-object? ch) |
| 1053 |
(error "string->date: premature end of time zone number")) |
| 1054 |
(set! offset (* (tm:char->int ch) |
| 1055 |
10 60 60))) |
| 1056 |
(let1 ch (read-char port) |
| 1057 |
(when (eof-object? ch) |
| 1058 |
(error "string->date: premature end of time zone number")) |
| 1059 |
(set! offset (+ offset (* (tm:char->int ch) |
| 1060 |
60 60)))) |
| 1061 |
(let1 ch (read-char port) |
| 1062 |
(when (eof-object? ch) |
| 1063 |
(error "string->date: premature end of time zone number")) |
| 1064 |
(set! offset (+ offset (* (tm:char->int ch) |
| 1065 |
10 60)))) |
| 1066 |
(let1 ch (read-char port) |
| 1067 |
(when (eof-object? ch) |
| 1068 |
(error "string->date: premature end of time zone number")) |
| 1069 |
(set! offset (+ offset (* (tm:char->int ch) |
| 1070 |
60)))) |
| 1071 |
(if positive? offset (- offset))))))) |
| 1072 |
|
| 1073 |
;; looking at a char, read the char string, run thru indexer, return index |
| 1074 |
(define (tm:locale-reader port indexer) |
| 1075 |
(let1 string-port (open-output-string) |
| 1076 |
(define (read-char-string) |
| 1077 |
(let1 ch (peek-char port) |
| 1078 |
(if (char-alphabetic? ch) |
| 1079 |
(begin (write-char (read-char port) string-port) |
| 1080 |
(read-char-string)) |
| 1081 |
(get-output-string string-port)))) |
| 1082 |
(let* ([str (read-char-string)] |
| 1083 |
[index (indexer str)]) |
| 1084 |
(or index |
| 1085 |
(errorf "string->date: invalid string for ~s" indexer))))) |
| 1086 |
|
| 1087 |
(define (tm:make-locale-reader indexer) |
| 1088 |
(^[port] (tm:locale-reader port indexer))) |
| 1089 |
|
| 1090 |
(define (tm:make-char-id-reader char) |
| 1091 |
(^[port] (if (char=? char (read-char port)) |
| 1092 |
char |
| 1093 |
(error "string->date: invalid character match")))) |
| 1094 |
|
| 1095 |
;; A List of formatted read directives. |
| 1096 |
;; Each entry is a list. |
| 1097 |
;; 1. the character directive; |
| 1098 |
;; a procedure, which takes a character as input & returns |
| 1099 |
;; 2. #t as soon as a character on the input port is acceptable |
| 1100 |
;; for input, |
| 1101 |
;; 3. a port reader procedure that knows how to read the current port |
| 1102 |
;; for a value. Its one parameter is the port. |
| 1103 |
;; 4. a action procedure, that takes the value (from 3.) and some |
| 1104 |
;; object (here, always the date) and (probably) side-effects it. |
| 1105 |
;; In some cases (e.g., ~A) the action is to do nothing |
| 1106 |
|
| 1107 |
(define tm:read-directives |
| 1108 |
(let ([ireader4 (tm:make-integer-reader 4)] |
| 1109 |
[ireader2 (tm:make-integer-reader 2)] |
| 1110 |
[ireaderf (tm:make-integer-reader #f)] |
| 1111 |
[eireader2 (tm:make-integer-exact-reader 2)] |
| 1112 |
[eireader4 (tm:make-integer-exact-reader 4)] |
| 1113 |
[locale-reader-abbr-weekday (tm:make-locale-reader |
| 1114 |
tm:locale-abbr-weekday->index)] |
| 1115 |
[locale-reader-long-weekday (tm:make-locale-reader |
| 1116 |
tm:locale-long-weekday->index)] |
| 1117 |
[locale-reader-abbr-month (tm:make-locale-reader |
| 1118 |
tm:locale-abbr-month->index)] |
| 1119 |
[locale-reader-long-month (tm:make-locale-reader |
| 1120 |
tm:locale-long-month->index)] |
| 1121 |
[char-fail (^_ #t)] |
| 1122 |
[do-nothing (^[val object] (values))]) |
| 1123 |
|
| 1124 |
(list |
| 1125 |
(list #\~ char-fail (tm:make-char-id-reader #\~) do-nothing) |
| 1126 |
(list #\a char-alphabetic? locale-reader-abbr-weekday do-nothing) |
| 1127 |
(list #\A char-alphabetic? locale-reader-long-weekday do-nothing) |
| 1128 |
(list #\b char-alphabetic? locale-reader-abbr-month |
| 1129 |
(^[val object] (slot-set! object 'month val))) |
| 1130 |
(list #\B char-alphabetic? locale-reader-long-month |
| 1131 |
(^[val object] (slot-set! object 'month val))) |
| 1132 |
(list #\d char-numeric? ireader2 |
| 1133 |
(^[val object] (slot-set! object 'day val))) |
| 1134 |
(list #\e char-fail eireader2 |
| 1135 |
(^[val object] (slot-set! object 'day val))) |
| 1136 |
(list #\h char-alphabetic? locale-reader-abbr-month |
| 1137 |
(^[val object] (slot-set! object 'month val))) |
| 1138 |
(list #\H char-numeric? ireader2 |
| 1139 |
(^[val object] (slot-set! object 'hour val))) |
| 1140 |
(list #\k char-fail eireader2 |
| 1141 |
(^[val object] (slot-set! object 'hour val))) |
| 1142 |
(list #\m char-numeric? ireader2 |
| 1143 |
(^[val object] (slot-set! object 'month val))) |
| 1144 |
(list #\M char-numeric? ireader2 |
| 1145 |
(^[val object] (slot-set! object 'minute val))) |
| 1146 |
(list #\S char-numeric? ireader2 |
| 1147 |
(^[val object] (slot-set! object 'second val))) |
| 1148 |
(list #\y char-fail eireader2 |
| 1149 |
(^[val object] (slot-set! object 'year (tm:natural-year val)))) |
| 1150 |
(list #\Y char-numeric? ireader4 |
| 1151 |
(^[val object] (slot-set! object 'year val))) |
| 1152 |
(list #\z (^c (or (char=? c #\Z) |
| 1153 |
(char=? c #\z) |
| 1154 |
(char=? c #\+) |
| 1155 |
(char=? c #\-))) |
| 1156 |
tm:zone-reader (^[val object] (slot-set! object 'zone-offset val))) |
| 1157 |
))) |
| 1158 |
|
| 1159 |
(define (tm:string->date date index format-string str-len port template-string) |
| 1160 |
(define (bad) |
| 1161 |
(errorf "string->date: bad date format string: \"~a >>>~a<<< ~a\"" |
| 1162 |
(string-take template-string index) |
| 1163 |
(substring template-string index (+ index 1)) |
| 1164 |
(string-drop template-string (+ index 1)))) |
| 1165 |
(define (skip-until port skipper) |
| 1166 |
(let1 ch (peek-char port) |
| 1167 |
(when (eof-object? port) (bad)) |
| 1168 |
(unless (skipper ch) |
| 1169 |
(read-char port) (skip-until port skipper)))) |
| 1170 |
(if (>= index str-len) |
| 1171 |
(values) |
| 1172 |
(let1 current-char (string-ref format-string index) |
| 1173 |
(if (not (char=? current-char #\~)) |
| 1174 |
(let1 port-char (read-char port) |
| 1175 |
(when (or (eof-object? port-char) |
| 1176 |
(not (char=? current-char port-char))) |
| 1177 |
(bad)) |
| 1178 |
(tm:string->date date (+ index 1) format-string str-len port template-string)) |
| 1179 |
;; otherwise, it's an escape, we hope |
| 1180 |
(if (> (+ index 1) str-len) |
| 1181 |
(bad) |
| 1182 |
(let* ([format-char (string-ref format-string (+ index 1))] |
| 1183 |
[format-info (assoc format-char tm:read-directives)]) |
| 1184 |
(when (not format-info) (bad)) |
| 1185 |
(let ([skipper (cadr format-info)] |
| 1186 |
[reader (caddr format-info)] |
| 1187 |
[actor (cadddr format-info)]) |
| 1188 |
(skip-until port skipper) |
| 1189 |
(let1 val (reader port) |
| 1190 |
(when (eof-object? val) (bad)) |
| 1191 |
(actor val date)) |
| 1192 |
(tm:string->date date (+ index 2) format-string str-len port template-string)))))))) |
| 1193 |
|
| 1194 |
(define (string->date input-string template-string) |
| 1195 |
(define (tm:date-ok? date) |
| 1196 |
(and (date-nanosecond date) |
| 1197 |
(date-second date) |
| 1198 |
(date-minute date) |
| 1199 |
(date-hour date) |
| 1200 |
(date-day date) |
| 1201 |
(date-month date) |
| 1202 |
(date-year date) |
| 1203 |
(date-zone-offset date))) |
| 1204 |
(let1 newdate (make-date 0 0 0 0 0 1 0 (tm:local-tz-offset)) |
| 1205 |
(tm:string->date newdate |
| 1206 |
0 |
| 1207 |
template-string |
| 1208 |
(string-length template-string) |
| 1209 |
(open-input-string input-string) |
| 1210 |
template-string) |
| 1211 |
(if (tm:date-ok? newdate) |
| 1212 |
newdate |
| 1213 |
(errorf "string->date: incomplete date read: ~s for ~s" |
| 1214 |
newdate template-string)))) |
| 1215 |
|
| 1216 |
;; A table of leap seconds |
| 1217 |
;; See ftp://maia.usno.navy.mil/ser7/tai-utc.dat |
| 1218 |
;; and update as necessary. |
| 1219 |
;; this procedures reads the file in the abover |
| 1220 |
;; format and creates the leap second table |
| 1221 |
;; it also calls the almost standard, but not R5 procedures read-line |
| 1222 |
;; & open-input-string |
| 1223 |
;; ie (set! tm:leap-second-table (tm:read-tai-utc-data "tai-utc.dat")) |
| 1224 |
|
| 1225 |
(define (tm:read-tai-utc-data filename) |
| 1226 |
(define (convert-jd jd) |
| 1227 |
(* (- (inexact->exact jd) tm:tai-epoch-in-jd) tm:sid)) |
| 1228 |
(define (convert-sec sec) |
| 1229 |
(inexact->exact sec)) |
| 1230 |
(let ([port (open-input-file filename)] |
| 1231 |
[table '()]) |
| 1232 |
(let loop ([line (read-line port)]) |
| 1233 |
(unless (eof-object? line) |
| 1234 |
(let* ([data (read (open-input-string (string-append "(" line ")")))] |
| 1235 |
[year (car data)] |
| 1236 |
[jd (cadddr (cdr data))] |
| 1237 |
[secs (cadddr (cdddr data))]) |
| 1238 |
(when (>= year 1972) |
| 1239 |
(set! table (cons (cons (convert-jd jd) (convert-sec secs)) table))) |
| 1240 |
(loop (read-line port))))) |
| 1241 |
table)) |
| 1242 |
|
| 1243 |
(define (read-leap-second-table filename) |
| 1244 |
(set! tm:leap-second-table (tm:read-tai-utc-data filename)) |
| 1245 |
(values)) |
| 1246 |
|
| 1247 |
;;; |
| 1248 |
;;; Gauche extension |
| 1249 |
;;; |
| 1250 |
|
| 1251 |
;; Some user friendly rendering with 'describe'. |
| 1252 |
|
| 1253 |
(define (describe-time t) |
| 1254 |
(define (as-date ->date) |
| 1255 |
(let ([local (->date t)] |
| 1256 |
[gmt (->date t 0)]) |
| 1257 |
(if (zero? (date-zone-offset local)) |
| 1258 |
(print (date->string gmt "in date: ~4")) |
| 1259 |
(format #t "in date: ~a ~a\n" |
| 1260 |
(date->string local "~4") |
| 1261 |
(date->string gmt "~4"))))) |
| 1262 |
(define (ns->string t) |
| 1263 |
(let1 ns (time-nanosecond t) |
| 1264 |
(cond [(zero? ns) ""] |
| 1265 |
[(zero? (modulo ns #e1e6)) (format "~3,'0d" (quotient ns #e1e6))] |
| 1266 |
[(zero? (modulo ns #e1e3)) (format "~6,'0d" (quotient ns #e1e3))] |
| 1267 |
[else (format "~9,'0d" ns)]))) |
| 1268 |
(define (as-time t) |
| 1269 |
(let* ([secs (modulo (time-second t) 60)] |
| 1270 |
[z (quotient (time-second t) 60)] |
| 1271 |
[mins (modulo z 60)] |
| 1272 |
[z (quotient z 60)] |
| 1273 |
[hours (modulo z 24)] |
| 1274 |
[days (quotient z 24)]) |
| 1275 |
(cond |
| 1276 |
[(> days 0) (format #t "dufation: ~ddays ~2,'0dh~2,'0dm~2,'0ds~a\n" |
| 1277 |
days hours mins secs (ns->string t))] |
| 1278 |
[(> hours 0) (format #t "duration: ~dh~2,'0dm~2,'0ds~a\n" |
| 1279 |
hours mins secs (ns->string t))] |
| 1280 |
[else (format #t "duration: ~dm~2,'0ds~a\n" |
| 1281 |
mins secs (ns->string t))]))) |
| 1282 |
(case (time-type t) |
| 1283 |
[(time-utc) (as-date time-utc->date)] |
| 1284 |
[(time-tai) (as-date time-tai->date)] |
| 1285 |
[(time-monotonic) (as-date time-monotonic->date)] |
| 1286 |
[(time-thread time-process time-duration) (as-time t)])) |
| 1287 |
|
| 1288 |
(define-method describe ((t <time>)) |
| 1289 |
(describe-common t) |
| 1290 |
(describe-time t) |
| 1291 |
(describe-slots t)) |