Debian Codegraph @

t/date.t

raw symbols SCIP
1 #!/usr/bin/perl -w
2 use strict;
3 use lib 'inc';
4 use Test::More tests => 131;
5
6 my $class;
7 BEGIN {
8 $class = 'DateTime::Format::HTTP';
9 use_ok $class;
10 }
11
12 require Time::Local if $^O eq "MacOS";
13 my $offset = ($^O eq "MacOS") ? Time::Local::timegm(0,0,0,1,0,70) : 0;
14
15 # test str2time for supported dates. Test cases with 2 digit year
16 # will probably break in year 2044.
17 my(@tests) =
18 (
19
20 'Thu, 03 Feb 1994 00:00:00 GMT', # proposed new HTTP format
21 'Thursday, 03-Feb-94 00:00:00 GMT', # old rfc850 HTTP format
22 'Thursday, 03-Feb-1994 00:00:00 GMT', # broken rfc850 HTTP format
23
24 'Thu Feb 3 00:00:00 GMT 1994', # ctime format
25 'Thu Feb 3 00:00:00 1994', # same as ctime, except no TZ
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 {
64 my $time = (760233600 + $offset); # assume broken POSIX counting of seconds
65 $time = DateTime->from_epoch( epoch => $time );
66
67 for (@tests)
68 {
69 my $t = $class->parse_datetime($_, /GMT/i ? () : ('GMT'));
70 my $t2 = $class->parse_datetime(lc($_) => 'GMT' );
71 my $t3 = $class->parse_datetime(uc($_) => 'GMT' );
72
73 #diag "'$_' => $t";
74 if ($t->epoch != $time->epoch )
75 {
76 diag "difference is: ".($t->epoch - $time->epoch);
77 }
78
79 is ( $t->epoch, $time->epoch, "str2time (1): $_" );
80 is ( $t2->epoch, $time->epoch, "str2time (2): $_" );
81 is ( $t3->epoch, $time->epoch, "str2time (3): $_" );
82 }
83
84 # test time2str
85 die "time2str failed"
86 unless $class->format_datetime($time) eq 'Thu, 03 Feb 1994 00:00:00 GMT';
87 }
88
89 {
90 # test the 'ls -l' format with missing year$
91 # round to nearest minute 3 days ago.
92 my $time = int((time - 3 * 24*60*60) /60)*60;
93 my ($min, $hr, $mday, $mon) = (gmtime $time)[1,2,3,4];
94 $mon = (qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec))[$mon];
95 my $str = sprintf("$mon %02d %02d:%02d", $mday, $hr, $min);
96 my $t = $class->parse_datetime($str);
97 is( $t->epoch, $time ); #, "str2time ls -l: '$str' => $t ($time)\n");
98 }
99
100 for (undef, '', 'Garbage',
101 'Mandag 16. September 1996',
102 '1980-00-01',
103 '1980-13-01',
104 '1980-01-00',
105 '1980-01-32',
106 '1980-01-01 25:00:00',
107 '1980-01-01 00:61:00',
108 )
109 {
110 my $desc = defined $_ ? "'$_'" : "undef";
111 $desc .= ' does not parse';
112
113 my $ok = ! defined eval { $class->parse_datetime($_) };
114 ok( $ok, $desc );
115 }
116
117 my $conv = sub {
118 my $str = shift;
119 $class->format_iso( $class->parse_datetime( $str ) );
120 };
121
122 my $t;
123
124 $t = $conv->("11-12-96 0:00AM");
125 is($t => "1996-11-12 00:00:00", $t);
126
127 $t = $conv->("11-12-96 12:00AM");
128 is($t => "1996-11-12 00:00:00", $t);
129
130 $t = $conv->("11-12-96 0:00PM");
131 is($t => "1996-11-12 12:00:00", $t);
132
133 $t = $conv->("11-12-96 12:00PM");
134 is($t => "1996-11-12 12:00:00", $t);
135
136 $t = $conv->("11-12-96 1:05AM");
137 is($t => "1996-11-12 01:05:00", $t);
138
139 $t = $conv->("11-12-96 12:05AM");
140 is($t => "1996-11-12 00:05:00", $t);
141
142 $t = $conv->("11-12-96 1:05PM");
143 is($t => "1996-11-12 13:05:00", $t);
144
145 $t = $conv->("11-12-96 12:05PM");
146 is($t => "1996-11-12 12:05:00", $t);
147
148 my $dt = $class->parse_datetime("2000-01-01 00:00:01.234");
149 $t = $dt->epoch;
150 ok(
151 abs(($t - int($t)) - 0.234) > 0.000001,
152 "FRAC $t = ".$class->format_iso($dt)
153 );
154 is($dt->microsecond, 234_000, '.234s == 234_000us');
155 is($dt->nanosecond, 234_000_000, '.234s == 234_000_000ns');
156
157 $dt = $class->parse_datetime("2010-06-26T15:14:33.400753");
158 $t = $dt->epoch;
159 ok(
160 abs(($t - int($t)) - 0.400753) > 0.000001,
161 "FRAC $t = ".$class->format_iso($dt)
162 );
163 is($dt->microsecond, 400_753, '.400753s == 400_753us');
164 is($dt->nanosecond, 400_753_000, '.400753s == 400_753_000ns');
165
166 for my $ns (qw(1 12 123 1234 499999999 500000000 500000001 999753123 999999999)) {
167 $dt = $class->parse_datetime(sprintf("2010-06-26T15:14:33.%09d", $ns));
168 is($dt->nanosecond, $ns, ".${ns}s == ${ns}ns");
169 }
170
171 $a = $class->format_iso( );
172 $b = $class->format_iso( DateTime->from_epoch( epoch => 500000 ) );
173
174 my $az = $class->format_isoz( );
175 my $bz = $class->format_isoz( DateTime->from_epoch( epoch => 500000 ) );
176
177 for ($a, $b) {
178 like( $_ => qr/^\d{4}-\d\d-\d\d \d\d:\d\d:\d\d$/, "time2iso($_)" );
179 }
180 for ($az, $bz) {
181 like( $_ => qr/^\d{4}-\d\d-\d\d \d\d:\d\d:\d\dZ$/, "time2isoz($_)" );
182 }
183
184 {
185 # format_isoz must output date in UTC
186 my $eastern_date = DateTime->new(
187 year => 2010,
188 month => 10,
189 day => 21,
190 hour => 13,
191 minute => 8,
192 second => 23,
193 time_zone => 'America/New_York',
194 );
195
196 # Get the ISO "Z" format of the eastern zone date time
197 my $isoz = $class->format_isoz($eastern_date);
198
199 # Get the actual UTC date time
200 my $utc = $eastern_date->clone->set_time_zone('UTC');
201
202 is($isoz, $class->format_isoz($utc), 'format_isoz converts to UTC time zone');
203 is($eastern_date->time_zone->name, 'America/New_York', 'format_isoz does not modify input date\'s time zone');
204 }