summaryrefslogtreecommitdiffstats
path: root/tests/clock.test
blob: 3b2ad274730fa3f7aa5d552412b68e462d6a20a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
# Commands covered:  clock
#
# This file contains a collection of tests for one or more of the Tcl
# built-in commands.  Sourcing this file into Tcl runs the tests and
# generates output for errors.  No output means no errors were found.
#
# Copyright (c) 1995-1998 Sun Microsystems, Inc.
# Copyright (c) 1998-1999 by Scriptics Corporation.
#
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
#
# RCS: @(#) $Id: clock.test,v 1.22.2.1 2003/04/12 20:11:34 kennykb Exp $

set env(LC_TIME) POSIX

if {[lsearch [namespace children] ::tcltest] == -1} {
    package require tcltest
    namespace import -force ::tcltest::*
}

test clock-1.1 {clock tests} {
    list [catch {clock} msg] $msg
} {1 {wrong # args: should be "clock option ?arg ...?"}}
test clock-1.2 {clock tests} {
    list [catch {clock foo} msg] $msg
} {1 {bad option "foo": must be clicks, format, scan, or seconds}}

# clock clicks
test clock-2.1 {clock clicks tests} {
    expr [clock clicks]+1
    concat {}
} {}
test clock-2.2 {clock clicks tests} {
    set start [clock clicks]
    after 10
    set end [clock clicks]
    expr "$end > $start"
} {1}
test clock-2.3 {clock clicks tests} {
    list [catch {clock clicks foo} msg] $msg
} {1 {bad switch "foo": must be -milliseconds}}
test clock-2.4 {clock clicks tests} {
    expr [clock clicks -milliseconds]+1
    concat {}
} {}
test clock-2.5 {clock clicks tests, millisecond timing test} {
    set start [clock clicks -milli]
    after 10
    set end [clock clicks -milli]
    # 60 msecs seems to be the max time slice under Windows 95/98
    expr {($end > $start) && (($end - $start) <= 60)}
} {1}
test clock-2.6 {clock clicks, milli with too much abbreviation} {
    list [catch { clock clicks {} } msg] $msg
} {1 {bad switch "": must be -milliseconds}}
test clock-2.7 {clock clicks, milli with too much abbreviation} {
    list [catch { clock clicks - } msg] $msg
} {1 {bad switch "-": must be -milliseconds}}

# clock format
test clock-3.1 {clock format tests} {unixOnly} {
    set clockval 657687766
    clock format $clockval -format {%a %b %d %I:%M:%S %p %Y} -gmt true
} {Sun Nov 04 03:02:46 AM 1990}
test clock-3.2 {clock format tests} {
    # TCL_USE_TIMEZONE_VAR

    catch {set oldtz $env(TZ)}
    set env(TZ) PST
    set x {}
    append x [clock format 863800000 -format %Z -gmt 1]
    append x [set env(TZ)]
    catch {unset env(TZ); set env(TZ) $oldtz}
    set x
} {GMTPST}
test clock-3.3 {clock format tests} {
    # tzset() under Borland doesn't seem to set up tzname[] for local 
    # timezone, which caused "clock format" to think that %Z was an invalid
    # string.  Don't care about answer, just that test runs w/o error.

    clock format 863800000 -format %Z
    set x {}
} {}
test clock-3.4 {clock format tests} {
    # tzset() under Borland doesn't seem to set up tzname[] for gmt timezone.
    # tzset() under MSVC has the following weird observed behavior:
    #	 First time we call "clock format [clock seconds] -format %Z -gmt 1"
    #	 we get "GMT", but on all subsequent calls we get the current time 
    #	 zone string, even though env(TZ) is GMT and the variable _timezone 
    #    is 0.

    set x {}
    append x [clock format 863800000 -format %Z -gmt 1]
    append x [clock format 863800000 -format %Z -gmt 1]
} {GMTGMT}
test clock-3.5 {clock format tests} {
    list [catch {clock format} msg] $msg
} {1 {wrong # args: should be "clock format clockval ?-format string? ?-gmt boolean?"}}
test clock-3.6 {clock format tests} {
    list [catch {clock format foo} msg] $msg
} {1 {expected integer but got "foo"}}
test clock-3.7 {clock format tests} {unixOrPc} {
    set clockval 657687766
    clock format $clockval -format "%a %b %d %I:%M:%S %p %Y" -gmt true
} "Sun Nov 04 03:02:46 AM 1990"
test clock-3.8 {clock format tests} {
    list [catch {clock format a b c d e g} msg] $msg
} {1 {wrong # args: should be "clock format clockval ?-format string? ?-gmt boolean?"}}
test clock-3.9 {clock format tests} {unixOrPc nonPortable} {
    set clockval -1
    clock format $clockval -format "%a %b %d %I:%M:%S %p %Y" -gmt true
} "Wed Dec 31 11:59:59 PM 1969"
test clock-3.10 {clock format tests} {
    list [catch {clock format 123 -bad arg} msg] $msg
} {1 {bad switch "-bad": must be -format or -gmt}}
test clock-3.11 {clock format tests} {
    clock format 123 -format "x"
} x
test clock-3.12 {clock format tests} {
    clock format 123 -format ""
} ""
test clock-3.13 {clock format with non-ASCII character in the format string} {
    set oldenc [encoding system] 
    encoding system iso8859-1
    set res [clock format 0 -format \u00c4]
    encoding system $oldenc
    unset oldenc
    set res
} "\u00c4"

# clock scan
test clock-4.1 {clock scan tests} {
    list [catch {clock scan} msg] $msg
} {1 {wrong # args: should be "clock scan dateString ?-base clockValue? ?-gmt boolean?"}}
test clock-4.2 {clock scan tests} {
    list [catch {clock scan "bad-string"} msg] $msg
} {1 {unable to convert date-time string "bad-string"}}
test clock-4.3 {clock scan tests} {
    clock format [clock scan "14 Feb 92" -gmt true] \
      -format {%m/%d/%y %I:%M:%S %p} -gmt true
} {02/14/92 12:00:00 AM}
test clock-4.4 {clock scan tests} {
    clock format [clock scan "Feb 14, 1992 12:20 PM" -gmt true] \
      -format {%m/%d/%y %I:%M:%S %p} -gmt true
} {02/14/92 12:20:00 PM}
test clock-4.5 {clock scan tests} {
    clock format \
      [clock scan "Feb 14, 1992 12:20 PM" -base 319363200 -gmt true] \
      -format {%m/%d/%y %I:%M:%S %p} -gmt true
} {02/14/92 12:20:00 PM}
test clock-4.6 {clock scan tests} {
    set time [clock scan "Oct 23,1992 15:00"]
    clock format $time -format {%b %d,%Y %H:%M}
} {Oct 23,1992 15:00}
test clock-4.7 {clock scan tests} {
    set time [clock scan "Oct 23,1992 15:00 GMT"]
    clock format $time -format {%b %d,%Y %H:%M GMT} -gmt true
} {Oct 23,1992 15:00 GMT}
test clock-4.8 {clock scan tests} {
    set time [clock scan "Oct 23,1992 15:00" -gmt true]
    clock format $time -format {%b %d,%Y %H:%M GMT} -gmt true
} {Oct 23,1992 15:00 GMT}
test clock-4.9 {clock scan tests} {
    list [catch {clock scan "Jan 12" -bad arg} msg] $msg
} {1 {bad switch "-bad": must be -base or -gmt}}
# The following two two tests test the two year date policy
test clock-4.10 {clock scan tests} {
    set time [clock scan "1/1/71" -gmt true]
    clock format $time -format {%b %d,%Y %H:%M GMT} -gmt true
} {Jan 01,1971 00:00 GMT}
test clock-4.11 {clock scan tests} {
    set time [clock scan "1/1/37" -gmt true]
    clock format $time -format {%b %d,%Y %H:%M GMT} -gmt true
} {Jan 01,2037 00:00 GMT}

test clock-4.12 {clock scan, relative times} {
    set time [clock scan "Oct 23, 1992 -1 day"]
    clock format $time -format {%b %d, %Y}
} "Oct 22, 1992"
test clock-4.13 {clock scan, ISO 8601 base date format} {
    set time [clock scan "19921023"]
    clock format $time -format {%b %d, %Y}
} "Oct 23, 1992"
test clock-4.14 {clock scan, ISO 8601 expanded date format} {
    set time [clock scan "1992-10-23"]
    clock format $time -format {%b %d, %Y}
} "Oct 23, 1992"
test clock-4.15 {clock scan, DD-Mon-YYYY format} {
    set time [clock scan "23-Oct-1992"]
    clock format $time -format {%b %d, %Y}
} "Oct 23, 1992"
test clock-4.16 {clock scan, ISO 8601 point in time format} {
    set time [clock scan "19921023T235959"]
    clock format $time -format {%b %d, %Y %H:%M:%S}
} "Oct 23, 1992 23:59:59"
test clock-4.17 {clock scan, ISO 8601 point in time format} {
    set time [clock scan "19921023 235959"]
    clock format $time -format {%b %d, %Y %H:%M:%S}
} "Oct 23, 1992 23:59:59"
test clock-4.18 {clock scan, ISO 8601 point in time format} {
    set time [clock scan "19921023T000000"]
    clock format $time -format {%b %d, %Y %H:%M:%S}
} "Oct 23, 1992 00:00:00"

# CLOCK SCAN REAL TESTS
# We use 5am PST, 31-12-1999 as the base for these scans because irrespective
# of your local timezone it should always give us times on December 31, 1999
set 5amPST 946645200
test clock-4.19 {clock scan, number meridian} {
    set t1 [clock scan "5 am" -base $5amPST -gmt true]
    set t2 [clock scan "5 pm" -base $5amPST -gmt true]
    set t3 [clock scan "5 a.m." -base $5amPST -gmt true]
    set t4 [clock scan "5 p.m." -base $5amPST -gmt true]
    list \
	    [clock format $t1 -format {%b %d, %Y %H:%M:%S} -gmt true] \
	    [clock format $t2 -format {%b %d, %Y %H:%M:%S} -gmt true] \
	    [clock format $t3 -format {%b %d, %Y %H:%M:%S} -gmt true] \
	    [clock format $t4 -format {%b %d, %Y %H:%M:%S} -gmt true]
} [list "Dec 31, 1999 05:00:00" "Dec 31, 1999 17:00:00" \
	"Dec 31, 1999 05:00:00" "Dec 31, 1999 17:00:00"]
test clock-4.20 {clock scan, number:number meridian} {
    clock format [clock scan "5:30 pm" -base $5amPST -gmt true] \
	    -format {%b %d, %Y %H:%M:%S} -gmt true
} "Dec 31, 1999 17:30:00"
test clock-4.21 {clock scan, number:number-timezone} {
    clock format [clock scan "00:00-0800" -gmt true -base $5amPST] \
	    -format {%b %d, %Y %H:%M:%S} -gmt true
} "Dec 31, 1999 08:00:00"
test clock-4.22 {clock scan, number:number:number o_merid} {
    clock format [clock scan "8:00:00" -gmt true -base $5amPST] \
	    -format {%b %d, %Y %H:%M:%S} -gmt true
} "Dec 31, 1999 08:00:00"
test clock-4.23 {clock scan, number:number:number o_merid} {
    clock format [clock scan "8:00:00 am" -gmt true -base $5amPST] \
	    -format {%b %d, %Y %H:%M:%S} -gmt true
} "Dec 31, 1999 08:00:00"
test clock-4.24 {clock scan, number:number:number o_merid} {
    clock format [clock scan "8:00:00 pm" -gmt true -base $5amPST] \
	    -format {%b %d, %Y %H:%M:%S} -gmt true
} "Dec 31, 1999 20:00:00"
test clock-4.25 {clock scan, number:number:number-timezone} {
    clock format [clock scan "00:00:30-0800" -gmt true -base $5amPST] \
	    -format {%b %d, %Y %H:%M:%S} -gmt true
} "Dec 31, 1999 08:00:30"
test clock-4.26 {clock scan, DST for days} {
    clock scan "tomorrow" -base [clock scan "19991031 00:00:00"]
} [clock scan "19991101 00:00:00"]
test clock-4.27 {clock scan, DST for days} {
    clock scan "yesterday" -base [clock scan "19991101 00:00:00"]
} [clock scan "19991031 00:00:00"]
test clock-4.28 {clock scan, day} knownBug {
    clock format [clock scan "Monday" -gmt true -base 946627200] \
	    -format {%b %d, %Y %H:%M:%S} -gmt true
} "Jan 03, 2000 00:00:00"
test clock-4.29 {clock scan, number/number} {
    clock format [clock scan "1/1" -gmt true -base 946627200] \
	    -format {%b %d, %Y %H:%M:%S} -gmt true
} "Jan 01, 1999 00:00:00"
test clock-4.30 {clock scan, number/number} {
    clock format [clock scan "1/1/1999" -gmt true -base 946627200] \
	    -format {%b %d, %Y %H:%M:%S} -gmt true
} "Jan 01, 1999 00:00:00"
test clock-4.31 {clock scan, number/number} {
    clock format [clock scan "19990101" -gmt true -base 946627200] \
	    -format {%b %d, %Y %H:%M:%S} -gmt true
} "Jan 01, 1999 00:00:00"
test clock-4.32 {clock scan, relative minutes} {
    clock scan "now + 1 minute" -base 946627200
} 946627260
test clock-4.33 {clock scan, relative minutes} {
    clock scan "now +1 minute" -base 946627200
} 946627260
test clock-4.34 {clock scan, relative minutes} {
    clock scan "now 1 minute" -base 946627200
} 946627260
test clock-4.35 {clock scan, relative minutes} {
    clock scan "now - 1 minute" -base 946627200
} 946627140
test clock-4.36 {clock scan, relative minutes} {
    clock scan "now -1 minute" -base 946627200
} 946627140
test clock-4.37 {clock scan, day of week} {
    clock format [clock scan "wednesday" -base [clock scan 20000112]] \
	    -format {%b %d, %Y}
} "Jan 12, 2000"
test clock-4.38 {clock scan, next day of week} {
    clock format [clock scan "next wednesday" -base [clock scan 20000112]] \
	    -format {%b %d, %Y}
} "Jan 19, 2000"
test clock-4.39 {clock scan, day of week} {
    clock format [clock scan "thursday" -base [clock scan 20000112]] \
	    -format {%b %d, %Y}
} "Jan 13, 2000"
test clock-4.40 {clock scan, next day of week} {
    clock format [clock scan "next thursday" -base [clock scan 20000112]] \
	    -format {%b %d, %Y}
} "Jan 20, 2000"

# weekday specification and base.
test clock-4.41 {2nd monday in november} {
    set res {}
    foreach i {91 92 93 94 95 96} {
      set nov8th [clock scan 11/8/$i]
      set monday [clock scan monday -base $nov8th]
      lappend res [clock format $monday -format %Y-%m-%d]
    }
    set res
} {1991-11-11 1992-11-09 1993-11-08 1994-11-14 1995-11-13 1996-11-11}
test clock-4.42 {2nd monday in november (2nd try)} {
    set res {}
    foreach i {91 92 93 94 95 96} {
      set nov1th [clock scan 11/1/$i]
      set monday [clock scan "2 monday" -base $nov1th]
      lappend res [clock format $monday -format %Y-%m-%d]
    }
    set res
} {1991-11-11 1992-11-09 1993-11-08 1994-11-14 1995-11-13 1996-11-11}
test clock-4.43 {last monday in november} {
    set res {}
    foreach i {91 92 93 94 95 96} {
      set dec1th [clock scan 12/1/$i]
      set monday [clock scan "monday 1 week ago" -base $dec1th]
      lappend res [clock format $monday -format %Y-%m-%d]
    }
    set res
} {1991-11-25 1992-11-30 1993-11-29 1994-11-28 1995-11-27 1996-11-25}

test clock-4.44 {2nd monday in november} knownBug {
    set res {}
    foreach i {91 92 93 94 95 96} {
      set nov8th [clock scan 11/8/$i -gmt 1]
      set monday [clock scan monday -base $nov8th -gmt 1]
      lappend res [clock format $monday -format %Y-%m-%d -gmt 1]
    }
    set res
} {1991-11-11 1992-11-09 1993-11-08 1994-11-14 1995-11-13 1996-11-11}
test clock-4.45 {2nd monday in november (2nd try)} knownBug {
    set res {}
    foreach i {91 92 93 94 95 96} {
      set nov1th [clock scan 11/1/$i -gmt 1]
      set monday [clock scan "2 monday" -base $nov1th -gmt 1]
      lappend res [clock format $monday -format %Y-%m-%d -gmt 1]
    }
    set res
} {1991-11-11 1992-11-09 1993-11-08 1994-11-14 1995-11-13 1996-11-11}
test clock-4.46 {last monday in november} knownBug {
    set res {}
    foreach i {91 92 93 94 95 96} {
      set dec1th [clock scan 12/1/$i -gmt 1]
      set monday [clock scan "monday 1 week ago" -base $dec1th -gmt 1]
      lappend res [clock format $monday -format %Y-%m-%d -gmt 1]
    }
    set res
} {1991-11-25 1992-11-30 1993-11-29 1994-11-28 1995-11-27 1996-11-25}
test clock-4.47 {ago with multiple relative units} {
    set base [clock scan "12/31/1999 00:00:00"]
    set res [clock scan "2 days 2 hours ago" -base $base]
    expr {$base - $res}
} 180000

# clock seconds
test clock-5.1 {clock seconds tests} {
    expr [clock seconds]+1
    concat {}
} {}
test clock-5.2 {clock seconds tests} {
    list [catch {clock seconds foo} msg] $msg
} {1 {wrong # args: should be "clock seconds"}}
test clock-5.3 {clock seconds tests} {
    set start [clock seconds]
    after 2000
    set end [clock seconds]
    expr "$end > $start"
} {1}

# The following dates check certain roll over dates
set day [expr 24 * 60 * 60]
test clock-6.1 {clock roll over dates} {
    set time [clock scan "12/31/1998" -gmt true]
    clock format [expr $time + $day] -format {%b %d,%Y %H:%M GMT} -gmt true
} {Jan 01,1999 00:00 GMT}
test clock-6.2 {clock roll over dates} {
    set time [clock scan "12/31/1999" -gmt true]
    clock format [expr $time + $day] -format {%b %d,%Y %H:%M GMT} -gmt true
} {Jan 01,2000 00:00 GMT}
test clock-6.3 {clock roll over dates} {
    set time [clock scan "2/28/2000" -gmt true]
    clock format [expr $time + $day] -format {%b %d,%Y %H:%M GMT} -gmt true
} {Feb 29,2000 00:00 GMT}
test clock-6.4 {clock roll over dates} {
    set time [clock scan "2/29/2000" -gmt true]
    clock format [expr $time + $day] -format {%b %d,%Y %H:%M GMT} -gmt true
} {Mar 01,2000 00:00 GMT}
test clock-6.5 {clock roll over dates} {
    set time [clock scan "January 1, 2000" -gmt true]
    clock format $time -format %A -gmt true
} {Saturday}
test clock-6.6 {clock roll over dates} {
    set time [clock scan "January 1, 2000" -gmt true]
    clock format $time -format %j -gmt true
} {001}
test clock-6.7 {clock roll over dates} {
    set time [clock scan "February 29, 2000" -gmt true]
    clock format $time -format %A -gmt true
} {Tuesday}
test clock-6.8 {clock roll over dates} {
    set time [clock scan "February 29, 2000" -gmt true]
    clock format $time -format %j -gmt true
} {060}
test clock-6.9 {clock roll over dates} {
    set time [clock scan "March 1, 2000" -gmt true]
    clock format $time -format %A -gmt true
} {Wednesday}
test clock-6.10 {clock roll over dates} {
    set time [clock scan "March 1, 2000" -gmt true]
    clock format $time -format %j -gmt true
} {061}
test clock-6.11 {clock roll over dates} {
    set time [clock scan "March 1, 2001" -gmt true]
    clock format $time -format %j -gmt true
} {060}

test clock-7.1 {clock scan next monthname} {
    clock format [clock scan "next june" -base [clock scan "june 1, 2000"]] \
	    -format %m.%Y
} "06.2001"
test clock-7.2 {clock scan next monthname} {
    clock format [clock scan "next july" -base [clock scan "june 1, 2000"]] \
	    -format %m.%Y
} "07.2000"
test clock-7.3 {clock scan next monthname} {
    clock format [clock scan "next may" -base [clock scan "june 1, 2000"]] \
	    -format %m.%Y
} "05.2001"

# We use 5am PST, 31-12-1999 as the base for these scans because irrespective
# of your local timezone it should always give us times on December 31
set 5amPST 946645200
test clock-8.1 {clock scan midnight/gmt range bug 413397} {
    set fmt "%m/%d"
    list [clock format [clock scan year -base $5amPST -gmt 0] -format $fmt] \
	    [clock format [clock scan year -base $5amPST -gmt 1] -format $fmt]
} {12/31 12/31}

set ::tcltest::testConstraints(needPST) [expr {
    [regexp {^(Pacific.*|P[DS]T)$} [clock format 1 -format %Z]]
    && ([clock format 1 -format %s] != "%s")
}]
test clock-9.1 {%s gmt testing} {needPST} {
    # We need PST to guarantee the difference value below, and %s isn't
    # valid on all OSes (like Solaris).
    set s 100000
    set a [clock format $s -format %s -gmt 0]
    set b [clock format $s -format %s -gmt 1]
    # This should be the offset in seconds between current locale and GMT.
    # This didn't seem to be correctly on Windows until the fix for
    # Bug #559376, which fiddled with env(TZ) when -gmt 1 was used.
    # It's hard-coded to check P[SD]T now. (8 hours)
    set c [expr {$b-$a}]
} {28800}

# cleanup
::tcltest::cleanupTests
return