time.test 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. ;;;; time.test --- test suite for Guile's time functions -*- scheme -*-
  2. ;;;; Jim Blandy <jimb@red-bean.com> --- June 1999, 2004
  3. ;;;;
  4. ;;;; Copyright (C) 1999,2004,2006,2007,2008,2019,2021 Free Software Foundation, Inc.
  5. ;;;;
  6. ;;;; This library is free software; you can redistribute it and/or
  7. ;;;; modify it under the terms of the GNU Lesser General Public
  8. ;;;; License as published by the Free Software Foundation; either
  9. ;;;; version 3 of the License, or (at your option) any later version.
  10. ;;;;
  11. ;;;; This library is distributed in the hope that it will be useful,
  12. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. ;;;; Lesser General Public License for more details.
  15. ;;;;
  16. ;;;; You should have received a copy of the GNU Lesser General Public
  17. ;;;; License along with this library; if not, write to the Free Software
  18. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. (define-module (test-suite test-time)
  20. #:use-module (test-suite lib)
  21. #:use-module (ice-9 threads))
  22. ;;;
  23. ;;; gmtime
  24. ;;;
  25. (with-test-prefix "gmtime"
  26. (for-each (lambda (t)
  27. (pass-if (list "in another thread after error" t)
  28. (or (provided? 'threads) (throw 'unsupported))
  29. (alarm 5)
  30. (false-if-exception (gmtime t))
  31. (join-thread (begin-thread (catch #t
  32. (lambda () (gmtime t))
  33. (lambda args #f))))
  34. (alarm 0)
  35. #t))
  36. ;; time values that might provoke an error from libc
  37. ;; on 32-bit glibc all values (which fit) are fine
  38. ;; on 64-bit glibc apparently 2^63 can overflow a 32-bit tm_year
  39. (list (1- (ash 1 31)) (1- (ash 1 63))
  40. -1 (- (ash 1 31)) (- (ash 1 63)))))
  41. ;;;
  42. ;;; internal-time-units-per-second
  43. ;;;
  44. (with-test-prefix "internal-time-units-per-second"
  45. ;; Check that sleep 1 gives about internal-time-units-per-second worth of
  46. ;; elapsed time from times:clock. This mainly ensures
  47. ;; internal-time-units-per-second correctly indicates CLK_TCK units.
  48. ;;
  49. (pass-if "versus times and sleep"
  50. (or (defined? 'times) (throw 'unsupported))
  51. (let ((old (times)))
  52. (sleep 1)
  53. (let* ((new (times))
  54. (elapsed (- (tms:clock new) (tms:clock old))))
  55. (<= (* 0.5 internal-time-units-per-second)
  56. elapsed
  57. (* 2 internal-time-units-per-second))))))
  58. ;;;
  59. ;;; localtime
  60. ;;;
  61. (with-test-prefix "localtime"
  62. ;; gmtoff is calculated with some explicit code, try to exercise that
  63. ;; here, looking at cases where the localtime and gmtime are within the same
  64. ;; day, or crossing midnight, or crossing new year
  65. (pass-if "gmtoff of EST+5 at GMT 10:00am on 10 Jan 2000"
  66. (let ((tm (gmtime 0)))
  67. (set-tm:hour tm 10)
  68. (set-tm:mday tm 10)
  69. (set-tm:mon tm 0)
  70. (set-tm:year tm 100)
  71. (let* ((t (car (mktime tm "GMT")))
  72. (tm (localtime t "EST+5")))
  73. (eqv? (* 5 3600) (tm:gmtoff tm)))))
  74. ;; crossing forward over day boundary
  75. (pass-if "gmtoff of EST+5 at GMT 3am on 10 Jan 2000"
  76. (let ((tm (gmtime 0)))
  77. (set-tm:hour tm 3)
  78. (set-tm:mday tm 10)
  79. (set-tm:mon tm 0)
  80. (set-tm:year tm 100)
  81. (let* ((t (car (mktime tm "GMT")))
  82. (tm (localtime t "EST+5")))
  83. (eqv? (* 5 3600) (tm:gmtoff tm)))))
  84. ;; crossing backward over day boundary
  85. (pass-if "gmtoff of AST-10 at GMT 10pm on 10 Jan 2000"
  86. (let ((tm (gmtime 0)))
  87. (set-tm:hour tm 22)
  88. (set-tm:mday tm 10)
  89. (set-tm:mon tm 0)
  90. (set-tm:year tm 100)
  91. (let* ((t (car (mktime tm "GMT")))
  92. (tm (localtime t "AST-10")))
  93. (eqv? (* -10 3600) (tm:gmtoff tm)))))
  94. ;; crossing forward over year boundary
  95. (pass-if "gmtoff of EST+5 at GMT 3am on 1 Jan 2000"
  96. (let ((tm (gmtime 0)))
  97. (set-tm:hour tm 3)
  98. (set-tm:mday tm 1)
  99. (set-tm:mon tm 0)
  100. (set-tm:year tm 100)
  101. (let* ((t (car (mktime tm "GMT")))
  102. (tm (localtime t "EST+5")))
  103. (eqv? (* 5 3600) (tm:gmtoff tm)))))
  104. ;; crossing backward over day boundary
  105. (pass-if "gmtoff of AST-10 at GMT 10pm on 31 Dec 2000"
  106. (let ((tm (gmtime 0)))
  107. (set-tm:hour tm 22)
  108. (set-tm:mday tm 31)
  109. (set-tm:mon tm 11)
  110. (set-tm:year tm 100)
  111. (let* ((t (car (mktime tm "GMT")))
  112. (tm (localtime t "AST-10")))
  113. (eqv? (* -10 3600) (tm:gmtoff tm))))))
  114. ;;;
  115. ;;; mktime
  116. ;;;
  117. (with-test-prefix "mktime"
  118. ;; gmtoff is calculated with some explicit code, try to exercise that
  119. ;; here, looking at cases where the mktime and gmtime are within the same
  120. ;; day, or crossing midnight, or crossing new year
  121. (pass-if "gmtoff of EST+5 at 10:00am on 10 Jan 2000"
  122. (let ((tm (gmtime 0)))
  123. (set-tm:hour tm 10)
  124. (set-tm:mday tm 10)
  125. (set-tm:mon tm 0)
  126. (set-tm:year tm 100)
  127. (let ((tm (cdr (mktime tm "EST+5"))))
  128. (eqv? (* 5 3600) (tm:gmtoff tm)))))
  129. ;; crossing forward over day boundary
  130. (pass-if "gmtoff of EST+5 at 10:00pm on 10 Jan 2000"
  131. (let ((tm (gmtime 0)))
  132. (set-tm:hour tm 22)
  133. (set-tm:mday tm 10)
  134. (set-tm:mon tm 0)
  135. (set-tm:year tm 100)
  136. (let ((tm (cdr (mktime tm "EST+5"))))
  137. (eqv? (* 5 3600) (tm:gmtoff tm)))))
  138. ;; crossing backward over day boundary
  139. (pass-if "gmtoff of AST-10 at 3:00am on 10 Jan 2000"
  140. (let ((tm (gmtime 0)))
  141. (set-tm:hour tm 3)
  142. (set-tm:mday tm 10)
  143. (set-tm:mon tm 0)
  144. (set-tm:year tm 100)
  145. (let ((tm (cdr (mktime tm "AST-10"))))
  146. (eqv? (* -10 3600) (tm:gmtoff tm)))))
  147. ;; crossing forward over year boundary
  148. (pass-if "gmtoff of EST+5 at 10:00pm on 31 Dec 2000"
  149. (let ((tm (gmtime 0)))
  150. (set-tm:hour tm 22)
  151. (set-tm:mday tm 31)
  152. (set-tm:mon tm 11)
  153. (set-tm:year tm 100)
  154. (let ((tm (cdr (mktime tm "EST+5"))))
  155. (eqv? (* 5 3600) (tm:gmtoff tm)))))
  156. ;; crossing backward over day boundary
  157. (pass-if "gmtoff of AST-10 at 3:00am on 1 Jan 2000"
  158. (let ((tm (gmtime 0)))
  159. (set-tm:hour tm 3)
  160. (set-tm:mday tm 1)
  161. (set-tm:mon tm 0)
  162. (set-tm:year tm 100)
  163. (let ((tm (cdr (mktime tm "AST-10"))))
  164. (eqv? (* -10 3600) (tm:gmtoff tm))))))
  165. ;;;
  166. ;;; strftime
  167. ;;;
  168. (with-test-prefix "strftime"
  169. (pass-if-equal "strftime %Z doesn't return garbage"
  170. "ZOW"
  171. (let ((t (localtime (current-time))))
  172. (set-tm:zone t "ZOW")
  173. (set-tm:isdst t 0)
  174. (strftime "%Z" t)))
  175. (pass-if-equal "strftime passes wide characters"
  176. "\u0100"
  177. (with-locale "en_US.utf8"
  178. (let ((t (localtime (current-time))))
  179. (substring (strftime "\u0100%Z" t) 0 1))))
  180. (with-test-prefix "C99 %z format"
  181. ;; %z here is quite possibly affected by the same tm:gmtoff vs current
  182. ;; zone as %Z above is, so in the following tests we make them the same.
  183. (pass-if-equal "GMT"
  184. "+0000"
  185. (begin
  186. (putenv "TZ=GMT+0")
  187. (tzset)
  188. (let ((tm (localtime 86400)))
  189. (strftime "%z" tm))))
  190. ;; prior to guile 1.6.9 and 1.8.1 this test failed, getting "+0500",
  191. ;; because we didn't adjust for tm:gmtoff being west of Greenwich versus
  192. ;; tm_gmtoff being east of Greenwich
  193. (pass-if-equal "EST+5"
  194. "-0500"
  195. (begin
  196. (putenv "TZ=EST+5")
  197. (tzset)
  198. (let ((tm (localtime 86400)))
  199. (strftime "%z" tm))))
  200. (pass-if-equal "strftime fr_FR.utf8"
  201. " 1 février 1970"
  202. (with-locale "fr_FR.utf8"
  203. (strftime "%e %B %Y" (gmtime (* 31 24 3600)))))
  204. (pass-if-equal "strftime fr_FR.iso88591" ;<https://bugs.gnu.org/35920>
  205. " 1 février 1970"
  206. (with-locale "fr_FR.iso88591"
  207. (strftime "%e %B %Y" (gmtime (* 31 24 3600)))))))
  208. ;;;
  209. ;;; strptime
  210. ;;;
  211. (with-test-prefix "strptime"
  212. (pass-if "in another thread after error"
  213. (or (defined? 'strptime) (throw 'unsupported))
  214. (or (provided? 'threads) (throw 'unsupported))
  215. (alarm 5)
  216. (false-if-exception
  217. (strptime "%a" "nosuchday"))
  218. (join-thread (begin-thread (strptime "%d" "1")))
  219. (alarm 0)
  220. #t)
  221. (with-test-prefix "GNU %s format"
  222. ;; "%s" to parse a count of seconds since 1970 is a GNU extension
  223. (define have-strptime-%s
  224. (false-if-exception (strptime "%s" "0")))
  225. (pass-if "gmtoff on GMT"
  226. (or have-strptime-%s (throw 'unsupported))
  227. (putenv "TZ=GMT+0")
  228. (tzset)
  229. (let ((tm (car (strptime "%s" "86400"))))
  230. (eqv? 0 (tm:gmtoff tm))))
  231. (pass-if-equal "strftime fr_FR.utf8"
  232. '(1 2 1999)
  233. (with-locale "fr_FR.utf8"
  234. (let ((tm (car (strptime "%e %B %Y" "1 février 1999"))))
  235. (list (tm:mday tm)
  236. (+ 1 (tm:mon tm))
  237. (+ 1900 (tm:year tm))))))
  238. (pass-if-equal "strftime fr_FR.iso88591" ;<https://bugs.gnu.org/35920>
  239. '(1 2 1999)
  240. (with-locale "fr_FR.iso88591"
  241. (let ((tm (car (strptime "%e %B %Y" "1 février 1999"))))
  242. (list (tm:mday tm)
  243. (+ 1 (tm:mon tm))
  244. (+ 1900 (tm:year tm))))))
  245. ;; prior to guile 1.6.9 and 1.8.1 we didn't pass tm_gmtoff back from
  246. ;; strptime
  247. (pass-if "gmtoff on EST+5"
  248. (or have-strptime-%s (throw 'unsupported))
  249. (putenv "TZ=EST+5")
  250. (tzset)
  251. (let ((tm (car (strptime "%s" "86400"))))
  252. (eqv? (* 5 3600) (tm:gmtoff tm))))))