srfi-19.scm 56 KB

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