srfi-19.scm 56 KB

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