srfi-19.scm 56 KB

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