srfi-19.scm 52 KB

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