srfi-19.scm 56 KB

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