Tizen 2.0 Release
[external/tizen-coreutils.git] / tests / misc / date
1 #!/bin/sh
2 # Test "date".
3
4 # Copyright (C) 2005-2007 Free Software Foundation, Inc.
5
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10
11 # This program 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
14 # GNU General Public License for more details.
15
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 # 02110-1301, USA.
20
21 : ${PERL=perl}
22 : ${srcdir=.}
23
24 $PERL -e 1 > /dev/null 2>&1 || {
25   echo 1>&2 "$0: configure didn't find a usable version of Perl," \
26     "so can't run this test"
27   exit 77
28 }
29
30 exec $PERL -w -I$srcdir/.. -MCoreutils -- - <<\EOF
31 require 5.003;
32 use strict;
33
34 (my $ME = $0) =~ s|.*/||;
35
36 # Turn off localisation of executable's ouput.
37 @ENV{qw(LANGUAGE LANG LC_ALL)} = ('C') x 3;
38
39 # Export TZ=UTC0 so that zone-dependent strings match.
40 $ENV{TZ} = 'UTC0';
41
42 my $t0 = '08:17:48';
43 my $d0 = '1997-01-19';
44 my $d1 = "$d0 $t0 +0";
45
46 my $ts = '08:17:49'; # next second
47 my $tm = '08:18:48'; # next minute
48 my $th = '09:17:48'; # next hour
49
50 my $dd = '1997-01-20'; # next day
51 my $dw = '1997-01-26'; # next week
52 my $dm = '1997-02-19'; # next month
53 my $dy = '1998-01-19'; # next month
54
55 my $fmt = "'+%Y-%m-%d %T'";
56
57 # Determine a number of seconds that will provoke an invalid
58 # time diagnostic from date.  If possible, use a number that
59 # is less than 2^64, yet so large that it would cause the resulting
60 # tm_year value to be too large for a 32-bit int.  But some systems
61 # (Solaris 8) have a buggy localtime that mistakenly accepts such
62 # invalid times and give garbage in tm_year.  Other systems might
63 # have an int type that is wider than 32.  So if this localtime
64 # call succeeds, use 2^64 as the number of seconds.
65 my $n_seconds = 72057594037927935;
66 my @d = localtime ($n_seconds);
67 my $year = $d[5];
68 defined $year
69   and $n_seconds = '18446744073709551616';
70
71 my @Tests =
72     (
73      # test-name, [option, option, ...] {OUT=>"expected-output"}
74      #
75      ['1', "-d '$d1' +'%% %a %A %b %B'", {OUT=>"% Sun Sunday Jan January"}],
76
77      # [Actually, skip it on *all* systems. -- this Perl code is run at
78      # distribution-build-time, not at configure/test time.  ]
79
80      # Skip the test of %c on SunOS4 systems.  Such systems would fail this
81      # test because their underlying strftime doesn't handle the %c format
82      # properly.  GNU strftime must rely on the underlying host library
83      # function to get locale-dependent behavior, as strftime is the only
84      # portable interface to that behavior.
85      # ['2', "-d '$d1' +'%c'", {OUT=>"Sun Jan 19 $t0 1997"}],
86
87      ['3', "-d '$d1' +'%d_%D_%e_%h_%H'", {OUT=>"19_01/19/97_19_Jan_08"}],
88      ['4', "-d '$d1' +'%I_%j_%k_%l_%m'", {OUT=>"08_019_ 8_ 8_01"}],
89      ['5', "-d '$d1' +'%M_%n_%p_%r'", {OUT=>"17_\n_AM_$t0 AM"}],
90      ['6', "-d '$d1' +'%s_%S_%t_%T'", {OUT=>"853661868_48_\t_$t0"}],
91      ['7', "-d '$d1' +'%U_%V_%w_%W'", {OUT=>"03_03_0_02"}],
92      ['8', "-d '$d1' +'%x_%X_%y_%Y'", {OUT=>"01/19/97_${t0}_97_1997"}],
93      ['9', "-d '$d1' +'%z'", {OUT=>"+0000"}],
94
95      ['leap-1', "--date '02/29/1996 1 year' +%Y-%m-%d", {OUT=>"1997-03-01"}],
96
97      ['U95-1', "--date '1995-1-1' +%U", {OUT=>"01"}],
98      ['U95-2', "--date '1995-1-7' +%U", {OUT=>"01"}],
99      ['U95-3', "--date '1995-1-8' +%U", {OUT=>"02"}],
100
101      ['U92-1', "--date '1992-1-1' +%U", {OUT=>"00"}],
102      ['U92-2', "--date '1992-1-4' +%U", {OUT=>"00"}],
103      ['U92-3', "--date '1992-1-5' +%U", {OUT=>"01"}],
104
105      ['V92-1', "--date '1992-1-1' +%V", {OUT=>"01"}],
106      ['V92-2', "--date '1992-1-5' +%V", {OUT=>"01"}],
107      ['V92-3', "--date '1992-1-6' +%V", {OUT=>"02"}],
108
109      ['W92-1', "--date '1992-1-1' +%W", {OUT=>"00"}],
110      ['W92-2', "--date '1992-1-5' +%W", {OUT=>"00"}],
111      ['W92-3', "--date '1992-1-6' +%W", {OUT=>"01"}],
112
113      ['millen-1', "--date '1998-1-1 3 years' +%Y", {OUT=>"2001"}],
114
115      ['rel-0', "-d '$d1 now' '+%Y-%m-%d %T'", {OUT=>"$d0 $t0"}],
116
117      ['rel-1a', "-d '$d1 yesterday' $fmt", {OUT=>"1997-01-18 $t0"}],
118      ['rel-1b', "-d '$d1 tomorrow' $fmt", {OUT=>"1997-01-20 $t0"}],
119
120      ['rel-2a', "-d '$d1 6 years ago' $fmt", {OUT=>"1991-01-19 $t0"}],
121      ['rel-2b', "-d '$d1 7 months ago' $fmt", {OUT=>"1996-06-19 $t0"}],
122      ['rel-2c', "-d '$d1 8 weeks ago' $fmt", {OUT=>"1996-11-24 $t0"}],
123      ['rel-2d', "-d '$d1 1 day ago' $fmt", {OUT=>"1997-01-18 $t0"}],
124      ['rel-2e', "-d '$d1 2 hours ago' $fmt", {OUT=>"$d0 06:17:48"}],
125      ['rel-2f', "-d '$d1 3 minutes ago' $fmt", {OUT=>"$d0 08:14:48"}],
126      ['rel-2g', "-d '$d1 4 seconds ago' $fmt", {OUT=>"$d0 08:17:44"}],
127
128      ['rel-3a', "-d '$d1 4 seconds ago' $fmt", {OUT=>"$d0 08:17:44"}],
129
130      ['next-s', "-d '$d1 next second' '+%Y-%m-%d %T'", {OUT=>"$d0 $ts"}],
131      ['next-m', "-d '$d1 next minute' '+%Y-%m-%d %T'", {OUT=>"$d0 $tm"}],
132      ['next-h', "-d '$d1 next hour'   '+%Y-%m-%d %T'", {OUT=>"$d0 $th"}],
133      ['next-d', "-d '$d1 next day'    '+%Y-%m-%d %T'", {OUT=>"$dd $t0"}],
134      ['next-w', "-d '$d1 next week'   '+%Y-%m-%d %T'", {OUT=>"$dw $t0"}],
135      ['next-mo', "-d '$d1 next month' '+%Y-%m-%d %T'", {OUT=>"$dm $t0"}],
136      ['next-y', "-d '$d1 next year'   '+%Y-%m-%d %T'", {OUT=>"$dy $t0"}],
137
138      ['utc-0', "-u -d '08/01/97 6:00' '+%D,%H:%M'", {OUT=>"08/01/97,06:00"},
139                {ENV => 'TZ=UTC+4'}],
140
141      ['utc-0a', "-u -d '08/01/97 6:00 UTC +4 hours' '+%D,%H:%M'",
142       {OUT=>"08/01/97,10:00"}],
143      # Make sure --file=FILE works with -u.
144      ['utc-1', "-u --file=f '+%Y-%m-%d %T'",
145       {AUX=>{f=>"$d0 $t0\n$d0 $t0"}},
146       {OUT=>"$d0 $t0\n$d0 $t0"},
147       {ENV => 'TZ=UTC+1'}],
148
149      ['utc-1a', "-u --file=f '+%Y-%m-%d %T'",
150       {AUX=>{f=>"$d0 $t0 UTC +1 hour\n$d0 $t0 UTC +1 hour"}},
151       {OUT=>"$d0 $th\n$d0 $th"}],
152
153      # From the examples in the documentation.
154      ['date2sec-0', "-d '1970-01-01 00:00:01' +%s", {OUT=>"7201"},
155       {ENV => 'TZ=UTC+2'}],
156
157      # Same as above, but don't rely on TZ in environment.
158      ['date2sec-0a', "-d '1970-01-01 00:00:01 UTC +2 hours' +%s",
159       {OUT=>"7201"}],
160
161      ['date2sec-1', "-d 2000-01-01 +%s", {OUT=>"946684800"}],
162      ['sec2date-0', "-d '1970-01-01 UTC 946684800 sec' +'%Y-%m-%d %T %z'",
163       {OUT=>"2000-01-01 00:00:00 +0000"}],
164
165      ['this-m', "-d '$d0 $t0 this minute' $fmt", {OUT=>"$d0 $t0"}],
166      ['this-h', "-d '$d0 $t0 this hour' $fmt", {OUT=>"$d0 $t0"}],
167      ['this-w', "-d '$d0 $t0 this week' $fmt", {OUT=>"$d0 $t0"}],
168      ['this-mo', "-d '$d0 $t0 this month' $fmt", {OUT=>"$d0 $t0"}],
169      ['this-y', "-d '$d0 $t0 this year' $fmt", {OUT=>"$d0 $t0"}],
170
171      ['risks-1', "-d 'Nov 10 1996' $fmt", {OUT=>"1996-11-10 00:00:00"}],
172
173      # This one would pass if TZ (with any, or even no, value) were in
174      # the environment.
175      ['regress-1', "-u -d '1996-11-10 0:00:00 +0' $fmt",
176      {OUT=>"1996-11-10 00:00:00"},
177      {ENV =>'LANG=C'}],
178
179
180      ['datevtime-1', "-d 000909 $fmt", {OUT=>"2000-09-09 00:00:00"}],
181
182      # test for RFC-822 conformance
183      ['rfc822-1', "-R -d '$d1'", {OUT=>"Sun, 19 Jan 1997 08:17:48 +0000"},
184       # Solaris 5.9's /bin/sh emits this diagnostic to stderr
185       # if you don't have support for the named locale.
186       {ERR_SUBST => q!s/^couldn't set locale correctly\n//!},
187       {ENV => 'LC_ALL=de_DE TZ=UTC0'}],
188
189      # Relative seconds, with time.  fixed in 2.0j
190      ['relative-1', "--utc -d '1970-01-01 00:00:00 UTC +961062237 sec' $fmt",
191       {OUT=>"2000-06-15 09:43:57"}],
192
193      # Relative seconds, no time.
194      ['relative-2', "--utc -d '1970-01-01 UTC +961062237 sec' $fmt",
195       {OUT=>"2000-06-15 09:43:57"},
196       {ENV => 'TZ=UTC+1'}],
197
198      # Relative days, no time, across time zones.
199      ['relative-3', "-I -d '2006-04-23 21 days ago'", {OUT=>"2006-04-02"},
200          {ENV=>'TZ=PST8PDT,M4.1.0,M10.5.0'}],
201
202      # This would infloop (or appear to) prior to coreutils-4.5.5,
203      # due to a bug in strftime.c.
204      ['wide-fmt', "-d '1999-06-01'", '+%3004Y', {OUT=>'0' x 3000 . "1999"}],
205
206      # Ensure that we can parse MONTHNAME-DAY-YEAR.
207      ['moname-d-y', '--iso -d May-23-2003', {OUT=>"2003-05-23"}],
208      ['moname-d-y-r', '--rfc-3339=date -d May-23-2003', {OUT=>"2003-05-23"}],
209
210      ['epoch', '--iso=sec -d @31536000',
211       {OUT=>"1971-01-01T00:00:00+0000"}],
212      ['epoch-r', '--rfc-3339=sec -d @31536000',
213       {OUT=>"1971-01-01 00:00:00+00:00"}],
214
215      ['ns-10', '--iso=ns', '-d "1969-12-31 13:00:00.00000001-1100"',
216       {OUT=>"1970-01-01T00:00:00,000000010+0000"}],
217      ['ns-10-r', '--rfc-3339=ns', '-d "1969-12-31 13:00:00.00000001-1100"',
218       {OUT=>"1970-01-01 00:00:00.000000010+00:00"}],
219
220      ['ns-max32', '--iso=ns', '-d "2038-01-19 03:14:07.999999999"',
221       {OUT=>"2038-01-19T03:14:07,999999999+0000"}],
222      ['ns-max32-r', '--rfc-3339=ns', '-d "2038-01-19 03:14:07.999999999"',
223       {OUT=>"2038-01-19 03:14:07.999999999+00:00"}],
224
225      ['tz-1', '+%:::z', {OUT=>"-12:34:56"}, {ENV=>'TZ=XXX12:34:56'}],
226
227      ['tz-2', '+%:::z', {OUT=>"+12:34:56"}, {ENV=>'TZ=XXX-12:34:56'}],
228
229      ['tz-3', '+%::z', {OUT=>"+01:02:03"}, {ENV=>'TZ=XXX-1:02:03'}],
230
231      ['tz-4', '+%:::z', {OUT=>"+12"}, {ENV=>'TZ=XXX-12'}],
232
233      ['tz-5', '+%:z', {OUT=>"-00:01"}, {ENV=>'TZ=XXX0:01'}],
234
235      # Accept %:z with a field width before the `:'.
236      ['tz-5w','+%8:z', {OUT=>"-0000:01"}, {ENV=>'TZ=XXX0:01'}],
237      # Don't recognize %:z with a field width between the `:' and the `z'.
238      ['tz-5wf', '+%:8z', {OUT=>"%:8z"}, {ENV=>'TZ=XXX0:01'}],
239
240      ['ns-relative',
241       '--iso=ns', "-d'1970-01-01 00:00:00.1234567 UTC +961062237.987654321 sec'",
242       {OUT=>"2000-06-15T09:43:58,111111021+0000"}],
243      ['ns-relativer', '--rfc-3339=ns',
244       "-d'1970-01-01 00:00:00.1234567 UTC +961062237.987654321 sec'",
245       {OUT=>"2000-06-15 09:43:58.111111021+00:00"}],
246
247      # Since coreutils/lib/getdate.y revision 1.96 (post-coreutils-5.3.0),
248      # a command like the following would mistakenly exit nonzero with an
249      # `invalid date ...' diagnostic, but when run in a time zone for
250      # which daylight savings time is in effect for the starting date.
251      # Unfortunately (for ease of testing), if you set TZ at all, this
252      # failure is not triggered, hence the removal of TZ from the environment.
253      ['cross-dst', "-d'2005-03-27 +1 day'", '+%Y', {OUT=>"2005"},
254                   {ENV_DEL => 'TZ'},
255                   ],
256
257      ['empty-fmt', '+', {OUT=>""}],
258
259      ['neg-secs', '-d @-22 +%05s', {OUT=>"-0022"}],
260      ['neg-secs2', '-d @-22 +%_5s', {OUT=>"  -22"}],
261
262      # Before today's fix, date would print uninitialized data
263      # to standard output for an out-of-range date:
264      # $ date -d @$(echo 2^56-1|bc) 2> /dev/null | od -a -N3
265      # 0000000   p   4   6
266      # 0000003
267      ['uninit-64', "-d \@$n_seconds",
268       {OUT=>''},
269       # Use ERR_SUBST to get around fact that the diagnostic
270       # you get on a system with 32-bit time_t is not the same as
271       # the one you get for a system where it's 64 bits wide:
272       # - date: time 72057594037927935 is out of range
273       # + date: invalid date `@72057594037927935'
274       {ERR_SUBST => 's/.*//'},
275       {ERR => "\n"},
276       {EXIT => 1},
277      ],
278
279      ['fill-1', '-d 1999-12-08 +%_3d', {OUT=>'  8'}],
280      ['fill-2', '-d 1999-12-08 +%03d', {OUT=>'008'}],
281
282      # Test the combination of the to-upper-case modifier (^) and a conversion
283      # specifier that expands to a string containing lower case characters.
284      ['subfmt-up1', '-d "1999-12-08 7:30" "+%^c"',
285       # Solaris 5.9 prints 'WED DEC 08 07:30:00 1999', while
286       # most others print  'WED DEC  8 07:30:00 1999'.
287       {OUT_SUBST => 's/ [ 0]8.*//'},
288       {OUT=>'WED DEC'}],
289     );
290
291 # Repeat the cross-dst test, using Jan 1, 2005 and every interval from 1..364.
292 foreach my $i (1..364)
293   {
294     push @Tests, ["cross-dst$i",
295                   "-d'2005-01-01 +$i day'", '+%Y', {OUT=>"2005"},
296                   {ENV_DEL => 'TZ'},
297                   ];
298   }
299
300 # Append "\n" to each OUT=> RHS if the expected exit value is either
301 # zero or not specified (defaults to zero).
302 foreach my $t (@Tests)
303   {
304     my $exit_val;
305     foreach my $e (@$t)
306       {
307         ref $e && ref $e eq 'HASH' && defined $e->{EXIT}
308           and $exit_val = $e->{EXIT};
309       }
310     foreach my $e (@$t)
311       {
312         ref $e && ref $e eq 'HASH' && defined $e->{OUT} && ! $exit_val
313           and $e->{OUT} .= "\n";
314       }
315   }
316
317 my $save_temps = $ENV{DEBUG};
318 my $verbose = $ENV{VERBOSE};
319
320 my $prog = 'date';
321 my $fail = run_tests ($ME, $prog, \@Tests, $save_temps, $verbose);
322 exit $fail;
323 EOF