Debian lenny version packages
[pkg-perl] / deb-src / libwww-perl / libwww-perl-5.813 / t / base / date.t
1 use HTTP::Date;
2
3 require Time::Local if $^O eq "MacOS";
4 my $offset = ($^O eq "MacOS") ? Time::Local::timegm(0,0,0,1,0,70) : 0;
5
6 print "1..59\n";
7
8 $no = 1;
9 $| = 1;
10 sub ok {
11    print "not " if $_[0];
12    print "ok $no\n";
13    $no++;
14 }
15
16 # test str2time for supported dates.  Test cases with 2 digit year
17 # will probably break in year 2044.
18 my(@tests) =
19 (
20  'Thu Feb  3 00:00:00 GMT 1994',        # ctime format
21  'Thu Feb  3 00:00:00 1994',            # same as ctime, except no TZ
22
23  'Thu, 03 Feb 1994 00:00:00 GMT',       # proposed new HTTP format
24  'Thursday, 03-Feb-94 00:00:00 GMT',    # old rfc850 HTTP format
25  'Thursday, 03-Feb-1994 00:00:00 GMT',  # broken rfc850 HTTP format
26
27  '03/Feb/1994:00:00:00 0000',   # common logfile format
28  '03/Feb/1994:01:00:00 +0100',  # common logfile format
29  '02/Feb/1994:23:00:00 -0100',  # common logfile format
30
31  '03 Feb 1994 00:00:00 GMT',    # HTTP format (no weekday)
32  '03-Feb-94 00:00:00 GMT',      # old rfc850 (no weekday)
33  '03-Feb-1994 00:00:00 GMT',    # broken rfc850 (no weekday)
34  '03-Feb-1994 00:00 GMT',       # broken rfc850 (no weekday, no seconds)
35  '03-Feb-1994 00:00',           # VMS dir listing format
36
37  '03-Feb-94',    # old rfc850 HTTP format    (no weekday, no time)
38  '03-Feb-1994',  # broken rfc850 HTTP format (no weekday, no time)
39  '03 Feb 1994',  # proposed new HTTP format  (no weekday, no time)
40  '03/Feb/1994',  # common logfile format     (no time, no offset)
41
42  #'Feb  3 00:00',     # Unix 'ls -l' format (can't really test it here)
43  'Feb  3 1994',       # Unix 'ls -l' format
44
45  "02-03-94  12:00AM", # Windows 'dir' format
46
47  # ISO 8601 formats
48  '1994-02-03 00:00:00 +0000',
49  '1994-02-03',
50  '19940203',
51  '1994-02-03T00:00:00+0000',
52  '1994-02-02T23:00:00-0100',
53  '1994-02-02T23:00:00-01:00',
54  '1994-02-03T00:00:00 Z',
55  '19940203T000000Z',
56  '199402030000',
57
58  # A few tests with extra space at various places
59  '  03/Feb/1994      ',
60  '  03   Feb   1994  0:00  ',
61 );
62
63 my $time = (760233600 + $offset);  # assume broken POSIX counting of seconds
64 for (@tests) {
65     my $t;
66     if (/GMT/i) {
67         $t = str2time($_);
68     }
69     else {
70         $t = str2time($_, "GMT");
71     }
72     my $t2 = str2time(lc($_), "GMT");
73     my $t3 = str2time(uc($_), "GMT");
74
75     $t = "UNDEF" unless defined $t;
76     print "'$_'  =>  $t\n";
77     print $@ if $@;
78     print "not " if $t eq 'UNDEF' || $t  != $time
79                                   || $t2 != $time
80                                   || $t3 != $time;
81     ok;
82 }
83
84 # test time2str
85 die "time2str failed"
86     unless time2str($time) eq 'Thu, 03 Feb 1994 00:00:00 GMT';
87
88 # test the 'ls -l' format with missing year$
89 # round to nearest minute 3 days ago.
90 $time = int((time - 3 * 24*60*60) /60)*60;
91 ($min, $hr, $mday, $mon) = (localtime $time)[1,2,3,4];
92 $mon = (qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec))[$mon];
93 $str = sprintf("$mon %02d %02d:%02d", $mday, $hr, $min);
94 $t = str2time($str);
95 $t = "UNDEF" unless defined $t;
96 print "'$str'  =>  $t ($time)\n";
97 print "not " if $t != $time;
98 ok;
99
100 # try some garbage.
101 for (undef, '', 'Garbage',
102      'Mandag 16. September 1996',
103      '12 Arp 2003',
104 #     'Thu Feb  3 00:00:00 CET 1994',
105 #     'Thu, 03 Feb 1994 00:00:00 CET',
106 #     'Wednesday, 31-Dec-69 23:59:59 GMT',
107
108      '1980-00-01',
109      '1980-13-01',
110      '1980-01-00',
111      '1980-01-32',
112      '1980-01-01 25:00:00',
113      '1980-01-01 00:61:00',
114      '1980-01-01 00:00:61',
115     )
116 {
117     my $bad = 0;
118     eval {
119         if (defined str2time $_) {
120             print "str2time($_) is not undefined\n";
121             $bad++;
122         }
123     };
124     print defined($_) ? "'$_'\n" : "undef\n";
125     print $@ if $@;
126     print "not " if $bad;
127     ok;
128 }
129
130 print "Testing AM/PM gruff...\n";
131
132 # Test the str2iso routines
133 use HTTP::Date qw(time2iso time2isoz);
134
135 print "Testing time2iso functions\n";
136
137 $t = time2iso(str2time("11-12-96  0:00AM"));print "$t\n";
138 ok($t ne "1996-11-12 00:00:00");
139
140 $t = time2iso(str2time("11-12-96 12:00AM"));print "$t\n";
141 ok($t ne "1996-11-12 00:00:00");
142
143 $t = time2iso(str2time("11-12-96  0:00PM"));print "$t\n";
144 ok($t ne "1996-11-12 12:00:00");
145
146 $t = time2iso(str2time("11-12-96 12:00PM"));print "$t\n";
147 ok($t ne "1996-11-12 12:00:00");
148
149
150 $t = time2iso(str2time("11-12-96  1:05AM"));print "$t\n";
151 ok($t ne "1996-11-12 01:05:00");
152
153 $t = time2iso(str2time("11-12-96 12:05AM"));print "$t\n";
154 ok($t ne "1996-11-12 00:05:00");
155
156 $t = time2iso(str2time("11-12-96  1:05PM"));print "$t\n";
157 ok($t ne "1996-11-12 13:05:00");
158
159 $t = time2iso(str2time("11-12-96 12:05PM"));print "$t\n";
160 ok($t ne "1996-11-12 12:05:00");
161
162 $t = str2time("2000-01-01 00:00:01.234");
163 print "FRAC $t = ", time2iso($t), "\n";
164 ok(abs(($t - int($t)) - 0.234) > 0.000001);
165
166 $a = time2iso;
167 $b = time2iso(500000);
168 print "LOCAL $a  $b\n";
169 $az = time2isoz;
170 $bz = time2isoz(500000);
171 print "GMT   $az $bz\n";
172
173 for ($a,  $b)  { ok if /^\d{4}-\d\d-\d\d \d\d:\d\d:\d\d$/;  }
174 for ($az, $bz) { ok if /^\d{4}-\d\d-\d\d \d\d:\d\d:\d\dZ$/; }
175
176 # Test the parse_date interface
177 use HTTP::Date qw(parse_date);
178
179 @d = parse_date("Jan 1 2001");
180
181 print "not " if defined(pop(@d)) ||
182                 "@d" ne "2001 1 1 0 0 0";
183 ok;
184
185 # This test will break around year 2070
186 print "not " unless parse_date("03-Feb-20") eq "2020-02-03 00:00:00";
187 ok;
188
189 # This test will break around year 2048
190 print "not " unless parse_date("03-Feb-98") eq "1998-02-03 00:00:00";
191 ok;
192
193 print "HTTP::Date $HTTP::Date::VERSION\n";