srfi-19.scm 55 KB

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