| 1 |
;;;; time.test --- test suite for Guile's time functions -*- scheme -*- |
| 2 |
;;;; Jim Blandy <[email protected]> --- 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 |
|
| 20 |
(define-module (test-suite test-time) |
| 21 |
#:use-module (test-suite lib) |
| 22 |
#:use-module (ice-9 threads)) |
| 23 |
|
| 24 |
;;; |
| 25 |
;;; gmtime |
| 26 |
;;; |
| 27 |
|
| 28 |
(with-test-prefix "gmtime" |
| 29 |
|
| 30 |
(for-each (lambda (t) |
| 31 |
(pass-if (list "in another thread after error" t) |
| 32 |
(or (provided? 'threads) (throw 'unsupported)) |
| 33 |
|
| 34 |
(alarm 5) |
| 35 |
(false-if-exception (gmtime t)) |
| 36 |
(join-thread (begin-thread (catch #t |
| 37 |
(lambda () (gmtime t)) |
| 38 |
(lambda args #f)))) |
| 39 |
(alarm 0) |
| 40 |
#t)) |
| 41 |
|
| 42 |
;; time values that might provoke an error from libc |
| 43 |
;; on 32-bit glibc all values (which fit) are fine |
| 44 |
;; on 64-bit glibc apparently 2^63 can overflow a 32-bit tm_year |
| 45 |
(list (1- (ash 1 31)) (1- (ash 1 63)) |
| 46 |
-1 (- (ash 1 31)) (- (ash 1 63))))) |
| 47 |
|
| 48 |
;;; |
| 49 |
;;; internal-time-units-per-second |
| 50 |
;;; |
| 51 |
|
| 52 |
(with-test-prefix "internal-time-units-per-second" |
| 53 |
|
| 54 |
;; Check that sleep 1 gives about internal-time-units-per-second worth of |
| 55 |
;; elapsed time from times:clock. This mainly ensures |
| 56 |
;; internal-time-units-per-second correctly indicates CLK_TCK units. |
| 57 |
;; |
| 58 |
(pass-if "versus times and sleep" |
| 59 |
(or (defined? 'times) (throw 'unsupported)) |
| 60 |
|
| 61 |
(let ((old (times))) |
| 62 |
(sleep 1) |
| 63 |
(let* ((new (times)) |
| 64 |
(elapsed (- (tms:clock new) (tms:clock old)))) |
| 65 |
(<= (* 0.5 internal-time-units-per-second) |
| 66 |
elapsed |
| 67 |
(* 2 internal-time-units-per-second)))))) |
| 68 |
|
| 69 |
;;; |
| 70 |
;;; localtime |
| 71 |
;;; |
| 72 |
|
| 73 |
(with-test-prefix "localtime" |
| 74 |
|
| 75 |
;; gmtoff is calculated with some explicit code, try to exercise that |
| 76 |
;; here, looking at cases where the localtime and gmtime are within the same |
| 77 |
;; day, or crossing midnight, or crossing new year |
| 78 |
|
| 79 |
(pass-if "gmtoff of EST+5 at GMT 10:00am on 10 Jan 2000" |
| 80 |
(let ((tm (gmtime 0))) |
| 81 |
(set-tm:hour tm 10) |
| 82 |
(set-tm:mday tm 10) |
| 83 |
(set-tm:mon tm 0) |
| 84 |
(set-tm:year tm 100) |
| 85 |
(let* ((t (car (mktime tm "GMT"))) |
| 86 |
(tm (localtime t "EST+5"))) |
| 87 |
(eqv? (* 5 3600) (tm:gmtoff tm))))) |
| 88 |
|
| 89 |
;; crossing forward over day boundary |
| 90 |
(pass-if "gmtoff of EST+5 at GMT 3am on 10 Jan 2000" |
| 91 |
(let ((tm (gmtime 0))) |
| 92 |
(set-tm:hour tm 3) |
| 93 |
(set-tm:mday tm 10) |
| 94 |
(set-tm:mon tm 0) |
| 95 |
(set-tm:year tm 100) |
| 96 |
(let* ((t (car (mktime tm "GMT"))) |
| 97 |
(tm (localtime t "EST+5"))) |
| 98 |
(eqv? (* 5 3600) (tm:gmtoff tm))))) |
| 99 |
|
| 100 |
;; crossing backward over day boundary |
| 101 |
(pass-if "gmtoff of AST-10 at GMT 10pm on 10 Jan 2000" |
| 102 |
(let ((tm (gmtime 0))) |
| 103 |
(set-tm:hour tm 22) |
| 104 |
(set-tm:mday tm 10) |
| 105 |
(set-tm:mon tm 0) |
| 106 |
(set-tm:year tm 100) |
| 107 |
(let* ((t (car (mktime tm "GMT"))) |
| 108 |
(tm (localtime t "AST-10"))) |
| 109 |
(eqv? (* -10 3600) (tm:gmtoff tm))))) |
| 110 |
|
| 111 |
;; crossing forward over year boundary |
| 112 |
(pass-if "gmtoff of EST+5 at GMT 3am on 1 Jan 2000" |
| 113 |
(let ((tm (gmtime 0))) |
| 114 |
(set-tm:hour tm 3) |
| 115 |
(set-tm:mday tm 1) |
| 116 |
(set-tm:mon tm 0) |
| 117 |
(set-tm:year tm 100) |
| 118 |
(let* ((t (car (mktime tm "GMT"))) |
| 119 |
(tm (localtime t "EST+5"))) |
| 120 |
(eqv? (* 5 3600) (tm:gmtoff tm))))) |
| 121 |
|
| 122 |
;; crossing backward over day boundary |
| 123 |
(pass-if "gmtoff of AST-10 at GMT 10pm on 31 Dec 2000" |
| 124 |
(let ((tm (gmtime 0))) |
| 125 |
(set-tm:hour tm 22) |
| 126 |
(set-tm:mday tm 31) |
| 127 |
(set-tm:mon tm 11) |
| 128 |
(set-tm:year tm 100) |
| 129 |
(let* ((t (car (mktime tm "GMT"))) |
| 130 |
(tm (localtime t "AST-10"))) |
| 131 |
(eqv? (* -10 3600) (tm:gmtoff tm)))))) |
| 132 |
|
| 133 |
;;; |
| 134 |
;;; mktime |
| 135 |
;;; |
| 136 |
|
| 137 |
(with-test-prefix "mktime" |
| 138 |
|
| 139 |
;; gmtoff is calculated with some explicit code, try to exercise that |
| 140 |
;; here, looking at cases where the mktime and gmtime are within the same |
| 141 |
;; day, or crossing midnight, or crossing new year |
| 142 |
|
| 143 |
(pass-if "gmtoff of EST+5 at 10:00am on 10 Jan 2000" |
| 144 |
(let ((tm (gmtime 0))) |
| 145 |
(set-tm:hour tm 10) |
| 146 |
(set-tm:mday tm 10) |
| 147 |
(set-tm:mon tm 0) |
| 148 |
(set-tm:year tm 100) |
| 149 |
(let ((tm (cdr (mktime tm "EST+5")))) |
| 150 |
(eqv? (* 5 3600) (tm:gmtoff tm))))) |
| 151 |
|
| 152 |
;; crossing forward over day boundary |
| 153 |
(pass-if "gmtoff of EST+5 at 10:00pm on 10 Jan 2000" |
| 154 |
(let ((tm (gmtime 0))) |
| 155 |
(set-tm:hour tm 22) |
| 156 |
(set-tm:mday tm 10) |
| 157 |
(set-tm:mon tm 0) |
| 158 |
(set-tm:year tm 100) |
| 159 |
(let ((tm (cdr (mktime tm "EST+5")))) |
| 160 |
(eqv? (* 5 3600) (tm:gmtoff tm))))) |
| 161 |
|
| 162 |
;; crossing backward over day boundary |
| 163 |
(pass-if "gmtoff of AST-10 at 3:00am on 10 Jan 2000" |
| 164 |
(let ((tm (gmtime 0))) |
| 165 |
(set-tm:hour tm 3) |
| 166 |
(set-tm:mday tm 10) |
| 167 |
(set-tm:mon tm 0) |
| 168 |
(set-tm:year tm 100) |
| 169 |
(let ((tm (cdr (mktime tm "AST-10")))) |
| 170 |
(eqv? (* -10 3600) (tm:gmtoff tm))))) |
| 171 |
|
| 172 |
;; crossing forward over year boundary |
| 173 |
(pass-if "gmtoff of EST+5 at 10:00pm on 31 Dec 2000" |
| 174 |
(let ((tm (gmtime 0))) |
| 175 |
(set-tm:hour tm 22) |
| 176 |
(set-tm:mday tm 31) |
| 177 |
(set-tm:mon tm 11) |
| 178 |
(set-tm:year tm 100) |
| 179 |
(let ((tm (cdr (mktime tm "EST+5")))) |
| 180 |
(eqv? (* 5 3600) (tm:gmtoff tm))))) |
| 181 |
|
| 182 |
;; crossing backward over day boundary |
| 183 |
(pass-if "gmtoff of AST-10 at 3:00am on 1 Jan 2000" |
| 184 |
(let ((tm (gmtime 0))) |
| 185 |
(set-tm:hour tm 3) |
| 186 |
(set-tm:mday tm 1) |
| 187 |
(set-tm:mon tm 0) |
| 188 |
(set-tm:year tm 100) |
| 189 |
(let ((tm (cdr (mktime tm "AST-10")))) |
| 190 |
(eqv? (* -10 3600) (tm:gmtoff tm)))))) |
| 191 |
|
| 192 |
;;; |
| 193 |
;;; strftime |
| 194 |
;;; |
| 195 |
|
| 196 |
(with-test-prefix "strftime" |
| 197 |
|
| 198 |
(pass-if-equal "strftime %Z doesn't return garbage" |
| 199 |
"ZOW" |
| 200 |
(let ((t (localtime (current-time)))) |
| 201 |
(set-tm:zone t "ZOW") |
| 202 |
(set-tm:isdst t 0) |
| 203 |
(strftime "%Z" t))) |
| 204 |
|
| 205 |
(pass-if-equal "strftime passes wide characters" |
| 206 |
"\u0100" |
| 207 |
(with-locale "en_US.utf8" |
| 208 |
(let ((t (localtime (current-time)))) |
| 209 |
(substring (strftime "\u0100%Z" t) 0 1)))) |
| 210 |
|
| 211 |
(with-test-prefix "C99 %z format" |
| 212 |
|
| 213 |
;; %z here is quite possibly affected by the same tm:gmtoff vs current |
| 214 |
;; zone as %Z above is, so in the following tests we make them the same. |
| 215 |
|
| 216 |
(pass-if-equal "GMT" |
| 217 |
"+0000" |
| 218 |
(begin |
| 219 |
(putenv "TZ=GMT+0") |
| 220 |
(tzset) |
| 221 |
(let ((tm (localtime 86400))) |
| 222 |
(strftime "%z" tm)))) |
| 223 |
|
| 224 |
;; prior to guile 1.6.9 and 1.8.1 this test failed, getting "+0500", |
| 225 |
;; because we didn't adjust for tm:gmtoff being west of Greenwich versus |
| 226 |
;; tm_gmtoff being east of Greenwich |
| 227 |
(pass-if-equal "EST+5" |
| 228 |
"-0500" |
| 229 |
(begin |
| 230 |
(putenv "TZ=EST+5") |
| 231 |
(tzset) |
| 232 |
(let ((tm (localtime 86400))) |
| 233 |
(strftime "%z" tm)))) |
| 234 |
|
| 235 |
(pass-if-equal "strftime fr_FR.utf8" |
| 236 |
" 1 février 1970" |
| 237 |
(with-locale "fr_FR.utf8" |
| 238 |
(strftime "%e %B %Y" (gmtime (* 31 24 3600))))) |
| 239 |
|
| 240 |
(pass-if-equal "strftime fr_FR.iso88591" ;<https://bugs.gnu.org/35920> |
| 241 |
" 1 février 1970" |
| 242 |
(with-locale "fr_FR.iso88591" |
| 243 |
(strftime "%e %B %Y" (gmtime (* 31 24 3600))))))) |
| 244 |
|
| 245 |
;;; |
| 246 |
;;; strptime |
| 247 |
;;; |
| 248 |
|
| 249 |
(with-test-prefix "strptime" |
| 250 |
|
| 251 |
(pass-if "in another thread after error" |
| 252 |
(or (defined? 'strptime) (throw 'unsupported)) |
| 253 |
(or (provided? 'threads) (throw 'unsupported)) |
| 254 |
|
| 255 |
(alarm 5) |
| 256 |
(false-if-exception |
| 257 |
(strptime "%a" "nosuchday")) |
| 258 |
(join-thread (begin-thread (strptime "%d" "1"))) |
| 259 |
(alarm 0) |
| 260 |
#t) |
| 261 |
|
| 262 |
(with-test-prefix "GNU %s format" |
| 263 |
|
| 264 |
;; "%s" to parse a count of seconds since 1970 is a GNU extension |
| 265 |
(define have-strptime-%s |
| 266 |
(false-if-exception (strptime "%s" "0"))) |
| 267 |
|
| 268 |
(pass-if "gmtoff on GMT" |
| 269 |
(or have-strptime-%s (throw 'unsupported)) |
| 270 |
(putenv "TZ=GMT+0") |
| 271 |
(tzset) |
| 272 |
(let ((tm (car (strptime "%s" "86400")))) |
| 273 |
(eqv? 0 (tm:gmtoff tm)))) |
| 274 |
|
| 275 |
(pass-if-equal "strftime fr_FR.utf8" |
| 276 |
'(1 2 1999) |
| 277 |
(with-locale "fr_FR.utf8" |
| 278 |
(let ((tm (car (strptime "%e %B %Y" "1 février 1999")))) |
| 279 |
(list (tm:mday tm) |
| 280 |
(+ 1 (tm:mon tm)) |
| 281 |
(+ 1900 (tm:year tm)))))) |
| 282 |
|
| 283 |
(pass-if-equal "strftime fr_FR.iso88591" ;<https://bugs.gnu.org/35920> |
| 284 |
'(1 2 1999) |
| 285 |
(with-locale "fr_FR.iso88591" |
| 286 |
(let ((tm (car (strptime "%e %B %Y" "1 février 1999")))) |
| 287 |
(list (tm:mday tm) |
| 288 |
(+ 1 (tm:mon tm)) |
| 289 |
(+ 1900 (tm:year tm)))))) |
| 290 |
|
| 291 |
;; prior to guile 1.6.9 and 1.8.1 we didn't pass tm_gmtoff back from |
| 292 |
;; strptime |
| 293 |
(pass-if "gmtoff on EST+5" |
| 294 |
(or have-strptime-%s (throw 'unsupported)) |
| 295 |
(putenv "TZ=EST+5") |
| 296 |
(tzset) |
| 297 |
(let ((tm (car (strptime "%s" "86400")))) |
| 298 |
(eqv? (* 5 3600) (tm:gmtoff tm)))))) |