;; SRFI-19: Time Data Types and Procedures. ;; ;; Copyright (C) I/NET, Inc. (2000, 2002, 2003). All Rights Reserved. ;; ;; Permission is hereby granted, free of charge, to any person obtaining ;; a copy of this software and associated documentation files (the ;; "Software"), to deal in the Software without restriction, including ;; without limitation the rights to use, copy, modify, merge, publish, ;; distribute, sublicense, and/or sell copies of the Software, and to ;; permit persons to whom the Software is furnished to do so, subject to ;; the following conditions: ;; ;; The above copyright notice and this permission notice shall be ;; included in all copies or substantial portions of the Software. ;; ;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, ;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF ;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND ;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE ;; LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION ;; OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION ;; WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ;;; Modified for Gauche by Shiro Kawai, shiro@acm.org (define-module srfi.19 (use scheme.list) (use gauche.sequence) (use srfi.13) (export time-tai time-utc time-monotonic time-thread time-process time-duration current-time time-resolution make-time time? time-type time-second time-nanosecond set-time-type! set-time-second! set-time-nanosecond! copy-time time=? time? time>=? time-difference time-difference! add-duration add-duration! subtract-duration subtract-duration! make-date date? date-nanosecond date-second date-minute date-hour date-day date-month date-year date-zone-offset date-year-day date-week-day date-week-number current-date current-julian-day current-modified-julian-day date->julian-day date->modified-julian-day date->time-monotonic date->time-tai date->time-utc julian-day->date julian-day->time-monotonic julian-day->time-tai julian-day->time-utc modified-julian-day->date modified-julian-day->time-monotonic modified-julian-day->time-tai modified-julian-day->time-utc time-monotonic->date time-monotonic->julian-day time-monotonic->modified-julian-day time-monotonic->time-tai time-monotonic->time-tai! time-monotonic->time-utc time-monotonic->time-utc! time-utc->date time-utc->julian-day time-utc->modified-julian-day time-utc->time-monotonic time-utc->time-monotonic! time-utc->time-tai time-utc->time-tai! time-tai->date time-tai->julian-day time-tai->modified-julian-day time-tai->time-monotonic time-tai->time-monotonic! time-tai->time-utc time-tai->time-utc! date->string string->date ) ) (select-module srfi.19) ;;;---------------------------------------------------------- ;;; Constants ;;; (define-constant time-tai 'time-tai) (define-constant time-utc 'time-utc) (define-constant time-monotonic 'time-monotonic) (define-constant time-thread 'time-thread) (define-constant time-process 'time-process) (define-constant time-duration 'time-duration) ;; example of extension (MZScheme specific) ;(define time-gc 'time-gc) ;;-- Miscellaneous Constants. ;;-- only the tm:tai-epoch-in-jd might need changing if ;; a different epoch is used. ;; NB: If the constant TM_EXACT_CALC is true, the calculation of julian-day ;; and modified-julian-day is done in exact calculation. It would have some ;; penalty. Here's some small benchmark results of round-trip conversion from ;; time-utc -> julian-day -> time-utc, repeating 100000 times on 0.8.12. ;; ;; current-time day-boundary half-day-boundary ;; exact 4.67 3.17 2.76 ;; inexact 1.67 1.95 1.94 ;; ;; 'day-boundary' uses (make-time time-utc 0 86400) ;; 'half-day-boundary' uses (make-time time-utc 0 43200) ;; ;; It does slow down the calculation, but I'd say it's not so bad. I'll ;; go for exact calculation for now. (define-constant TM_EXACT_CALC #t) (define-constant tm:nano 1000000000) (define-constant tm:nano/i (if TM_EXACT_CALC tm:nano (exact->inexact tm:nano))) (define-constant tm:sid 86400) ; seconds in a day (define-constant tm:sihd 43200) ; seconds in a half day (define-constant tm:tai-epoch-in-jd ; julian day number for 'the epoch' (let1 off 4881175/2 (if TM_EXACT_CALC off (exact->inexact off)))) ;; each entry is ( tai seconds since epoch . # seconds to subtract for utc ) ;; note they go higher to lower, and end in 1972. ;; See srfi-19/read-tai.scm to update this list. (define-constant tm:leap-second-table '((1483228800 . 37) (1435708800 . 36) (1341100800 . 35) (1230768000 . 34) (1136073600 . 33) (915148800 . 32) (867715200 . 31) (820454400 . 30) (773020800 . 29) (741484800 . 28) (709948800 . 27) (662688000 . 26) (631152000 . 25) (567993600 . 24) (489024000 . 23) (425865600 . 22) (394329600 . 21) (362793600 . 20) (315532800 . 19) (283996800 . 18) (252460800 . 17) (220924800 . 16) (189302400 . 15) (157766400 . 14) (126230400 . 13) (94694400 . 12) (78796800 . 11) (63072000 . 10))) (define-constant tm:leap-second-base (* (- 1972 1970) 365 tm:sid)) (define (tm:leap-second-delta utc-seconds) (if (< utc-seconds tm:leap-second-base) 0 (let loop ((table tm:leap-second-table)) (if (>= utc-seconds (caar table)) (cdar table) (loop (cdr table)))))) ;;;---------------------------------------------------------- ;;; TIME strcture interface ;;; The