time.test 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 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 "strftime %Z doesn't return garbage"
  170. (let ((t (localtime (current-time))))
  171. (set-tm:zone t "ZOW")
  172. (set-tm:isdst t 0)
  173. (string=? (strftime "%Z" t)
  174. "ZOW")))
  175. (pass-if "strftime passes wide characters"
  176. (let ((t (localtime (current-time))))
  177. (string=? (substring (strftime "\u0100%Z" t) 0 1)
  178. "\u0100")))
  179. (with-test-prefix "C99 %z format"
  180. ;; %z here is quite possibly affected by the same tm:gmtoff vs current
  181. ;; zone as %Z above is, so in the following tests we make them the same.
  182. (pass-if "GMT"
  183. (putenv "TZ=GMT+0")
  184. (tzset)
  185. (let ((tm (localtime 86400)))
  186. (string=? "+0000" (strftime "%z" tm))))
  187. ;; prior to guile 1.6.9 and 1.8.1 this test failed, getting "+0500",
  188. ;; because we didn't adjust for tm:gmtoff being west of Greenwich versus
  189. ;; tm_gmtoff being east of Greenwich
  190. (pass-if "EST+5"
  191. (putenv "TZ=EST+5")
  192. (tzset)
  193. (let ((tm (localtime 86400)))
  194. (string=? "-0500" (strftime "%z" tm))))))
  195. ;;;
  196. ;;; strptime
  197. ;;;
  198. (with-test-prefix "strptime"
  199. (pass-if "in another thread after error"
  200. (or (defined? 'strptime) (throw 'unsupported))
  201. (or (provided? 'threads) (throw 'unsupported))
  202. (alarm 5)
  203. (false-if-exception
  204. (strptime "%a" "nosuchday"))
  205. (join-thread (begin-thread (strptime "%d" "1")))
  206. (alarm 0)
  207. #t)
  208. (with-test-prefix "GNU %s format"
  209. ;; "%s" to parse a count of seconds since 1970 is a GNU extension
  210. (define have-strptime-%s
  211. (false-if-exception (strptime "%s" "0")))
  212. (pass-if "gmtoff on GMT"
  213. (or have-strptime-%s (throw 'unsupported))
  214. (putenv "TZ=GMT+0")
  215. (tzset)
  216. (let ((tm (car (strptime "%s" "86400"))))
  217. (eqv? 0 (tm:gmtoff tm))))
  218. ;; prior to guile 1.6.9 and 1.8.1 we didn't pass tm_gmtoff back from
  219. ;; strptime
  220. (pass-if "gmtoff on EST+5"
  221. (or have-strptime-%s (throw 'unsupported))
  222. (putenv "TZ=EST+5")
  223. (tzset)
  224. (let ((tm (car (strptime "%s" "86400"))))
  225. (eqv? (* 5 3600) (tm:gmtoff tm))))))