srfi-19.scm 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482
  1. ;;; srfi-19.scm --- Time/Date Library
  2. ;; Copyright (C) 2001, 2002, 2003, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
  3. ;;
  4. ;; This library is free software; you can redistribute it and/or
  5. ;; modify it under the terms of the GNU Lesser General Public
  6. ;; License as published by the Free Software Foundation; either
  7. ;; version 3 of the License, or (at your option) any later version.
  8. ;;
  9. ;; This library is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. ;; Lesser General Public License for more details.
  13. ;;
  14. ;; You should have received a copy of the GNU Lesser General Public
  15. ;; License along with this library; if not, write to the Free Software
  16. ;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. ;;; Author: Rob Browning <rlb@cs.utexas.edu>
  18. ;;; Originally from SRFI reference implementation by Will Fitzgerald.
  19. ;;; Commentary:
  20. ;; This module is fully documented in the Guile Reference Manual.
  21. ;;; Code:
  22. ;; FIXME: I haven't checked a decent amount of this code for potential
  23. ;; performance improvements, but I suspect that there may be some
  24. ;; substantial ones to be realized, esp. in the later "parsing" half
  25. ;; of the file, by rewriting the code with use of more Guile native
  26. ;; functions that do more work in a "chunk".
  27. ;;
  28. ;; FIXME: mkoeppe: Time zones are treated a little simplistic in
  29. ;; SRFI-19; they are only a numeric offset. Thus, printing time zones
  30. ;; (LOCALE-PRINT-TIME-ZONE) can't be implemented sensibly. The
  31. ;; functions taking an optional TZ-OFFSET should be extended to take a
  32. ;; symbolic time-zone (like "CET"); this string should be stored in
  33. ;; the DATE structure.
  34. (define-module (srfi srfi-19)
  35. :use-module (srfi srfi-6)
  36. :use-module (srfi srfi-8)
  37. :use-module (srfi srfi-9)
  38. :autoload (ice-9 rdelim) (read-line)
  39. :use-module (ice-9 i18n)
  40. :replace (current-time)
  41. :export (;; Constants
  42. time-duration
  43. time-monotonic
  44. time-process
  45. time-tai
  46. time-thread
  47. time-utc
  48. ;; Current time and clock resolution
  49. current-date
  50. current-julian-day
  51. current-modified-julian-day
  52. time-resolution
  53. ;; Time object and accessors
  54. make-time
  55. time?
  56. time-type
  57. time-nanosecond
  58. time-second
  59. set-time-type!
  60. set-time-nanosecond!
  61. set-time-second!
  62. copy-time
  63. ;; Time comparison procedures
  64. time<=?
  65. time<?
  66. time=?
  67. time>=?
  68. time>?
  69. ;; Time arithmetic procedures
  70. time-difference
  71. time-difference!
  72. add-duration
  73. add-duration!
  74. subtract-duration
  75. subtract-duration!
  76. ;; Date object and accessors
  77. make-date
  78. date?
  79. date-nanosecond
  80. date-second
  81. date-minute
  82. date-hour
  83. date-day
  84. date-month
  85. date-year
  86. date-zone-offset
  87. date-year-day
  88. date-week-day
  89. date-week-number
  90. ;; Time/Date/Julian Day/Modified Julian Day converters
  91. date->julian-day
  92. date->modified-julian-day
  93. date->time-monotonic
  94. date->time-tai
  95. date->time-utc
  96. julian-day->date
  97. julian-day->time-monotonic
  98. julian-day->time-tai
  99. julian-day->time-utc
  100. modified-julian-day->date
  101. modified-julian-day->time-monotonic
  102. modified-julian-day->time-tai
  103. modified-julian-day->time-utc
  104. time-monotonic->date
  105. time-monotonic->julian-day
  106. time-monotonic->modified-julian-day
  107. time-monotonic->time-tai
  108. time-monotonic->time-tai!
  109. time-monotonic->time-utc
  110. time-monotonic->time-utc!
  111. time-tai->date
  112. time-tai->julian-day
  113. time-tai->modified-julian-day
  114. time-tai->time-monotonic
  115. time-tai->time-monotonic!
  116. time-tai->time-utc
  117. time-tai->time-utc!
  118. time-utc->date
  119. time-utc->julian-day
  120. time-utc->modified-julian-day
  121. time-utc->time-monotonic
  122. time-utc->time-monotonic!
  123. time-utc->time-tai
  124. time-utc->time-tai!
  125. ;; Date to string/string to date converters.
  126. date->string
  127. string->date))
  128. (cond-expand-provide (current-module) '(srfi-19))
  129. (define time-tai 'time-tai)
  130. (define time-utc 'time-utc)
  131. (define time-monotonic 'time-monotonic)
  132. (define time-thread 'time-thread)
  133. (define time-process 'time-process)
  134. (define time-duration 'time-duration)
  135. ;; FIXME: do we want to add gc time?
  136. ;; (define time-gc 'time-gc)
  137. ;;-- LOCALE dependent constants
  138. ;; See date->string
  139. (define locale-date-time-format "~a ~b ~d ~H:~M:~S~z ~Y")
  140. (define locale-short-date-format "~m/~d/~y")
  141. (define locale-time-format "~H:~M:~S")
  142. (define iso-8601-date-time-format "~Y-~m-~dT~H:~M:~S~z")
  143. ;;-- Miscellaneous Constants.
  144. ;;-- only the tai-epoch-in-jd might need changing if
  145. ;; a different epoch is used.
  146. (define nano 1000000000) ; nanoseconds in a second
  147. (define sid 86400) ; seconds in a day
  148. (define sihd 43200) ; seconds in a half day
  149. (define tai-epoch-in-jd 4881175/2) ; julian day number for 'the epoch'
  150. ;; FIXME: should this be something other than misc-error?
  151. (define (time-error caller type value)
  152. (if value
  153. (throw 'misc-error caller "TIME-ERROR type ~A: ~S" (list type value) #f)
  154. (throw 'misc-error caller "TIME-ERROR type ~A" (list type) #f)))
  155. ;; A table of leap seconds
  156. ;; See ftp://maia.usno.navy.mil/ser7/tai-utc.dat
  157. ;; and update as necessary.
  158. ;; this procedures reads the file in the abover
  159. ;; format and creates the leap second table
  160. ;; it also calls the almost standard, but not R5 procedures read-line
  161. ;; & open-input-string
  162. ;; ie (set! leap-second-table (read-tai-utc-date "tai-utc.dat"))
  163. (define (read-tai-utc-data filename)
  164. (define (convert-jd jd)
  165. (* (- (inexact->exact jd) tai-epoch-in-jd) sid))
  166. (define (convert-sec sec)
  167. (inexact->exact sec))
  168. (let ((port (open-input-file filename))
  169. (table '()))
  170. (let loop ((line (read-line port)))
  171. (if (not (eof-object? line))
  172. (begin
  173. (let* ((data (read (open-input-string
  174. (string-append "(" line ")"))))
  175. (year (car data))
  176. (jd (cadddr (cdr data)))
  177. (secs (cadddr (cdddr data))))
  178. (if (>= year 1972)
  179. (set! table (cons
  180. (cons (convert-jd jd) (convert-sec secs))
  181. table)))
  182. (loop (read-line port))))))
  183. table))
  184. ;; each entry is (tai seconds since epoch . # seconds to subtract for utc)
  185. ;; note they go higher to lower, and end in 1972.
  186. (define leap-second-table
  187. '((1136073600 . 33)
  188. (915148800 . 32)
  189. (867715200 . 31)
  190. (820454400 . 30)
  191. (773020800 . 29)
  192. (741484800 . 28)
  193. (709948800 . 27)
  194. (662688000 . 26)
  195. (631152000 . 25)
  196. (567993600 . 24)
  197. (489024000 . 23)
  198. (425865600 . 22)
  199. (394329600 . 21)
  200. (362793600 . 20)
  201. (315532800 . 19)
  202. (283996800 . 18)
  203. (252460800 . 17)
  204. (220924800 . 16)
  205. (189302400 . 15)
  206. (157766400 . 14)
  207. (126230400 . 13)
  208. (94694400 . 12)
  209. (78796800 . 11)
  210. (63072000 . 10)))
  211. (define (read-leap-second-table filename)
  212. (set! leap-second-table (read-tai-utc-data filename)))
  213. (define (leap-second-delta utc-seconds)
  214. (letrec ((lsd (lambda (table)
  215. (cond ((>= utc-seconds (caar table))
  216. (cdar table))
  217. (else (lsd (cdr table)))))))
  218. (if (< utc-seconds (* (- 1972 1970) 365 sid)) 0
  219. (lsd leap-second-table))))
  220. ;;; the TIME structure; creates the accessors, too.
  221. (define-record-type time
  222. (make-time-unnormalized type nanosecond second)
  223. time?
  224. (type time-type set-time-type!)
  225. (nanosecond time-nanosecond set-time-nanosecond!)
  226. (second time-second set-time-second!))
  227. (define (copy-time time)
  228. (make-time (time-type time) (time-nanosecond time) (time-second time)))
  229. (define (split-real r)
  230. (if (integer? r)
  231. (values (inexact->exact r) 0)
  232. (let ((l (truncate r)))
  233. (values (inexact->exact l) (- r l)))))
  234. (define (time-normalize! t)
  235. (if (>= (abs (time-nanosecond t)) 1000000000)
  236. (receive (int frac)
  237. (split-real (time-nanosecond t))
  238. (set-time-second! t (+ (time-second t)
  239. (quotient int 1000000000)))
  240. (set-time-nanosecond! t (+ (remainder int 1000000000)
  241. frac))))
  242. (if (and (positive? (time-second t))
  243. (negative? (time-nanosecond t)))
  244. (begin
  245. (set-time-second! t (- (time-second t) 1))
  246. (set-time-nanosecond! t (+ 1000000000 (time-nanosecond t))))
  247. (if (and (negative? (time-second t))
  248. (positive? (time-nanosecond t)))
  249. (begin
  250. (set-time-second! t (+ (time-second t) 1))
  251. (set-time-nanosecond! t (+ 1000000000 (time-nanosecond t))))))
  252. t)
  253. (define (make-time type nanosecond second)
  254. (time-normalize! (make-time-unnormalized type nanosecond second)))
  255. ;; Helpers
  256. ;; FIXME: finish this and publish it?
  257. (define (date->broken-down-time date)
  258. (let ((result (mktime 0)))
  259. ;; FIXME: What should we do about leap-seconds which may overflow
  260. ;; set-tm:sec?
  261. (set-tm:sec result (date-second date))
  262. (set-tm:min result (date-minute date))
  263. (set-tm:hour result (date-hour date))
  264. ;; FIXME: SRFI day ranges from 0-31. (not compatible with set-tm:mday).
  265. (set-tm:mday result (date-day date))
  266. (set-tm:mon result (- (date-month date) 1))
  267. ;; FIXME: need to signal error on range violation.
  268. (set-tm:year result (+ 1900 (date-year date)))
  269. (set-tm:isdst result -1)
  270. (set-tm:gmtoff result (- (date-zone-offset date)))
  271. result))
  272. ;;; current-time
  273. ;;; specific time getters.
  274. (define (current-time-utc)
  275. ;; Resolution is microseconds.
  276. (let ((tod (gettimeofday)))
  277. (make-time time-utc (* (cdr tod) 1000) (car tod))))
  278. (define (current-time-tai)
  279. ;; Resolution is microseconds.
  280. (let* ((tod (gettimeofday))
  281. (sec (car tod))
  282. (usec (cdr tod)))
  283. (make-time time-tai
  284. (* usec 1000)
  285. (+ (car tod) (leap-second-delta sec)))))
  286. ;;(define (current-time-ms-time time-type proc)
  287. ;; (let ((current-ms (proc)))
  288. ;; (make-time time-type
  289. ;; (quotient current-ms 10000)
  290. ;; (* (remainder current-ms 1000) 10000))))
  291. ;; -- we define it to be the same as TAI.
  292. ;; A different implemation of current-time-montonic
  293. ;; will require rewriting all of the time-monotonic converters,
  294. ;; of course.
  295. (define (current-time-monotonic)
  296. ;; Resolution is microseconds.
  297. (current-time-tai))
  298. (define (current-time-thread)
  299. (time-error 'current-time 'unsupported-clock-type 'time-thread))
  300. (define ns-per-guile-tick (/ 1000000000 internal-time-units-per-second))
  301. (define (current-time-process)
  302. (let ((run-time (get-internal-run-time)))
  303. (make-time
  304. time-process
  305. (* (remainder run-time internal-time-units-per-second)
  306. ns-per-guile-tick)
  307. (quotient run-time internal-time-units-per-second))))
  308. ;;(define (current-time-gc)
  309. ;; (current-time-ms-time time-gc current-gc-milliseconds))
  310. (define (current-time . clock-type)
  311. (let ((clock-type (if (null? clock-type) time-utc (car clock-type))))
  312. (cond
  313. ((eq? clock-type time-tai) (current-time-tai))
  314. ((eq? clock-type time-utc) (current-time-utc))
  315. ((eq? clock-type time-monotonic) (current-time-monotonic))
  316. ((eq? clock-type time-thread) (current-time-thread))
  317. ((eq? clock-type time-process) (current-time-process))
  318. ;; ((eq? clock-type time-gc) (current-time-gc))
  319. (else (time-error 'current-time 'invalid-clock-type clock-type)))))
  320. ;; -- Time Resolution
  321. ;; This is the resolution of the clock in nanoseconds.
  322. ;; This will be implementation specific.
  323. (define (time-resolution . clock-type)
  324. (let ((clock-type (if (null? clock-type) time-utc (car clock-type))))
  325. (case clock-type
  326. ((time-tai) 1000)
  327. ((time-utc) 1000)
  328. ((time-monotonic) 1000)
  329. ((time-process) ns-per-guile-tick)
  330. ;; ((eq? clock-type time-thread) 1000)
  331. ;; ((eq? clock-type time-gc) 10000)
  332. (else (time-error 'time-resolution 'invalid-clock-type clock-type)))))
  333. ;; -- Time comparisons
  334. (define (time=? t1 t2)
  335. ;; Arrange tests for speed and presume that t1 and t2 are actually times.
  336. ;; also presume it will be rare to check two times of different types.
  337. (and (= (time-second t1) (time-second t2))
  338. (= (time-nanosecond t1) (time-nanosecond t2))
  339. (eq? (time-type t1) (time-type t2))))
  340. (define (time>? t1 t2)
  341. (or (> (time-second t1) (time-second t2))
  342. (and (= (time-second t1) (time-second t2))
  343. (> (time-nanosecond t1) (time-nanosecond t2)))))
  344. (define (time<? t1 t2)
  345. (or (< (time-second t1) (time-second t2))
  346. (and (= (time-second t1) (time-second t2))
  347. (< (time-nanosecond t1) (time-nanosecond t2)))))
  348. (define (time>=? t1 t2)
  349. (or (> (time-second t1) (time-second t2))
  350. (and (= (time-second t1) (time-second t2))
  351. (>= (time-nanosecond t1) (time-nanosecond t2)))))
  352. (define (time<=? t1 t2)
  353. (or (< (time-second t1) (time-second t2))
  354. (and (= (time-second t1) (time-second t2))
  355. (<= (time-nanosecond t1) (time-nanosecond t2)))))
  356. ;; -- Time arithmetic
  357. (define (time-difference! time1 time2)
  358. (let ((sec-diff (- (time-second time1) (time-second time2)))
  359. (nsec-diff (- (time-nanosecond time1) (time-nanosecond time2))))
  360. (set-time-type! time1 time-duration)
  361. (set-time-second! time1 sec-diff)
  362. (set-time-nanosecond! time1 nsec-diff)
  363. (time-normalize! time1)))
  364. (define (time-difference time1 time2)
  365. (let ((result (copy-time time1)))
  366. (time-difference! result time2)))
  367. (define (add-duration! t duration)
  368. (if (not (eq? (time-type duration) time-duration))
  369. (time-error 'add-duration 'not-duration duration)
  370. (let ((sec-plus (+ (time-second t) (time-second duration)))
  371. (nsec-plus (+ (time-nanosecond t) (time-nanosecond duration))))
  372. (set-time-second! t sec-plus)
  373. (set-time-nanosecond! t nsec-plus)
  374. (time-normalize! t))))
  375. (define (add-duration t duration)
  376. (let ((result (copy-time t)))
  377. (add-duration! result duration)))
  378. (define (subtract-duration! t duration)
  379. (if (not (eq? (time-type duration) time-duration))
  380. (time-error 'add-duration 'not-duration duration)
  381. (let ((sec-minus (- (time-second t) (time-second duration)))
  382. (nsec-minus (- (time-nanosecond t) (time-nanosecond duration))))
  383. (set-time-second! t sec-minus)
  384. (set-time-nanosecond! t nsec-minus)
  385. (time-normalize! t))))
  386. (define (subtract-duration time1 duration)
  387. (let ((result (copy-time time1)))
  388. (subtract-duration! result duration)))
  389. ;; -- Converters between types.
  390. (define (priv:time-tai->time-utc! time-in time-out caller)
  391. (if (not (eq? (time-type time-in) time-tai))
  392. (time-error caller 'incompatible-time-types time-in))
  393. (set-time-type! time-out time-utc)
  394. (set-time-nanosecond! time-out (time-nanosecond time-in))
  395. (set-time-second! time-out (- (time-second time-in)
  396. (leap-second-delta
  397. (time-second time-in))))
  398. time-out)
  399. (define (time-tai->time-utc time-in)
  400. (priv:time-tai->time-utc! time-in (make-time-unnormalized #f #f #f) 'time-tai->time-utc))
  401. (define (time-tai->time-utc! time-in)
  402. (priv:time-tai->time-utc! time-in time-in 'time-tai->time-utc!))
  403. (define (priv:time-utc->time-tai! time-in time-out caller)
  404. (if (not (eq? (time-type time-in) time-utc))
  405. (time-error caller 'incompatible-time-types time-in))
  406. (set-time-type! time-out time-tai)
  407. (set-time-nanosecond! time-out (time-nanosecond time-in))
  408. (set-time-second! time-out (+ (time-second time-in)
  409. (leap-second-delta
  410. (time-second time-in))))
  411. time-out)
  412. (define (time-utc->time-tai time-in)
  413. (priv:time-utc->time-tai! time-in (make-time-unnormalized #f #f #f) 'time-utc->time-tai))
  414. (define (time-utc->time-tai! time-in)
  415. (priv:time-utc->time-tai! time-in time-in 'time-utc->time-tai!))
  416. ;; -- these depend on time-monotonic having the same definition as time-tai!
  417. (define (time-monotonic->time-utc time-in)
  418. (if (not (eq? (time-type time-in) time-monotonic))
  419. (time-error 'time-monotonic->time-utc
  420. 'incompatible-time-types time-in))
  421. (let ((ntime (copy-time time-in)))
  422. (set-time-type! ntime time-tai)
  423. (priv:time-tai->time-utc! ntime ntime 'time-monotonic->time-utc)))
  424. (define (time-monotonic->time-utc! time-in)
  425. (if (not (eq? (time-type time-in) time-monotonic))
  426. (time-error 'time-monotonic->time-utc!
  427. 'incompatible-time-types time-in))
  428. (set-time-type! time-in time-tai)
  429. (priv:time-tai->time-utc! time-in time-in 'time-monotonic->time-utc))
  430. (define (time-monotonic->time-tai time-in)
  431. (if (not (eq? (time-type time-in) time-monotonic))
  432. (time-error 'time-monotonic->time-tai
  433. 'incompatible-time-types time-in))
  434. (let ((ntime (copy-time time-in)))
  435. (set-time-type! ntime time-tai)
  436. ntime))
  437. (define (time-monotonic->time-tai! time-in)
  438. (if (not (eq? (time-type time-in) time-monotonic))
  439. (time-error 'time-monotonic->time-tai!
  440. '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. (time-error 'time-utc->time-monotonic
  446. 'incompatible-time-types time-in))
  447. (let ((ntime (priv:time-utc->time-tai! time-in (make-time-unnormalized #f #f #f)
  448. 'time-utc->time-monotonic)))
  449. (set-time-type! ntime time-monotonic)
  450. ntime))
  451. (define (time-utc->time-monotonic! time-in)
  452. (if (not (eq? (time-type time-in) time-utc))
  453. (time-error 'time-utc->time-monotonic!
  454. 'incompatible-time-types time-in))
  455. (let ((ntime (priv:time-utc->time-tai! time-in time-in
  456. 'time-utc->time-monotonic!)))
  457. (set-time-type! ntime time-monotonic)
  458. ntime))
  459. (define (time-tai->time-monotonic time-in)
  460. (if (not (eq? (time-type time-in) time-tai))
  461. (time-error 'time-tai->time-monotonic
  462. 'incompatible-time-types time-in))
  463. (let ((ntime (copy-time time-in)))
  464. (set-time-type! ntime time-monotonic)
  465. ntime))
  466. (define (time-tai->time-monotonic! time-in)
  467. (if (not (eq? (time-type time-in) time-tai))
  468. (time-error 'time-tai->time-monotonic!
  469. 'incompatible-time-types time-in))
  470. (set-time-type! time-in time-monotonic)
  471. time-in)
  472. ;; -- Date Structures
  473. ;; FIXME: to be really safe, perhaps we should normalize the
  474. ;; seconds/nanoseconds/minutes coming in to make-date...
  475. (define-record-type date
  476. (make-date nanosecond second minute
  477. hour day month
  478. year
  479. zone-offset)
  480. date?
  481. (nanosecond date-nanosecond set-date-nanosecond!)
  482. (second date-second set-date-second!)
  483. (minute date-minute set-date-minute!)
  484. (hour date-hour set-date-hour!)
  485. (day date-day set-date-day!)
  486. (month date-month set-date-month!)
  487. (year date-year set-date-year!)
  488. (zone-offset date-zone-offset set-date-zone-offset!))
  489. ;; gives the julian day which starts at noon.
  490. (define (encode-julian-day-number day month year)
  491. (let* ((a (quotient (- 14 month) 12))
  492. (y (- (+ year 4800) a (if (negative? year) -1 0)))
  493. (m (- (+ month (* 12 a)) 3)))
  494. (+ day
  495. (quotient (+ (* 153 m) 2) 5)
  496. (* 365 y)
  497. (quotient y 4)
  498. (- (quotient y 100))
  499. (quotient y 400)
  500. -32045)))
  501. ;; gives the seconds/date/month/year
  502. (define (decode-julian-day-number jdn)
  503. (let* ((days (inexact->exact (truncate jdn)))
  504. (a (+ days 32044))
  505. (b (quotient (+ (* 4 a) 3) 146097))
  506. (c (- a (quotient (* 146097 b) 4)))
  507. (d (quotient (+ (* 4 c) 3) 1461))
  508. (e (- c (quotient (* 1461 d) 4)))
  509. (m (quotient (+ (* 5 e) 2) 153))
  510. (y (+ (* 100 b) d -4800 (quotient m 10))))
  511. (values ; seconds date month year
  512. (* (- jdn days) sid)
  513. (+ e (- (quotient (+ (* 153 m) 2) 5)) 1)
  514. (+ m 3 (* -12 (quotient m 10)))
  515. (if (>= 0 y) (- y 1) y))))
  516. ;; relies on the fact that we named our time zone accessor
  517. ;; differently from MzScheme's....
  518. ;; This should be written to be OS specific.
  519. (define (local-tz-offset utc-time)
  520. ;; SRFI uses seconds West, but guile (and libc) use seconds East.
  521. (- (tm:gmtoff (localtime (time-second utc-time)))))
  522. ;; special thing -- ignores nanos
  523. (define (time->julian-day-number seconds tz-offset)
  524. (+ (/ (+ seconds tz-offset sihd)
  525. sid)
  526. tai-epoch-in-jd))
  527. (define (leap-second? second)
  528. (and (assoc second leap-second-table) #t))
  529. (define (time-utc->date time . tz-offset)
  530. (if (not (eq? (time-type time) time-utc))
  531. (time-error 'time->date 'incompatible-time-types time))
  532. (let* ((offset (if (null? tz-offset)
  533. (local-tz-offset time)
  534. (car tz-offset)))
  535. (leap-second? (leap-second? (+ offset (time-second time))))
  536. (jdn (time->julian-day-number (if leap-second?
  537. (- (time-second time) 1)
  538. (time-second time))
  539. offset)))
  540. (call-with-values (lambda () (decode-julian-day-number jdn))
  541. (lambda (secs date month year)
  542. ;; secs is a real because jdn is a real in Guile;
  543. ;; but it is conceptionally an integer.
  544. (let* ((int-secs (inexact->exact (round secs)))
  545. (hours (quotient int-secs (* 60 60)))
  546. (rem (remainder int-secs (* 60 60)))
  547. (minutes (quotient rem 60))
  548. (seconds (remainder rem 60)))
  549. (make-date (time-nanosecond time)
  550. (if leap-second? (+ seconds 1) seconds)
  551. minutes
  552. hours
  553. date
  554. month
  555. year
  556. offset))))))
  557. (define (time-tai->date time . tz-offset)
  558. (if (not (eq? (time-type time) time-tai))
  559. (time-error 'time->date 'incompatible-time-types time))
  560. (let* ((offset (if (null? tz-offset)
  561. (local-tz-offset (time-tai->time-utc time))
  562. (car tz-offset)))
  563. (seconds (- (time-second time)
  564. (leap-second-delta (time-second time))))
  565. (leap-second? (leap-second? (+ offset seconds)))
  566. (jdn (time->julian-day-number (if leap-second?
  567. (- seconds 1)
  568. seconds)
  569. offset)))
  570. (call-with-values (lambda () (decode-julian-day-number jdn))
  571. (lambda (secs date month year)
  572. ;; secs is a real because jdn is a real in Guile;
  573. ;; but it is conceptionally an integer.
  574. ;; adjust for leap seconds if necessary ...
  575. (let* ((int-secs (inexact->exact (round secs)))
  576. (hours (quotient int-secs (* 60 60)))
  577. (rem (remainder int-secs (* 60 60)))
  578. (minutes (quotient rem 60))
  579. (seconds (remainder rem 60)))
  580. (make-date (time-nanosecond time)
  581. (if leap-second? (+ seconds 1) seconds)
  582. minutes
  583. hours
  584. date
  585. month
  586. year
  587. offset))))))
  588. ;; this is the same as time-tai->date.
  589. (define (time-monotonic->date time . tz-offset)
  590. (if (not (eq? (time-type time) time-monotonic))
  591. (time-error 'time->date 'incompatible-time-types time))
  592. (let* ((offset (if (null? tz-offset)
  593. (local-tz-offset (time-monotonic->time-utc time))
  594. (car tz-offset)))
  595. (seconds (- (time-second time)
  596. (leap-second-delta (time-second time))))
  597. (leap-second? (leap-second? (+ offset seconds)))
  598. (jdn (time->julian-day-number (if leap-second?
  599. (- seconds 1)
  600. seconds)
  601. offset)))
  602. (call-with-values (lambda () (decode-julian-day-number jdn))
  603. (lambda (secs date month year)
  604. ;; secs is a real because jdn is a real in Guile;
  605. ;; but it is conceptionally an integer.
  606. ;; adjust for leap seconds if necessary ...
  607. (let* ((int-secs (inexact->exact (round secs)))
  608. (hours (quotient int-secs (* 60 60)))
  609. (rem (remainder int-secs (* 60 60)))
  610. (minutes (quotient rem 60))
  611. (seconds (remainder rem 60)))
  612. (make-date (time-nanosecond time)
  613. (if leap-second? (+ seconds 1) seconds)
  614. minutes
  615. hours
  616. date
  617. month
  618. year
  619. offset))))))
  620. (define (date->time-utc date)
  621. (let* ((jdays (- (encode-julian-day-number (date-day date)
  622. (date-month date)
  623. (date-year date))
  624. tai-epoch-in-jd))
  625. ;; jdays is an integer plus 1/2,
  626. (jdays-1/2 (inexact->exact (- jdays 1/2))))
  627. (make-time
  628. time-utc
  629. (date-nanosecond date)
  630. (+ (* jdays-1/2 24 60 60)
  631. (* (date-hour date) 60 60)
  632. (* (date-minute date) 60)
  633. (date-second date)
  634. (- (date-zone-offset date))))))
  635. (define (date->time-tai date)
  636. (time-utc->time-tai! (date->time-utc date)))
  637. (define (date->time-monotonic date)
  638. (time-utc->time-monotonic! (date->time-utc date)))
  639. (define (leap-year? year)
  640. (or (= (modulo year 400) 0)
  641. (and (= (modulo year 4) 0) (not (= (modulo year 100) 0)))))
  642. ;; Map 1-based month number M to number of days in the year before the
  643. ;; start of month M (in a non-leap year).
  644. (define month-assoc '((1 . 0) (2 . 31) (3 . 59) (4 . 90)
  645. (5 . 120) (6 . 151) (7 . 181) (8 . 212)
  646. (9 . 243) (10 . 273) (11 . 304) (12 . 334)))
  647. (define (year-day day month year)
  648. (let ((days-pr (assoc month month-assoc)))
  649. (if (not days-pr)
  650. (time-error 'date-year-day 'invalid-month-specification month))
  651. (if (and (leap-year? year) (> month 2))
  652. (+ day (cdr days-pr) 1)
  653. (+ day (cdr days-pr)))))
  654. (define (date-year-day date)
  655. (year-day (date-day date) (date-month date) (date-year date)))
  656. ;; from calendar faq
  657. (define (week-day day month year)
  658. (let* ((a (quotient (- 14 month) 12))
  659. (y (- year a))
  660. (m (+ month (* 12 a) -2)))
  661. (modulo (+ day
  662. y
  663. (quotient y 4)
  664. (- (quotient y 100))
  665. (quotient y 400)
  666. (quotient (* 31 m) 12))
  667. 7)))
  668. (define (date-week-day date)
  669. (week-day (date-day date) (date-month date) (date-year date)))
  670. (define (days-before-first-week date day-of-week-starting-week)
  671. (let* ((first-day (make-date 0 0 0 0
  672. 1
  673. 1
  674. (date-year date)
  675. #f))
  676. (fdweek-day (date-week-day first-day)))
  677. (modulo (- day-of-week-starting-week fdweek-day)
  678. 7)))
  679. ;; The "-1" here is a fix for the reference implementation, to make a new
  680. ;; week start on the given day-of-week-starting-week. date-year-day returns
  681. ;; a day starting from 1 for 1st Jan.
  682. ;;
  683. (define (date-week-number date day-of-week-starting-week)
  684. (quotient (- (date-year-day date)
  685. 1
  686. (days-before-first-week date day-of-week-starting-week))
  687. 7))
  688. (define (current-date . tz-offset)
  689. (let ((time (current-time time-utc)))
  690. (time-utc->date
  691. time
  692. (if (null? tz-offset)
  693. (local-tz-offset time)
  694. (car tz-offset)))))
  695. ;; given a 'two digit' number, find the year within 50 years +/-
  696. (define (natural-year n)
  697. (let* ((current-year (date-year (current-date)))
  698. (current-century (* (quotient current-year 100) 100)))
  699. (cond
  700. ((>= n 100) n)
  701. ((< n 0) n)
  702. ((<= (- (+ current-century n) current-year) 50) (+ current-century n))
  703. (else (+ (- current-century 100) n)))))
  704. (define (date->julian-day date)
  705. (let ((nanosecond (date-nanosecond date))
  706. (second (date-second date))
  707. (minute (date-minute date))
  708. (hour (date-hour date))
  709. (day (date-day date))
  710. (month (date-month date))
  711. (year (date-year date))
  712. (offset (date-zone-offset date)))
  713. (+ (encode-julian-day-number day month year)
  714. (- 1/2)
  715. (+ (/ (+ (- offset)
  716. (* hour 60 60)
  717. (* minute 60)
  718. second
  719. (/ nanosecond nano))
  720. sid)))))
  721. (define (date->modified-julian-day date)
  722. (- (date->julian-day date)
  723. 4800001/2))
  724. (define (time-utc->julian-day time)
  725. (if (not (eq? (time-type time) time-utc))
  726. (time-error 'time->date 'incompatible-time-types time))
  727. (+ (/ (+ (time-second time) (/ (time-nanosecond time) nano))
  728. sid)
  729. tai-epoch-in-jd))
  730. (define (time-utc->modified-julian-day time)
  731. (- (time-utc->julian-day time)
  732. 4800001/2))
  733. (define (time-tai->julian-day time)
  734. (if (not (eq? (time-type time) time-tai))
  735. (time-error 'time->date 'incompatible-time-types time))
  736. (+ (/ (+ (- (time-second time)
  737. (leap-second-delta (time-second time)))
  738. (/ (time-nanosecond time) nano))
  739. sid)
  740. tai-epoch-in-jd))
  741. (define (time-tai->modified-julian-day time)
  742. (- (time-tai->julian-day time)
  743. 4800001/2))
  744. ;; this is the same as time-tai->julian-day
  745. (define (time-monotonic->julian-day time)
  746. (if (not (eq? (time-type time) time-monotonic))
  747. (time-error 'time->date 'incompatible-time-types time))
  748. (+ (/ (+ (- (time-second time)
  749. (leap-second-delta (time-second time)))
  750. (/ (time-nanosecond time) nano))
  751. sid)
  752. tai-epoch-in-jd))
  753. (define (time-monotonic->modified-julian-day time)
  754. (- (time-monotonic->julian-day time)
  755. 4800001/2))
  756. (define (julian-day->time-utc jdn)
  757. (let ((secs (* sid (- jdn tai-epoch-in-jd))))
  758. (receive (seconds parts)
  759. (split-real secs)
  760. (make-time time-utc
  761. (* parts nano)
  762. seconds))))
  763. (define (julian-day->time-tai jdn)
  764. (time-utc->time-tai! (julian-day->time-utc jdn)))
  765. (define (julian-day->time-monotonic jdn)
  766. (time-utc->time-monotonic! (julian-day->time-utc jdn)))
  767. (define (julian-day->date jdn . tz-offset)
  768. (let* ((time (julian-day->time-utc jdn))
  769. (offset (if (null? tz-offset)
  770. (local-tz-offset time)
  771. (car tz-offset))))
  772. (time-utc->date time offset)))
  773. (define (modified-julian-day->date jdn . tz-offset)
  774. (apply julian-day->date (+ jdn 4800001/2)
  775. tz-offset))
  776. (define (modified-julian-day->time-utc jdn)
  777. (julian-day->time-utc (+ jdn 4800001/2)))
  778. (define (modified-julian-day->time-tai jdn)
  779. (julian-day->time-tai (+ jdn 4800001/2)))
  780. (define (modified-julian-day->time-monotonic jdn)
  781. (julian-day->time-monotonic (+ jdn 4800001/2)))
  782. (define (current-julian-day)
  783. (time-utc->julian-day (current-time time-utc)))
  784. (define (current-modified-julian-day)
  785. (time-utc->modified-julian-day (current-time time-utc)))
  786. ;; returns a string rep. of number N, of minimum LENGTH, padded with
  787. ;; character PAD-WITH. If PAD-WITH is #f, no padding is done, and it's
  788. ;; as if number->string was used. if string is longer than or equal
  789. ;; in length to LENGTH, it's as if number->string was used.
  790. (define (padding n pad-with length)
  791. (let* ((str (number->string n))
  792. (str-len (string-length str)))
  793. (if (or (>= str-len length)
  794. (not pad-with))
  795. str
  796. (string-append (make-string (- length str-len) pad-with) str))))
  797. (define (last-n-digits i n)
  798. (abs (remainder i (expt 10 n))))
  799. (define (locale-abbr-weekday n) (locale-day-short (+ 1 n)))
  800. (define (locale-long-weekday n) (locale-day (+ 1 n)))
  801. (define locale-abbr-month locale-month-short)
  802. (define locale-long-month locale-month)
  803. (define (date-reverse-lookup needle haystack-ref haystack-len
  804. same?)
  805. ;; Lookup NEEDLE (a string) using HAYSTACK-REF (a one argument procedure
  806. ;; that returns a string corresponding to the given index) by passing it
  807. ;; indices lower than HAYSTACK-LEN.
  808. (let loop ((index 1))
  809. (cond ((> index haystack-len) #f)
  810. ((same? needle (haystack-ref index))
  811. index)
  812. (else (loop (+ index 1))))))
  813. (define (locale-abbr-weekday->index string)
  814. (date-reverse-lookup string locale-day-short 7 string=?))
  815. (define (locale-long-weekday->index string)
  816. (date-reverse-lookup string locale-day 7 string=?))
  817. (define (locale-abbr-month->index string)
  818. (date-reverse-lookup string locale-abbr-month 12 string=?))
  819. (define (locale-long-month->index string)
  820. (date-reverse-lookup string locale-long-month 12 string=?))
  821. ;; FIXME: mkoeppe: Put a symbolic time zone in the date structs.
  822. ;; Print it here instead of the numerical offset if available.
  823. (define (locale-print-time-zone date port)
  824. (tz-printer (date-zone-offset date) port))
  825. (define (locale-am-string/pm hr)
  826. (if (> hr 11) (locale-pm-string) (locale-am-string)))
  827. (define (tz-printer offset port)
  828. (cond
  829. ((= offset 0) (display "Z" port))
  830. ((negative? offset) (display "-" port))
  831. (else (display "+" port)))
  832. (if (not (= offset 0))
  833. (let ((hours (abs (quotient offset (* 60 60))))
  834. (minutes (abs (quotient (remainder offset (* 60 60)) 60))))
  835. (display (padding hours #\0 2) port)
  836. (display (padding minutes #\0 2) port))))
  837. ;; A table of output formatting directives.
  838. ;; the first time is the format char.
  839. ;; the second is a procedure that takes the date, a padding character
  840. ;; (which might be #f), and the output port.
  841. ;;
  842. (define directives
  843. (list
  844. (cons #\~ (lambda (date pad-with port)
  845. (display #\~ port)))
  846. (cons #\a (lambda (date pad-with port)
  847. (display (locale-abbr-weekday (date-week-day date))
  848. port)))
  849. (cons #\A (lambda (date pad-with port)
  850. (display (locale-long-weekday (date-week-day date))
  851. port)))
  852. (cons #\b (lambda (date pad-with port)
  853. (display (locale-abbr-month (date-month date))
  854. port)))
  855. (cons #\B (lambda (date pad-with port)
  856. (display (locale-long-month (date-month date))
  857. port)))
  858. (cons #\c (lambda (date pad-with port)
  859. (display (date->string date locale-date-time-format) port)))
  860. (cons #\d (lambda (date pad-with port)
  861. (display (padding (date-day date)
  862. #\0 2)
  863. port)))
  864. (cons #\D (lambda (date pad-with port)
  865. (display (date->string date "~m/~d/~y") port)))
  866. (cons #\e (lambda (date pad-with port)
  867. (display (padding (date-day date)
  868. #\Space 2)
  869. port)))
  870. (cons #\f (lambda (date pad-with port)
  871. (if (> (date-nanosecond date)
  872. nano)
  873. (display (padding (+ (date-second date) 1)
  874. pad-with 2)
  875. port)
  876. (display (padding (date-second date)
  877. pad-with 2)
  878. port))
  879. (receive (i f)
  880. (split-real (/
  881. (date-nanosecond date)
  882. nano 1.0))
  883. (let* ((ns (number->string f))
  884. (le (string-length ns)))
  885. (if (> le 2)
  886. (begin
  887. (display (locale-decimal-point) port)
  888. (display (substring ns 2 le) port)))))))
  889. (cons #\h (lambda (date pad-with port)
  890. (display (date->string date "~b") port)))
  891. (cons #\H (lambda (date pad-with port)
  892. (display (padding (date-hour date)
  893. pad-with 2)
  894. port)))
  895. (cons #\I (lambda (date pad-with port)
  896. (let ((hr (date-hour date)))
  897. (if (> hr 12)
  898. (display (padding (- hr 12)
  899. pad-with 2)
  900. port)
  901. (display (padding hr
  902. pad-with 2)
  903. port)))))
  904. (cons #\j (lambda (date pad-with port)
  905. (display (padding (date-year-day date)
  906. pad-with 3)
  907. port)))
  908. (cons #\k (lambda (date pad-with port)
  909. (display (padding (date-hour date)
  910. #\Space 2)
  911. port)))
  912. (cons #\l (lambda (date pad-with port)
  913. (let ((hr (if (> (date-hour date) 12)
  914. (- (date-hour date) 12) (date-hour date))))
  915. (display (padding hr #\Space 2)
  916. port))))
  917. (cons #\m (lambda (date pad-with port)
  918. (display (padding (date-month date)
  919. pad-with 2)
  920. port)))
  921. (cons #\M (lambda (date pad-with port)
  922. (display (padding (date-minute date)
  923. pad-with 2)
  924. port)))
  925. (cons #\n (lambda (date pad-with port)
  926. (newline port)))
  927. (cons #\N (lambda (date pad-with port)
  928. (display (padding (date-nanosecond date)
  929. pad-with 7)
  930. port)))
  931. (cons #\p (lambda (date pad-with port)
  932. (display (locale-am-string/pm (date-hour date)) port)))
  933. (cons #\r (lambda (date pad-with port)
  934. (display (date->string date "~I:~M:~S ~p") port)))
  935. (cons #\s (lambda (date pad-with port)
  936. (display (time-second (date->time-utc date)) port)))
  937. (cons #\S (lambda (date pad-with port)
  938. (if (> (date-nanosecond date)
  939. nano)
  940. (display (padding (+ (date-second date) 1)
  941. pad-with 2)
  942. port)
  943. (display (padding (date-second date)
  944. pad-with 2)
  945. port))))
  946. (cons #\t (lambda (date pad-with port)
  947. (display #\Tab port)))
  948. (cons #\T (lambda (date pad-with port)
  949. (display (date->string date "~H:~M:~S") port)))
  950. (cons #\U (lambda (date pad-with port)
  951. (if (> (days-before-first-week date 0) 0)
  952. (display (padding (+ (date-week-number date 0) 1)
  953. #\0 2) port)
  954. (display (padding (date-week-number date 0)
  955. #\0 2) port))))
  956. (cons #\V (lambda (date pad-with port)
  957. (display (padding (date-week-number date 1)
  958. #\0 2) port)))
  959. (cons #\w (lambda (date pad-with port)
  960. (display (date-week-day date) port)))
  961. (cons #\x (lambda (date pad-with port)
  962. (display (date->string date locale-short-date-format) port)))
  963. (cons #\X (lambda (date pad-with port)
  964. (display (date->string date locale-time-format) port)))
  965. (cons #\W (lambda (date pad-with port)
  966. (if (> (days-before-first-week date 1) 0)
  967. (display (padding (+ (date-week-number date 1) 1)
  968. #\0 2) port)
  969. (display (padding (date-week-number date 1)
  970. #\0 2) port))))
  971. (cons #\y (lambda (date pad-with port)
  972. (display (padding (last-n-digits
  973. (date-year date) 2)
  974. pad-with
  975. 2)
  976. port)))
  977. (cons #\Y (lambda (date pad-with port)
  978. (display (date-year date) port)))
  979. (cons #\z (lambda (date pad-with port)
  980. (tz-printer (date-zone-offset date) port)))
  981. (cons #\Z (lambda (date pad-with port)
  982. (locale-print-time-zone date port)))
  983. (cons #\1 (lambda (date pad-with port)
  984. (display (date->string date "~Y-~m-~d") port)))
  985. (cons #\2 (lambda (date pad-with port)
  986. (display (date->string date "~k:~M:~S~z") port)))
  987. (cons #\3 (lambda (date pad-with port)
  988. (display (date->string date "~k:~M:~S") port)))
  989. (cons #\4 (lambda (date pad-with port)
  990. (display (date->string date "~Y-~m-~dT~k:~M:~S~z") port)))
  991. (cons #\5 (lambda (date pad-with port)
  992. (display (date->string date "~Y-~m-~dT~k:~M:~S") port)))))
  993. (define (get-formatter char)
  994. (let ((associated (assoc char directives)))
  995. (if associated (cdr associated) #f)))
  996. (define (date-printer date index format-string str-len port)
  997. (if (< index str-len)
  998. (let ((current-char (string-ref format-string index)))
  999. (if (not (char=? current-char #\~))
  1000. (begin
  1001. (display current-char port)
  1002. (date-printer date (+ index 1) format-string str-len port))
  1003. (if (= (+ index 1) str-len) ; bad format string.
  1004. (time-error 'date-printer 'bad-date-format-string
  1005. format-string)
  1006. (let ((pad-char? (string-ref format-string (+ index 1))))
  1007. (cond
  1008. ((char=? pad-char? #\-)
  1009. (if (= (+ index 2) str-len) ; bad format string.
  1010. (time-error 'date-printer
  1011. 'bad-date-format-string
  1012. format-string)
  1013. (let ((formatter (get-formatter
  1014. (string-ref format-string
  1015. (+ index 2)))))
  1016. (if (not formatter)
  1017. (time-error 'date-printer
  1018. 'bad-date-format-string
  1019. format-string)
  1020. (begin
  1021. (formatter date #f port)
  1022. (date-printer date
  1023. (+ index 3)
  1024. format-string
  1025. str-len
  1026. port))))))
  1027. ((char=? pad-char? #\_)
  1028. (if (= (+ index 2) str-len) ; bad format string.
  1029. (time-error 'date-printer
  1030. 'bad-date-format-string
  1031. format-string)
  1032. (let ((formatter (get-formatter
  1033. (string-ref format-string
  1034. (+ index 2)))))
  1035. (if (not formatter)
  1036. (time-error 'date-printer
  1037. 'bad-date-format-string
  1038. format-string)
  1039. (begin
  1040. (formatter date #\Space port)
  1041. (date-printer date
  1042. (+ index 3)
  1043. format-string
  1044. str-len
  1045. port))))))
  1046. (else
  1047. (let ((formatter (get-formatter
  1048. (string-ref format-string
  1049. (+ index 1)))))
  1050. (if (not formatter)
  1051. (time-error 'date-printer
  1052. 'bad-date-format-string
  1053. format-string)
  1054. (begin
  1055. (formatter date #\0 port)
  1056. (date-printer date
  1057. (+ index 2)
  1058. format-string
  1059. str-len
  1060. port))))))))))))
  1061. (define (date->string date . format-string)
  1062. (let ((str-port (open-output-string))
  1063. (fmt-str (if (null? format-string) "~c" (car format-string))))
  1064. (date-printer date 0 fmt-str (string-length fmt-str) str-port)
  1065. (get-output-string str-port)))
  1066. (define (char->int ch)
  1067. (case ch
  1068. ((#\0) 0)
  1069. ((#\1) 1)
  1070. ((#\2) 2)
  1071. ((#\3) 3)
  1072. ((#\4) 4)
  1073. ((#\5) 5)
  1074. ((#\6) 6)
  1075. ((#\7) 7)
  1076. ((#\8) 8)
  1077. ((#\9) 9)
  1078. (else (time-error 'char->int 'bad-date-template-string
  1079. (list "Non-integer character" ch)))))
  1080. ;; read an integer upto n characters long on port; upto -> #f is any length
  1081. (define (integer-reader upto port)
  1082. (let loop ((accum 0) (nchars 0))
  1083. (let ((ch (peek-char port)))
  1084. (if (or (eof-object? ch)
  1085. (not (char-numeric? ch))
  1086. (and upto (>= nchars upto)))
  1087. accum
  1088. (loop (+ (* accum 10) (char->int (read-char port)))
  1089. (+ nchars 1))))))
  1090. (define (make-integer-reader upto)
  1091. (lambda (port)
  1092. (integer-reader upto port)))
  1093. ;; read *exactly* n characters and convert to integer; could be padded
  1094. (define (integer-reader-exact n port)
  1095. (let ((padding-ok #t))
  1096. (define (accum-int port accum nchars)
  1097. (let ((ch (peek-char port)))
  1098. (cond
  1099. ((>= nchars n) accum)
  1100. ((eof-object? ch)
  1101. (time-error 'string->date 'bad-date-template-string
  1102. "Premature ending to integer read."))
  1103. ((char-numeric? ch)
  1104. (set! padding-ok #f)
  1105. (accum-int port
  1106. (+ (* accum 10) (char->int (read-char port)))
  1107. (+ nchars 1)))
  1108. (padding-ok
  1109. (read-char port) ; consume padding
  1110. (accum-int port accum (+ nchars 1)))
  1111. (else ; padding where it shouldn't be
  1112. (time-error 'string->date 'bad-date-template-string
  1113. "Non-numeric characters in integer read.")))))
  1114. (accum-int port 0 0)))
  1115. (define (make-integer-exact-reader n)
  1116. (lambda (port)
  1117. (integer-reader-exact n port)))
  1118. (define (zone-reader port)
  1119. (let ((offset 0)
  1120. (positive? #f))
  1121. (let ((ch (read-char port)))
  1122. (if (eof-object? ch)
  1123. (time-error 'string->date 'bad-date-template-string
  1124. (list "Invalid time zone +/-" ch)))
  1125. (if (or (char=? ch #\Z) (char=? ch #\z))
  1126. 0
  1127. (begin
  1128. (cond
  1129. ((char=? ch #\+) (set! positive? #t))
  1130. ((char=? ch #\-) (set! positive? #f))
  1131. (else
  1132. (time-error 'string->date 'bad-date-template-string
  1133. (list "Invalid time zone +/-" ch))))
  1134. (let ((ch (read-char port)))
  1135. (if (eof-object? ch)
  1136. (time-error 'string->date 'bad-date-template-string
  1137. (list "Invalid time zone number" ch)))
  1138. (set! offset (* (char->int ch)
  1139. 10 60 60)))
  1140. (let ((ch (read-char port)))
  1141. (if (eof-object? ch)
  1142. (time-error 'string->date 'bad-date-template-string
  1143. (list "Invalid time zone number" ch)))
  1144. (set! offset (+ offset (* (char->int ch)
  1145. 60 60))))
  1146. (let ((ch (read-char port)))
  1147. (if (eof-object? ch)
  1148. (time-error 'string->date 'bad-date-template-string
  1149. (list "Invalid time zone number" ch)))
  1150. (set! offset (+ offset (* (char->int ch)
  1151. 10 60))))
  1152. (let ((ch (read-char port)))
  1153. (if (eof-object? ch)
  1154. (time-error 'string->date 'bad-date-template-string
  1155. (list "Invalid time zone number" ch)))
  1156. (set! offset (+ offset (* (char->int ch)
  1157. 60))))
  1158. (if positive? offset (- offset)))))))
  1159. ;; looking at a char, read the char string, run thru indexer, return index
  1160. (define (locale-reader port indexer)
  1161. (define (read-char-string result)
  1162. (let ((ch (peek-char port)))
  1163. (if (char-alphabetic? ch)
  1164. (read-char-string (cons (read-char port) result))
  1165. (list->string (reverse! result)))))
  1166. (let* ((str (read-char-string '()))
  1167. (index (indexer str)))
  1168. (if index index (time-error 'string->date
  1169. 'bad-date-template-string
  1170. (list "Invalid string for " indexer)))))
  1171. (define (make-locale-reader indexer)
  1172. (lambda (port)
  1173. (locale-reader port indexer)))
  1174. (define (make-char-id-reader char)
  1175. (lambda (port)
  1176. (if (char=? char (read-char port))
  1177. char
  1178. (time-error 'string->date
  1179. 'bad-date-template-string
  1180. "Invalid character match."))))
  1181. ;; A List of formatted read directives.
  1182. ;; Each entry is a list.
  1183. ;; 1. the character directive;
  1184. ;; a procedure, which takes a character as input & returns
  1185. ;; 2. #t as soon as a character on the input port is acceptable
  1186. ;; for input,
  1187. ;; 3. a port reader procedure that knows how to read the current port
  1188. ;; for a value. Its one parameter is the port.
  1189. ;; 4. an optional action procedure, that takes the value (from 3.) and
  1190. ;; some object (here, always the date) and (probably) side-effects it.
  1191. ;; If no action is required, as with ~A, this element may be #f.
  1192. (define read-directives
  1193. (let ((ireader4 (make-integer-reader 4))
  1194. (ireader2 (make-integer-reader 2))
  1195. (eireader2 (make-integer-exact-reader 2))
  1196. (locale-reader-abbr-weekday (make-locale-reader
  1197. locale-abbr-weekday->index))
  1198. (locale-reader-long-weekday (make-locale-reader
  1199. locale-long-weekday->index))
  1200. (locale-reader-abbr-month (make-locale-reader
  1201. locale-abbr-month->index))
  1202. (locale-reader-long-month (make-locale-reader
  1203. locale-long-month->index))
  1204. (char-fail (lambda (ch) #t)))
  1205. (list
  1206. (list #\~ char-fail (make-char-id-reader #\~) #f)
  1207. (list #\a char-alphabetic? locale-reader-abbr-weekday #f)
  1208. (list #\A char-alphabetic? locale-reader-long-weekday #f)
  1209. (list #\b char-alphabetic? locale-reader-abbr-month
  1210. (lambda (val object)
  1211. (set-date-month! object val)))
  1212. (list #\B char-alphabetic? locale-reader-long-month
  1213. (lambda (val object)
  1214. (set-date-month! object val)))
  1215. (list #\d char-numeric? ireader2 (lambda (val object)
  1216. (set-date-day!
  1217. object val)))
  1218. (list #\e char-fail eireader2 (lambda (val object)
  1219. (set-date-day! object val)))
  1220. (list #\h char-alphabetic? locale-reader-abbr-month
  1221. (lambda (val object)
  1222. (set-date-month! object val)))
  1223. (list #\H char-numeric? ireader2 (lambda (val object)
  1224. (set-date-hour! object val)))
  1225. (list #\k char-fail eireader2 (lambda (val object)
  1226. (set-date-hour! object val)))
  1227. (list #\m char-numeric? ireader2 (lambda (val object)
  1228. (set-date-month! object val)))
  1229. (list #\M char-numeric? ireader2 (lambda (val object)
  1230. (set-date-minute!
  1231. object val)))
  1232. (list #\S char-numeric? ireader2 (lambda (val object)
  1233. (set-date-second! object val)))
  1234. (list #\y char-fail eireader2
  1235. (lambda (val object)
  1236. (set-date-year! object (natural-year val))))
  1237. (list #\Y char-numeric? ireader4 (lambda (val object)
  1238. (set-date-year! object val)))
  1239. (list #\z (lambda (c)
  1240. (or (char=? c #\Z)
  1241. (char=? c #\z)
  1242. (char=? c #\+)
  1243. (char=? c #\-)))
  1244. zone-reader (lambda (val object)
  1245. (set-date-zone-offset! object val))))))
  1246. (define (priv:string->date date index format-string str-len port template-string)
  1247. (define (skip-until port skipper)
  1248. (let ((ch (peek-char port)))
  1249. (if (eof-object? ch)
  1250. (time-error 'string->date 'bad-date-format-string template-string)
  1251. (if (not (skipper ch))
  1252. (begin (read-char port) (skip-until port skipper))))))
  1253. (if (< index str-len)
  1254. (let ((current-char (string-ref format-string index)))
  1255. (if (not (char=? current-char #\~))
  1256. (let ((port-char (read-char port)))
  1257. (if (or (eof-object? port-char)
  1258. (not (char=? current-char port-char)))
  1259. (time-error 'string->date
  1260. 'bad-date-format-string template-string))
  1261. (priv:string->date date
  1262. (+ index 1)
  1263. format-string
  1264. str-len
  1265. port
  1266. template-string))
  1267. ;; otherwise, it's an escape, we hope
  1268. (if (> (+ index 1) str-len)
  1269. (time-error 'string->date
  1270. 'bad-date-format-string template-string)
  1271. (let* ((format-char (string-ref format-string (+ index 1)))
  1272. (format-info (assoc format-char read-directives)))
  1273. (if (not format-info)
  1274. (time-error 'string->date
  1275. 'bad-date-format-string template-string)
  1276. (begin
  1277. (let ((skipper (cadr format-info))
  1278. (reader (caddr format-info))
  1279. (actor (cadddr format-info)))
  1280. (skip-until port skipper)
  1281. (let ((val (reader port)))
  1282. (if (eof-object? val)
  1283. (time-error 'string->date
  1284. 'bad-date-format-string
  1285. template-string)
  1286. (if actor (actor val date))))
  1287. (priv:string->date date
  1288. (+ index 2)
  1289. format-string
  1290. str-len
  1291. port
  1292. template-string))))))))))
  1293. (define (string->date input-string template-string)
  1294. (define (date-ok? date)
  1295. (and (date-nanosecond date)
  1296. (date-second date)
  1297. (date-minute date)
  1298. (date-hour date)
  1299. (date-day date)
  1300. (date-month date)
  1301. (date-year date)
  1302. (date-zone-offset date)))
  1303. (let ((newdate (make-date 0 0 0 0 #f #f #f #f)))
  1304. (priv:string->date newdate
  1305. 0
  1306. template-string
  1307. (string-length template-string)
  1308. (open-input-string input-string)
  1309. template-string)
  1310. (if (not (date-zone-offset newdate))
  1311. (begin
  1312. ;; this is necessary to get DST right -- as far as we can
  1313. ;; get it right (think of the double/missing hour in the
  1314. ;; night when we are switching between normal time and DST).
  1315. (set-date-zone-offset! newdate
  1316. (local-tz-offset
  1317. (make-time time-utc 0 0)))
  1318. (set-date-zone-offset! newdate
  1319. (local-tz-offset
  1320. (date->time-utc newdate)))))
  1321. (if (date-ok? newdate)
  1322. newdate
  1323. (time-error
  1324. 'string->date
  1325. 'bad-date-format-string
  1326. (list "Incomplete date read. " newdate template-string)))))
  1327. ;;; srfi-19.scm ends here