summaryrefslogtreecommitdiff
path: root/lib/test-getdate.sh
blob: 128def2e689c32961c5e30f2d36a3499cf8f4004 (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
#! /bin/sh

# Test that a getdate executable meets its specification.
#
# Copyright (C) 2004 Free Software Foundation, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */



###
### Globals
###
LOGFILE=`pwd`/getdate.log
if test -f "$LOGFILE"; then
	mv $LOGFILE $LOGFILE~
fi



###
### Functions
###
verify ()
{
	echo >>getdate-got
	if cmp getdate-expected getdate-got >getdate.cmp; then
		echo "PASS: $1" >>$LOGFILE
	else
		cat getdate.cmp >>$LOGFILE
		echo "** expected: " >>$LOGFILE
		cat getdate-expected >>$LOGFILE
		echo "** got: " >>$LOGFILE
		cat getdate-got >>$LOGFILE
		echo "FAIL: $1" | tee -a $LOGFILE >&2
		echo "Failed!  See $LOGFILE for more!" >&2
		exit 1
	fi
}



skip ()
{
	echo "SKIP: $1"${2+" ($2)"} >>$LOGFILE
}



# Prep for future calls to valid_timezone().
#
# This should set $UTZ to three spaces, `GMT', `Unrecognized/Unrecognized', or
# possibly the empty string, depending on what system we are running on.  With
# any luck, this will catch any other existing variations as well.  The way it
# is used later does have the disadvantage of rejecting at least the
# `Europe/London' timezone for half the year when $UTZ gets set to `GMT', like
# happens on NetBSD, but, since I haven't come up with any better ideas and
# since rejecting a timezone just causes a few tests to be skipped, this will
# have to do for now.
#
# UTZ stands for Unrecognized Time Zone.
UTZ=`TZ=Unrecognized/Unrecognized date +%Z`

# The following function will return true if $1 is a valid timezone.  It will
# return false and set $skipreason, otherwise.
#
# Clobbers $NTZ & $skipreason.
#
# SUS2 says `date +%Z' will return `no characters' if `no timezone is
# determinable'.  It is, unfortunately, not very specific about what
# `determinable' means.  On GNU/Linux, `date +%Z' returns $TZ when $TZ is not
# recognized.  NetBSD 1.6.1 "determines" that an unrecognizable value in $TZ
# really means `GMT'.  On Cray, the standard is ignored and `date +%Z' returns
# three spaces when $TZ is not recognized.  We test for all three cases, plus
# the empty string for good measure, though I know of no set of conditions
# which will actually cause `date +%Z' to return the empty string SUS2
# specifies.
#
# Due to the current nature of this test, this will not work for the
# three-letter zone codes on some systems.  e.g.:
#
#	test `TZ=EST date +%Z` = "EST"
#
# should, quite correctly, evaluate to true on most systems, but:
#
#	TZ=Asia/Calcutta date +%Z
#
# would return `IST' on GNU/Linux, and hopefully any system which understands
# the `Asia/Calcutta' timezone, and `   ' on Cray.  Similarly:
#
#	TZ=Doesnt_Exist/Doesnt_Exist date +%Z
#
# returns `Doesnt_Exist/Doesnt_Exist' on GNU/Linux and `   ' on Cray.
#
# Unfortunately, the %z date format string (-HHMM format time zone) supported
# by the GNU `date' command is not part of any standard I know of and,
# therefore, is probably not portable.
#
valid_timezone ()
{
	NTZ=`TZ=$1 date +%Z`
	if test "$NTZ" = "$UTZ" || test "$NTZ" = "$1"; then
		skipreason="$1 is not a recognized timezone on this system"
		return `false`
	else
		return `:`
	fi
}



###
### Tests
###

# Why are these dates tested?
#
# February 29, 2003
#   Is not a leap year - should be invalid.
#
# 2004-12-40
#   Make sure get_date does not "roll" date forward to January 9th.  Some
#   versions have been known to do this.
#
# Dec-5-1972
#   This is my birthday.  :)
#
# 3/29/1974
# 1996/05/12 13:57:45
#   Because.
#
# 12-05-12
#   This will be my 40th birthday.  Ouch.  :)
#
# 05/12/96
#   Because.
#
# third tuesday in March, 2078
#   Wanted this to work.
#
# 1969-12-32 2:00:00 UTC
# 1970-01-01 2:00:00 UTC
# 1969-12-32 2:00:00 +0400
# 1970-01-01 2:00:00 +0400
# 1969-12-32 2:00:00 -0400
# 1970-01-01 2:00:00 -0400
#   Playing near the UNIX Epoch boundry condition to make sure date rolling
#   is also disabled there.
#
# 1996-12-12 1 month
#   Test a relative date.



# The following tests are currently being skipped for being unportable:
#
# Tue Jan 19 03:14:07 2038 +0000
#   For machines with 31-bit time_t, any date past this date will be an
#   invalid date. So, any test date with a value greater than this
#   time is not portable.
#
# Feb. 29, 2096 4 years
#   4 years from this date is _not_ a leap year, so Feb. 29th does not exist.
#
# Feb. 29, 2096 8 years
#   8 years from this date is a leap year, so Feb. 29th does exist,
#   but on many hosts with 32-bit time_t types time, this test will
#   fail. So, this is not a portable test.
#

TZ=UTC0; export TZ

cat >getdate-expected <<EOF
Enter date, or blank line to exit.
	> Bad format - couldn't convert.
	> Bad format - couldn't convert.
	> 1972-12-05 00:00:00.000000000
	> 1974-03-29 00:00:00.000000000
	> 1996-05-12 13:57:45.000000000
	> 2012-05-12 00:00:00.000000000
	> 1996-05-12 00:00:00.000000000
	> Bad format - couldn't convert.
	> Bad format - couldn't convert.
	> 1970-01-01 02:00:00.000000000
	> Bad format - couldn't convert.
	> 1969-12-31 22:00:00.000000000
	> Bad format - couldn't convert.
	> 1970-01-01 06:00:00.000000000
	> 1997-01-12 00:00:00.000000000
	> 
EOF

./getdate >getdate-got <<EOF
February 29, 2003
2004-12-40
Dec-5-1972
3/29/1974
1996/05/12 13:57:45
12-05-12
05/12/96
third tuesday in March, 2078
1969-12-32 2:00:00 UTC
1970-01-01 2:00:00 UTC
1969-12-32 2:00:00 +0400
1970-01-01 2:00:00 +0400
1969-12-32 2:00:00 -0400
1970-01-01 2:00:00 -0400
1996-12-12 1 month
EOF

verify getdate-1



# Why are these dates tested?
#
# Ian Abbot reported these odd boundry cases.  After daylight savings time went
# into effect, non-daylight time zones would cause
# "Bad format - couldn't convert." errors, even when the non-daylight zone
# happened to be a universal one, like GMT.

TZ=Europe/London; export TZ
if valid_timezone $TZ; then
	cat >getdate-expected <<EOF
Enter date, or blank line to exit.
	> 2005-03-01 00:00:00.000000000
	> 2005-03-27 00:00:00.000000000
	> 2005-03-28 01:00:00.000000000
	> 2005-03-28 01:00:00.000000000
	> 2005-03-29 01:00:00.000000000
	> 2005-03-29 01:00:00.000000000
	> 2005-03-30 01:00:00.000000000
	> 2005-03-30 01:00:00.000000000
	> 2005-03-31 01:00:00.000000000
	> 2005-03-31 01:00:00.000000000
	> 2005-04-01 01:00:00.000000000
	> 2005-04-01 01:00:00.000000000
	> 2005-04-10 01:00:00.000000000
	> 2005-04-10 01:00:00.000000000
	> 2005-04-01 00:00:00.000000000
	> 
EOF

	./getdate >getdate-got <<EOF
2005-3-1 GMT
2005-3-27 GMT
2005-3-28 GMT
2005-3-28 UTC0
2005-3-29 GMT
2005-3-29 UTC0
2005-3-30 GMT
2005-3-30 UTC0
2005-3-31 GMT
2005-3-31 UTC0
2005-4-1 GMT
2005-4-1 UTC0
2005-4-10 GMT
2005-4-10 UTC0
2005-4-1 BST
EOF

	verify getdate-2
else
	skip getdate-2 "$skipreason"
fi



# Many of the following cases were also submitted by Ian Abbott, but the same
# errors are not exhibited.  The original problem had a similar root, but
# managed to produce errors with GMT, which is considered a "Universal Zone".
# This was fixed.
#
# The deeper problem has to do with "local zone" processing in getdate.y
# that causes local daylight zones to be excluded when local standard time is
# in effect and vice versa.  This used to cause trouble with GMT in Britian
# when British Summer Time was in effect, but this was overridden for the
# "Universal Timezones" (GMT, UTC, & UT), that might double as a local zone in
# some locales.  We still see in these tests the local daylight/standard zone
# exclusion in EST/EDT.  According to Paul Eggert in a message to
# bug-gnulib@gnu.org on 2005-04-12, this is considered a bug but may not be
# fixed soon due to its complexity.

TZ=America/New_York; export TZ
if valid_timezone $TZ; then
	cat >getdate-expected <<EOF
Enter date, or blank line to exit.
	> 2005-03-01 00:00:00.000000000
	> 2005-02-28 18:00:00.000000000
	> 2005-04-01 00:00:00.000000000
	> Bad format - couldn't convert.
	> 2005-04-30 19:00:00.000000000
	> 2005-04-30 20:00:00.000000000
	> 2005-05-01 00:00:00.000000000
	> 2005-04-30 20:00:00.000000000
	> Bad format - couldn't convert.
	> 2005-05-31 19:00:00.000000000
	> 2005-05-31 20:00:00.000000000
	> 2005-06-01 00:00:00.000000000
	> 2005-05-31 20:00:00.000000000
	> 
EOF

	./getdate >getdate-got <<EOF
2005-3-1 EST
2005-3-1 BST
2005-4-1 EST
2005-5-1 EST
2005-5-1 BST
2005-5-1 GMT
2005-5-1 EDT
2005-5-1 UTC0
2005-6-1 EST
2005-6-1 BST
2005-6-1 GMT
2005-6-1 EDT
2005-6-1 UTC0
EOF

	verify getdate-3
else
	skip getdate-3 "$skipreason"
fi



rm getdate-expected getdate-got getdate.cmp
exit 0