summaryrefslogtreecommitdiffstats
path: root/tests/clock.test
blob: 1b1632f6892b5b179637031370d35e1b4b7e9b8f (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
# 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.5 1999/09/21 04:20:44 hobbs Exp $

if {[lsearch [namespace children] ::tcltest] == -1} {
    package require tcltest
    namespace import ::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.3 {clock clicks tests} {
    expr [clock clicks -milliseconds]+1
    concat {}
} {}
test clock-2.2 {clock clicks tests, millisecond timing test} {
    set start [clock clicks -milli]
    after 10
    set end [clock clicks -milli]
    # assume, even with slow interp'ing, the diff is less than 60 msecs
    expr {($end > $start) && (($end - $start) < 60)}
} {1}

# 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 ""
} ""

# 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}

# 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}

# cleanup
::tcltest::cleanupTests
return