srfi-19.scm 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471
  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. (define (current-date . tz-offset)
  659. (let ((time (current-time time-utc)))
  660. (time-utc->date
  661. time
  662. (if (null? tz-offset)
  663. (local-tz-offset time)
  664. (car tz-offset)))))
  665. ;; given a 'two digit' number, find the year within 50 years +/-
  666. (define (natural-year n)
  667. (let* ((current-year (date-year (current-date)))
  668. (current-century (* (quotient current-year 100) 100)))
  669. (cond
  670. ((>= n 100) n)
  671. ((< n 0) n)
  672. ((<= (- (+ current-century n) current-year) 50) (+ current-century n))
  673. (else (+ (- current-century 100) n)))))
  674. (define (date->julian-day date)
  675. (let ((nanosecond (date-nanosecond date))
  676. (second (date-second date))
  677. (minute (date-minute date))
  678. (hour (date-hour date))
  679. (day (date-day date))
  680. (month (date-month date))
  681. (year (date-year date))
  682. (offset (date-zone-offset date)))
  683. (+ (encode-julian-day-number day month year)
  684. (- 1/2)
  685. (+ (/ (+ (- offset)
  686. (* hour 60 60)
  687. (* minute 60)
  688. second
  689. (/ nanosecond nano))
  690. sid)))))
  691. (define (date->modified-julian-day date)
  692. (- (date->julian-day date)
  693. 4800001/2))
  694. (define (time-utc->julian-day time)
  695. (if (not (eq? (time-type time) time-utc))
  696. (time-error 'time-utc->julian-day 'incompatible-time-types time))
  697. (+ (/ (+ (time-second time) (/ (time-nanosecond time) nano))
  698. sid)
  699. utc-epoch-in-jd))
  700. (define (time-utc->modified-julian-day time)
  701. (- (time-utc->julian-day time)
  702. 4800001/2))
  703. (define (time-tai->julian-day time)
  704. (if (not (eq? (time-type time) time-tai))
  705. (time-error 'time-tai->julian-day 'incompatible-time-types time))
  706. (+ (/ (+ (tai->utc (time-second time))
  707. (/ (time-nanosecond time) nano))
  708. sid)
  709. utc-epoch-in-jd))
  710. (define (time-tai->modified-julian-day time)
  711. (- (time-tai->julian-day time)
  712. 4800001/2))
  713. ;; this is the same as time-tai->julian-day
  714. (define (time-monotonic->julian-day time)
  715. (if (not (eq? (time-type time) time-monotonic))
  716. (time-error 'time-monotonic->julian-day 'incompatible-time-types time))
  717. (+ (/ (+ (tai->utc (time-second time))
  718. (/ (time-nanosecond time) nano))
  719. sid)
  720. utc-epoch-in-jd))
  721. (define (time-monotonic->modified-julian-day time)
  722. (- (time-monotonic->julian-day time)
  723. 4800001/2))
  724. (define (julian-day->time-utc jdn)
  725. (let ((secs (* sid (- jdn utc-epoch-in-jd))))
  726. (receive (seconds parts)
  727. (split-real secs)
  728. (make-time time-utc
  729. (* parts nano)
  730. seconds))))
  731. (define (julian-day->time-tai jdn)
  732. (time-utc->time-tai! (julian-day->time-utc jdn)))
  733. (define (julian-day->time-monotonic jdn)
  734. (time-utc->time-monotonic! (julian-day->time-utc jdn)))
  735. (define (julian-day->date jdn . tz-offset)
  736. (let* ((time (julian-day->time-utc jdn))
  737. (offset (if (null? tz-offset)
  738. (local-tz-offset time)
  739. (car tz-offset))))
  740. (time-utc->date time offset)))
  741. (define (modified-julian-day->date jdn . tz-offset)
  742. (apply julian-day->date (+ jdn 4800001/2)
  743. tz-offset))
  744. (define (modified-julian-day->time-utc jdn)
  745. (julian-day->time-utc (+ jdn 4800001/2)))
  746. (define (modified-julian-day->time-tai jdn)
  747. (julian-day->time-tai (+ jdn 4800001/2)))
  748. (define (modified-julian-day->time-monotonic jdn)
  749. (julian-day->time-monotonic (+ jdn 4800001/2)))
  750. (define (current-julian-day)
  751. (time-utc->julian-day (current-time time-utc)))
  752. (define (current-modified-julian-day)
  753. (time-utc->modified-julian-day (current-time time-utc)))
  754. ;; returns a string rep. of number N, of minimum LENGTH, padded with
  755. ;; character PAD-WITH. If PAD-WITH is #f, no padding is done, and it's
  756. ;; as if number->string was used. if string is longer than or equal
  757. ;; in length to LENGTH, it's as if number->string was used.
  758. (define (padding n pad-with length)
  759. (let* ((str (number->string n))
  760. (str-len (string-length str)))
  761. (if (or (>= str-len length)
  762. (not pad-with))
  763. str
  764. (string-append (make-string (- length str-len) pad-with) str))))
  765. (define (last-n-digits i n)
  766. (abs (remainder i (expt 10 n))))
  767. (define (locale-abbr-weekday n) (locale-day-short (+ 1 n)))
  768. (define (locale-long-weekday n) (locale-day (+ 1 n)))
  769. (define locale-abbr-month locale-month-short)
  770. (define locale-long-month locale-month)
  771. (define (date-reverse-lookup needle haystack-ref haystack-len
  772. same?)
  773. ;; Lookup NEEDLE (a string) using HAYSTACK-REF (a one argument procedure
  774. ;; that returns a string corresponding to the given index) by passing it
  775. ;; indices lower than HAYSTACK-LEN.
  776. (let loop ((index 1))
  777. (cond ((> index haystack-len) #f)
  778. ((same? needle (haystack-ref index))
  779. index)
  780. (else (loop (+ index 1))))))
  781. (define (locale-abbr-weekday->index string)
  782. (date-reverse-lookup string locale-day-short 7 string=?))
  783. (define (locale-long-weekday->index string)
  784. (date-reverse-lookup string locale-day 7 string=?))
  785. (define (locale-abbr-month->index string)
  786. (date-reverse-lookup string locale-abbr-month 12 string=?))
  787. (define (locale-long-month->index string)
  788. (date-reverse-lookup string locale-long-month 12 string=?))
  789. ;; FIXME: mkoeppe: Put a symbolic time zone in the date structs.
  790. ;; Print it here instead of the numerical offset if available.
  791. (define (locale-print-time-zone date port)
  792. (tz-printer (date-zone-offset date) port))
  793. (define (locale-am-string/pm hr)
  794. (if (> hr 11) (locale-pm-string) (locale-am-string)))
  795. (define (tz-printer offset port)
  796. (cond
  797. ((= offset 0) (display "Z" port))
  798. ((negative? offset) (display "-" port))
  799. (else (display "+" port)))
  800. (if (not (= offset 0))
  801. (let ((hours (abs (quotient offset (* 60 60))))
  802. (minutes (abs (quotient (remainder offset (* 60 60)) 60))))
  803. (display (padding hours #\0 2) port)
  804. (display (padding minutes #\0 2) port))))
  805. ;; A table of output formatting directives.
  806. ;; the first time is the format char.
  807. ;; the second is a procedure that takes the date, a padding character
  808. ;; (which might be #f), and the output port.
  809. ;;
  810. (define directives
  811. (list
  812. (cons #\~ (lambda (date pad-with port)
  813. (display #\~ port)))
  814. (cons #\a (lambda (date pad-with port)
  815. (display (locale-abbr-weekday (date-week-day date))
  816. port)))
  817. (cons #\A (lambda (date pad-with port)
  818. (display (locale-long-weekday (date-week-day date))
  819. port)))
  820. (cons #\b (lambda (date pad-with port)
  821. (display (locale-abbr-month (date-month date))
  822. port)))
  823. (cons #\B (lambda (date pad-with port)
  824. (display (locale-long-month (date-month date))
  825. port)))
  826. (cons #\c (lambda (date pad-with port)
  827. (display (date->string date locale-date-time-format) port)))
  828. (cons #\d (lambda (date pad-with port)
  829. (display (padding (date-day date)
  830. #\0 2)
  831. port)))
  832. (cons #\D (lambda (date pad-with port)
  833. (display (date->string date "~m/~d/~y") port)))
  834. (cons #\e (lambda (date pad-with port)
  835. (display (padding (date-day date)
  836. #\Space 2)
  837. port)))
  838. (cons #\f (lambda (date pad-with port)
  839. (receive (s ns) (floor/ (+ (* (date-second date) nano)
  840. (date-nanosecond date))
  841. nano)
  842. (display (number->string s) port)
  843. (display (locale-decimal-point) port)
  844. (let ((str (padding ns #\0 9)))
  845. (display (substring str 0 1) port)
  846. (display (string-trim-right str #\0 1) port)))))
  847. (cons #\h (lambda (date pad-with port)
  848. (display (date->string date "~b") port)))
  849. (cons #\H (lambda (date pad-with port)
  850. (display (padding (date-hour date)
  851. pad-with 2)
  852. port)))
  853. (cons #\I (lambda (date pad-with port)
  854. (let ((hr (date-hour date)))
  855. (if (> hr 12)
  856. (display (padding (- hr 12)
  857. pad-with 2)
  858. port)
  859. (display (padding hr
  860. pad-with 2)
  861. port)))))
  862. (cons #\j (lambda (date pad-with port)
  863. (display (padding (date-year-day date)
  864. pad-with 3)
  865. port)))
  866. (cons #\k (lambda (date pad-with port)
  867. (display (padding (date-hour date)
  868. #\Space 2)
  869. port)))
  870. (cons #\l (lambda (date pad-with port)
  871. (let ((hr (if (> (date-hour date) 12)
  872. (- (date-hour date) 12) (date-hour date))))
  873. (display (padding hr #\Space 2)
  874. port))))
  875. (cons #\m (lambda (date pad-with port)
  876. (display (padding (date-month date)
  877. pad-with 2)
  878. port)))
  879. (cons #\M (lambda (date pad-with port)
  880. (display (padding (date-minute date)
  881. pad-with 2)
  882. port)))
  883. (cons #\n (lambda (date pad-with port)
  884. (newline port)))
  885. (cons #\N (lambda (date pad-with port)
  886. (display (padding (date-nanosecond date)
  887. pad-with 9)
  888. port)))
  889. (cons #\p (lambda (date pad-with port)
  890. (display (locale-am-string/pm (date-hour date)) port)))
  891. (cons #\r (lambda (date pad-with port)
  892. (display (date->string date "~I:~M:~S ~p") port)))
  893. (cons #\s (lambda (date pad-with port)
  894. (display (time-second (date->time-utc date)) port)))
  895. (cons #\S (lambda (date pad-with port)
  896. (if (> (date-nanosecond date)
  897. nano)
  898. (display (padding (+ (date-second date) 1)
  899. pad-with 2)
  900. port)
  901. (display (padding (date-second date)
  902. pad-with 2)
  903. port))))
  904. (cons #\t (lambda (date pad-with port)
  905. (display #\Tab port)))
  906. (cons #\T (lambda (date pad-with port)
  907. (display (date->string date "~H:~M:~S") port)))
  908. (cons #\U (lambda (date pad-with port)
  909. (if (> (days-before-first-week date 0) 0)
  910. (display (padding (+ (date-week-number date 0) 1)
  911. #\0 2) port)
  912. (display (padding (date-week-number date 0)
  913. #\0 2) port))))
  914. (cons #\V (lambda (date pad-with port)
  915. (display (padding (date-week-number date 1)
  916. #\0 2) port)))
  917. (cons #\w (lambda (date pad-with port)
  918. (display (date-week-day date) port)))
  919. (cons #\x (lambda (date pad-with port)
  920. (display (date->string date locale-short-date-format) port)))
  921. (cons #\X (lambda (date pad-with port)
  922. (display (date->string date locale-time-format) port)))
  923. (cons #\W (lambda (date pad-with port)
  924. (if (> (days-before-first-week date 1) 0)
  925. (display (padding (+ (date-week-number date 1) 1)
  926. #\0 2) port)
  927. (display (padding (date-week-number date 1)
  928. #\0 2) port))))
  929. (cons #\y (lambda (date pad-with port)
  930. (display (padding (last-n-digits
  931. (date-year date) 2)
  932. pad-with
  933. 2)
  934. port)))
  935. (cons #\Y (lambda (date pad-with port)
  936. (let* ((yy (date-year date))
  937. (y (if (negative? yy) (+ yy 1) yy)))
  938. (unless (<= 0 y 9999)
  939. (display (if (negative? y) #\- #\+) port))
  940. (display (padding (abs y) pad-with 4) port))))
  941. (cons #\z (lambda (date pad-with port)
  942. (tz-printer (date-zone-offset date) port)))
  943. (cons #\Z (lambda (date pad-with port)
  944. (locale-print-time-zone date port)))
  945. (cons #\1 (lambda (date pad-with port)
  946. (display (date->string date "~Y-~m-~d") port)))
  947. (cons #\2 (lambda (date pad-with port)
  948. (display (date->string date "~H:~M:~S~z") port)))
  949. (cons #\3 (lambda (date pad-with port)
  950. (display (date->string date "~H:~M:~S") port)))
  951. (cons #\4 (lambda (date pad-with port)
  952. (display (date->string date "~Y-~m-~dT~H:~M:~S~z") port)))
  953. (cons #\5 (lambda (date pad-with port)
  954. (display (date->string date "~Y-~m-~dT~H:~M:~S") port)))))
  955. (define (get-formatter char)
  956. (let ((associated (assoc char directives)))
  957. (if associated (cdr associated) #f)))
  958. (define (date-printer date index format-string str-len port)
  959. (if (< index str-len)
  960. (let ((current-char (string-ref format-string index)))
  961. (if (not (char=? current-char #\~))
  962. (begin
  963. (display current-char port)
  964. (date-printer date (+ index 1) format-string str-len port))
  965. (if (= (+ index 1) str-len) ; bad format string.
  966. (time-error 'date-printer 'bad-date-format-string
  967. format-string)
  968. (let ((pad-char? (string-ref format-string (+ index 1))))
  969. (cond
  970. ((char=? pad-char? #\-)
  971. (if (= (+ index 2) str-len) ; bad format string.
  972. (time-error 'date-printer
  973. 'bad-date-format-string
  974. format-string)
  975. (let ((formatter (get-formatter
  976. (string-ref format-string
  977. (+ index 2)))))
  978. (if (not formatter)
  979. (time-error 'date-printer
  980. 'bad-date-format-string
  981. format-string)
  982. (begin
  983. (formatter date #f port)
  984. (date-printer date
  985. (+ index 3)
  986. format-string
  987. str-len
  988. port))))))
  989. ((char=? pad-char? #\_)
  990. (if (= (+ index 2) str-len) ; bad format string.
  991. (time-error 'date-printer
  992. 'bad-date-format-string
  993. format-string)
  994. (let ((formatter (get-formatter
  995. (string-ref format-string
  996. (+ index 2)))))
  997. (if (not formatter)
  998. (time-error 'date-printer
  999. 'bad-date-format-string
  1000. format-string)
  1001. (begin
  1002. (formatter date #\Space port)
  1003. (date-printer date
  1004. (+ index 3)
  1005. format-string
  1006. str-len
  1007. port))))))
  1008. (else
  1009. (let ((formatter (get-formatter
  1010. (string-ref format-string
  1011. (+ index 1)))))
  1012. (if (not formatter)
  1013. (time-error 'date-printer
  1014. 'bad-date-format-string
  1015. format-string)
  1016. (begin
  1017. (formatter date #\0 port)
  1018. (date-printer date
  1019. (+ index 2)
  1020. format-string
  1021. str-len
  1022. port))))))))))))
  1023. (define (date->string date . format-string)
  1024. (let ((str-port (open-output-string))
  1025. (fmt-str (if (null? format-string) "~c" (car format-string))))
  1026. (date-printer date 0 fmt-str (string-length fmt-str) str-port)
  1027. (get-output-string str-port)))
  1028. (define (char->int ch)
  1029. (case ch
  1030. ((#\0) 0)
  1031. ((#\1) 1)
  1032. ((#\2) 2)
  1033. ((#\3) 3)
  1034. ((#\4) 4)
  1035. ((#\5) 5)
  1036. ((#\6) 6)
  1037. ((#\7) 7)
  1038. ((#\8) 8)
  1039. ((#\9) 9)
  1040. (else (time-error 'char->int 'bad-date-template-string
  1041. (list "Non-integer character" ch)))))
  1042. ;; read an integer upto n characters long on port; upto -> #f is any length
  1043. (define (integer-reader upto port)
  1044. (let loop ((accum 0) (nchars 0))
  1045. (let ((ch (peek-char port)))
  1046. (if (or (eof-object? ch)
  1047. (not (char-numeric? ch))
  1048. (and upto (>= nchars upto)))
  1049. accum
  1050. (loop (+ (* accum 10) (char->int (read-char port)))
  1051. (+ nchars 1))))))
  1052. (define (make-integer-reader upto)
  1053. (lambda (port)
  1054. (integer-reader upto port)))
  1055. ;; read an fractional integer upto n characters long on port; upto -> #f if any length
  1056. ;;
  1057. ;; The return value is normalized to upto decimal places. For example, if upto is 9 and
  1058. ;; the string read is "123", the return value is 123000000.
  1059. (define (fractional-integer-reader upto port)
  1060. (define (accum-int port accum nchars)
  1061. (let ((ch (peek-char port)))
  1062. (if (or (eof-object? ch)
  1063. (not (char-numeric? ch))
  1064. (and upto (>= nchars upto)))
  1065. (* accum (expt 10 (- upto nchars)))
  1066. (accum-int port (+ (* accum 10) (char->int (read-char port))) (+ nchars 1)))))
  1067. (accum-int port 0 0))
  1068. (define (make-fractional-integer-reader upto)
  1069. (lambda (port)
  1070. (fractional-integer-reader upto port)))
  1071. ;; read *exactly* n characters and convert to integer; could be padded
  1072. (define (integer-reader-exact n port)
  1073. (let ((padding-ok #t))
  1074. (define (accum-int port accum nchars)
  1075. (let ((ch (peek-char port)))
  1076. (cond
  1077. ((>= nchars n) accum)
  1078. ((eof-object? ch)
  1079. (time-error 'string->date 'bad-date-template-string
  1080. "Premature ending to integer read."))
  1081. ((char-numeric? ch)
  1082. (set! padding-ok #f)
  1083. (accum-int port
  1084. (+ (* accum 10) (char->int (read-char port)))
  1085. (+ nchars 1)))
  1086. (padding-ok
  1087. (read-char port) ; consume padding
  1088. (accum-int port accum (+ nchars 1)))
  1089. (else ; padding where it shouldn't be
  1090. (time-error 'string->date 'bad-date-template-string
  1091. "Non-numeric characters in integer read.")))))
  1092. (accum-int port 0 0)))
  1093. (define (make-integer-exact-reader n)
  1094. (lambda (port)
  1095. (integer-reader-exact n port)))
  1096. (define (zone-reader port)
  1097. (let ((offset 0)
  1098. (positive? #f))
  1099. (let ((ch (read-char port)))
  1100. (if (eof-object? ch)
  1101. (time-error 'string->date 'bad-date-template-string
  1102. (list "Invalid time zone +/-" ch)))
  1103. (if (or (char=? ch #\Z) (char=? ch #\z))
  1104. 0
  1105. (begin
  1106. (cond
  1107. ((char=? ch #\+) (set! positive? #t))
  1108. ((char=? ch #\-) (set! positive? #f))
  1109. (else
  1110. (time-error 'string->date 'bad-date-template-string
  1111. (list "Invalid time zone +/-" ch))))
  1112. (let ((ch (read-char port)))
  1113. (if (eof-object? ch)
  1114. (time-error 'string->date 'bad-date-template-string
  1115. (list "Invalid time zone number" ch)))
  1116. (set! offset (* (char->int ch)
  1117. 10 60 60)))
  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 number" ch)))
  1122. (set! offset (+ offset (* (char->int ch)
  1123. 60 60))))
  1124. (let ((ch (read-char port)))
  1125. (if (eof-object? ch)
  1126. (time-error 'string->date 'bad-date-template-string
  1127. (list "Invalid time zone number" ch)))
  1128. (set! offset (+ offset (* (char->int ch)
  1129. 10 60))))
  1130. (let ((ch (read-char port)))
  1131. (if (eof-object? ch)
  1132. (time-error 'string->date 'bad-date-template-string
  1133. (list "Invalid time zone number" ch)))
  1134. (set! offset (+ offset (* (char->int ch)
  1135. 60))))
  1136. (if positive? offset (- offset)))))))
  1137. ;; looking at a char, read the char string, run thru indexer, return index
  1138. (define (locale-reader port indexer)
  1139. (define (read-char-string result)
  1140. (let ((ch (peek-char port)))
  1141. (if (char-alphabetic? ch)
  1142. (read-char-string (cons (read-char port) result))
  1143. (list->string (reverse! result)))))
  1144. (let* ((str (read-char-string '()))
  1145. (index (indexer str)))
  1146. (if index index (time-error 'string->date
  1147. 'bad-date-template-string
  1148. (list "Invalid string for " indexer)))))
  1149. (define (make-locale-reader indexer)
  1150. (lambda (port)
  1151. (locale-reader port indexer)))
  1152. (define (make-char-id-reader char)
  1153. (lambda (port)
  1154. (if (char=? char (read-char port))
  1155. char
  1156. (time-error 'string->date
  1157. 'bad-date-template-string
  1158. "Invalid character match."))))
  1159. ;; A List of formatted read directives.
  1160. ;; Each entry is a list.
  1161. ;; 1. the character directive;
  1162. ;; a procedure, which takes a character as input & returns
  1163. ;; 2. #t as soon as a character on the input port is acceptable
  1164. ;; for input,
  1165. ;; 3. a port reader procedure that knows how to read the current port
  1166. ;; for a value. Its one parameter is the port.
  1167. ;; 4. an optional action procedure, that takes the value (from 3.) and
  1168. ;; some object (here, always the date) and (probably) side-effects it.
  1169. ;; If no action is required, as with ~A, this element may be #f.
  1170. (define read-directives
  1171. (let ((ireader4 (make-integer-reader 4))
  1172. (ireader2 (make-integer-reader 2))
  1173. (fireader9 (make-fractional-integer-reader 9))
  1174. (eireader2 (make-integer-exact-reader 2))
  1175. (locale-reader-abbr-weekday (make-locale-reader
  1176. locale-abbr-weekday->index))
  1177. (locale-reader-long-weekday (make-locale-reader
  1178. locale-long-weekday->index))
  1179. (locale-reader-abbr-month (make-locale-reader
  1180. locale-abbr-month->index))
  1181. (locale-reader-long-month (make-locale-reader
  1182. locale-long-month->index))
  1183. (char-fail (lambda (ch) #t)))
  1184. (list
  1185. (list #\~ char-fail (make-char-id-reader #\~) #f)
  1186. (list #\a char-alphabetic? locale-reader-abbr-weekday #f)
  1187. (list #\A char-alphabetic? locale-reader-long-weekday #f)
  1188. (list #\b char-alphabetic? locale-reader-abbr-month
  1189. (lambda (val object)
  1190. (set-date-month! object val)))
  1191. (list #\B char-alphabetic? locale-reader-long-month
  1192. (lambda (val object)
  1193. (set-date-month! object val)))
  1194. (list #\d char-numeric? ireader2 (lambda (val object)
  1195. (set-date-day!
  1196. object val)))
  1197. (list #\e char-fail eireader2 (lambda (val object)
  1198. (set-date-day! object val)))
  1199. (list #\h char-alphabetic? locale-reader-abbr-month
  1200. (lambda (val object)
  1201. (set-date-month! object val)))
  1202. (list #\H char-numeric? ireader2 (lambda (val object)
  1203. (set-date-hour! object val)))
  1204. (list #\k char-fail eireader2 (lambda (val object)
  1205. (set-date-hour! object val)))
  1206. (list #\m char-numeric? ireader2 (lambda (val object)
  1207. (set-date-month! object val)))
  1208. (list #\M char-numeric? ireader2 (lambda (val object)
  1209. (set-date-minute!
  1210. object val)))
  1211. (list #\N char-numeric? fireader9 (lambda (val object)
  1212. (set-date-nanosecond!
  1213. object val)))
  1214. (list #\S char-numeric? ireader2 (lambda (val object)
  1215. (set-date-second! object val)))
  1216. (list #\y char-fail eireader2
  1217. (lambda (val object)
  1218. (set-date-year! object (natural-year val))))
  1219. ;; XXX FIXME: Support the extended year format used by
  1220. ;; 'date->string' when the year is not in the range 0-9999.
  1221. (list #\Y char-numeric? ireader4 (lambda (val object)
  1222. (set-date-year! object val)))
  1223. (list #\z (lambda (c)
  1224. (or (char=? c #\Z)
  1225. (char=? c #\z)
  1226. (char=? c #\+)
  1227. (char=? c #\-)))
  1228. zone-reader (lambda (val object)
  1229. (set-date-zone-offset! object val))))))
  1230. (define (priv:string->date date index format-string str-len port template-string)
  1231. (define (skip-until port skipper)
  1232. (let ((ch (peek-char port)))
  1233. (if (eof-object? ch)
  1234. (time-error 'string->date 'bad-date-format-string template-string)
  1235. (if (not (skipper ch))
  1236. (begin (read-char port) (skip-until port skipper))))))
  1237. (if (< index str-len)
  1238. (let ((current-char (string-ref format-string index)))
  1239. (if (not (char=? current-char #\~))
  1240. (let ((port-char (read-char port)))
  1241. (if (or (eof-object? port-char)
  1242. (not (char=? current-char port-char)))
  1243. (time-error 'string->date
  1244. 'bad-date-format-string template-string))
  1245. (priv:string->date date
  1246. (+ index 1)
  1247. format-string
  1248. str-len
  1249. port
  1250. template-string))
  1251. ;; otherwise, it's an escape, we hope
  1252. (if (> (+ index 1) str-len)
  1253. (time-error 'string->date
  1254. 'bad-date-format-string template-string)
  1255. (let* ((format-char (string-ref format-string (+ index 1)))
  1256. (format-info (assoc format-char read-directives)))
  1257. (if (not format-info)
  1258. (time-error 'string->date
  1259. 'bad-date-format-string template-string)
  1260. (begin
  1261. (let ((skipper (cadr format-info))
  1262. (reader (caddr format-info))
  1263. (actor (cadddr format-info)))
  1264. (skip-until port skipper)
  1265. (let ((val (reader port)))
  1266. (if (eof-object? val)
  1267. (time-error 'string->date
  1268. 'bad-date-format-string
  1269. template-string)
  1270. (if actor (actor val date))))
  1271. (priv:string->date date
  1272. (+ index 2)
  1273. format-string
  1274. str-len
  1275. port
  1276. template-string))))))))))
  1277. (define (string->date input-string template-string)
  1278. (define (date-ok? date)
  1279. (and (date-nanosecond date)
  1280. (date-second date)
  1281. (date-minute date)
  1282. (date-hour date)
  1283. (date-day date)
  1284. (date-month date)
  1285. (date-year date)
  1286. (date-zone-offset date)))
  1287. (let ((newdate (make-date 0 0 0 0 #f #f #f #f)))
  1288. (priv:string->date newdate
  1289. 0
  1290. template-string
  1291. (string-length template-string)
  1292. (open-input-string input-string)
  1293. template-string)
  1294. (if (not (date-zone-offset newdate))
  1295. (begin
  1296. ;; this is necessary to get DST right -- as far as we can
  1297. ;; get it right (think of the double/missing hour in the
  1298. ;; night when we are switching between normal time and DST).
  1299. (set-date-zone-offset! newdate
  1300. (local-tz-offset
  1301. (make-time time-utc 0 0)))
  1302. (set-date-zone-offset! newdate
  1303. (local-tz-offset
  1304. (date->time-utc newdate)))))
  1305. (if (date-ok? newdate)
  1306. newdate
  1307. (time-error
  1308. 'string->date
  1309. 'bad-date-format-string
  1310. (list "Incomplete date read. " newdate template-string)))))
  1311. ;;; srfi-19.scm ends here