Debian Codegraph @

t/edge-cases.t

raw
1 use strict;
2 use warnings;
3 use Test::More;
4 use Date::Parse qw(str2time strptime);
5 use Date::Language;
6
7 # --- Undef / empty / garbage inputs ---
8 ok(!defined str2time(undef), "str2time(undef) returns undef");
9 ok(!defined str2time(""), "str2time('') returns undef");
10 ok(!defined str2time("garbage"), "str2time('garbage') returns undef");
11 ok(!defined str2time("not a date at all"), "str2time('not a date at all') returns undef");
12
13 # --- ISO 8601 variants ---
14 {
15 my $t1 = str2time("2002-07-22T10:00:00Z");
16 ok(defined $t1, "ISO 8601 with T separator and Z");
17 is($t1, 1027332000, "ISO 8601 basic format parses correctly");
18
19 my $t2 = str2time("2002-07-22 10:00:00Z");
20 is($t2, 1027332000, "ISO 8601 with space separator and Z");
21
22 my $t3 = str2time("20020722T100000Z");
23 is($t3, 1027332000, "ISO 8601 compact format");
24
25 my $t4 = str2time("2001-02-26T13:44:12-0700");
26 is($t4, 983220252, "ISO 8601 with negative offset");
27
28 my $t5 = str2time("2002-11-07T23:31:49-05:00");
29 is($t5, 1036729909, "RFC 3339 with colon in offset");
30 }
31
32 # --- AM/PM parsing ---
33 {
34 my $base = str2time("Jul 13 1999 13:23:00 GMT");
35 is(str2time("Jul 13 1999 1:23P GMT"), $base, "1:23P parses as PM");
36 is(str2time("Jul 13 1999 1:23P.M GMT"), $base, "1:23P.M parses as PM");
37 is(str2time("Jul 13 1999 1:23P.M. GMT"), $base, "1:23P.M. parses as PM");
38
39 my $am = str2time("92/01/02 12:01 AM");
40 my $pm = str2time("92/01/02 12:01 PM");
41 ok(defined $am && defined $pm, "AM/PM dates parse");
42 cmp_ok($pm - $am, '==', 12 * 3600, "PM is 12 hours after AM");
43 }
44
45 # --- Fractional seconds ---
46 {
47 my $t = str2time("2003-02-17T08:14:07.198189+0000");
48 ok(defined $t, "fractional seconds parse");
49 cmp_ok(abs($t - 1045469647.198189), '<', 0.001, "fractional seconds preserved");
50
51 my $t2 = str2time("1995-01-24T09:08:17.1823213");
52 ok(defined $t2, "high-precision fractional seconds parse");
53 }
54
55 # --- Apache-style dates ---
56 {
57 my $t = str2time("07/Nov/2000:16:45:56 +0100");
58 is($t, 973611956, "Apache log date format");
59 }
60
61 # --- RFC 2822 / email dates ---
62 {
63 my $t = str2time("Wed, 9 Nov 1994 09:50:32 -0500 (EST)");
64 is($t, 784392632, "RFC 2822 date with comment");
65
66 my $t2 = str2time("Sat, 19 Nov 1994 16:59:14 +0100");
67 is($t2, 785260754, "RFC 2822 date with positive offset");
68 }
69
70 # --- Date::Language error handling ---
71 {
72 eval { Date::Language->new('NonexistentLanguage') };
73 ok($@, "Date::Language->new with invalid language dies");
74
75 my $fr = Date::Language->new('French');
76 ok(defined $fr, "Date::Language->new('French') succeeds");
77
78 my $ts = $fr->str2time('15 janvier 2010');
79 ok(defined $ts, "French 'janvier' parses");
80 }
81
82 # --- French month parsing ---
83 {
84 my $fr = Date::Language->new('French');
85 my @months = (
86 ['janvier', 1],
87 ['mars', 3],
88 ['juin', 6],
89 ['octobre', 10],
90 ['novembre', 11],
91 );
92 for my $pair (@months) {
93 my ($month_name, $month_num) = @$pair;
94 my $ts = $fr->str2time("15 $month_name 2010");
95 if (defined $ts) {
96 my @lt = localtime($ts);
97 is($lt[4] + 1, $month_num, "French '$month_name' -> month $month_num");
98 }
99 else {
100 fail("French '$month_name' failed to parse");
101 }
102 }
103 }
104
105 # --- Two-digit year handling ---
106 # Two-digit years: 69-99 -> 1969-1999, 0-68 -> 2000-2068 (POSIX convention)
107 {
108 my $t = str2time("16 Oct 09");
109 ok(defined $t, "two-digit year '09' parses");
110 cmp_ok($t, '>=', 0, "two-digit year '09' gives non-negative time");
111 my @gm = gmtime($t);
112 is($gm[5] + 1900, 2009, "two-digit year '09' maps to 2009");
113 }
114
115 # --- Two-digit years for 1970s/1980s dates (GH issue #47) ---
116 {
117 my $t74 = str2time("01 Jan 74 00:00:00 GMT");
118 ok(defined $t74, "two-digit year '74' parses");
119 is($t74, 126230400, "two-digit year '74' gives 1974-01-01 not 2074");
120
121 my $t73 = str2time("15 Jun 73 12:00:00 GMT");
122 ok(defined $t73, "two-digit year '73' parses");
123 my @gm73 = gmtime($t73);
124 is($gm73[5] + 1900, 1973, "two-digit year '73' maps to 1973");
125
126 my $t72 = str2time("01 Jan 72 00:00:00 GMT");
127 ok(defined $t72, "two-digit year '72' parses");
128 my @gm72 = gmtime($t72);
129 is($gm72[5] + 1900, 1972, "two-digit year '72' maps to 1972");
130
131 # Boundary: 69 -> 1969, 68 -> 2068
132 my $t69 = str2time("01 Jan 69 00:00:00 GMT");
133 ok(defined $t69, "two-digit year '69' parses");
134 my @gm69 = gmtime($t69);
135 is($gm69[5] + 1900, 1969, "two-digit year '69' maps to 1969 (boundary)");
136 }
137
138 # --- Time-only formats ---
139 {
140 my $t = str2time("10:00:00Z");
141 ok(defined $t, "time-only with Z parses");
142
143 my $t2 = str2time("10:00:00");
144 ok(defined $t2, "time-only without zone parses");
145
146 my $t3 = str2time("10:00");
147 ok(defined $t3, "time-only HH:MM parses");
148 }
149
150 # --- Various date separator styles ---
151 {
152 my $t1 = str2time("21 dec 17:05");
153 my $t2 = str2time("21-dec 17:05");
154 my $t3 = str2time("21/dec 17:05");
155 ok(defined $t1, "space-separated day month");
156 is($t1, $t2, "dash separator matches space separator");
157 is($t1, $t3, "slash separator matches space separator");
158 }
159
160 # --- Boost C++ timestamp format ---
161 {
162 my $t = str2time("2024-May-15 14:30:00.123456");
163 ok(defined $t, "boost format YYYY-Mon-DD HH:MM:SS.f parses");
164 }
165
166 # --- Leap day (Feb 29) handling ---
167 # When only a year is given, str2time fills in the current month/day.
168 # On Feb 29 the year must be a leap year or str2time returns undef.
169 # See: https://github.com/atoomic/perl-TimeDate/issues/28
170 {
171 # Explicit Feb 29 on a leap year must succeed
172 my $t1 = str2time("29 Feb 2000 10:02:18 GMT");
173 ok(defined $t1, "Feb 29 of leap year 2000 parses");
174 is($t1, 951818538, "Feb 29 2000 10:02:18 GMT matches expected epoch");
175
176 # Explicit Feb 29 on a non-leap year must fail
177 my $t2 = str2time("29 Feb 1999 10:02:18 GMT");
178 ok(!defined $t2, "Feb 29 of non-leap year 1999 returns undef");
179 }
180
181 # --- Comma as decimal separator in fractional seconds (ISO 8601) ---
182 {
183 my $dot = str2time("2016-01-28 23:27:13.995 UTC");
184 my $comma = str2time("2016-01-28 23:27:13,995 UTC");
185 ok(defined $dot, "dot decimal fractional seconds parse");
186 ok(defined $comma, "comma decimal fractional seconds parse");
187 cmp_ok(abs($dot - $comma), '<', 0.01,
188 "comma decimal gives same result as dot decimal");
189
190 my $t1 = str2time("2016-01-28 23:27:13,995 UTC");
191 my $t2 = str2time("2016-01-28 23:27:13,996 UTC");
192 cmp_ok($t2, '>', $t1,
193 "comma decimal: ,996 is later than ,995");
194 cmp_ok(abs($t2 - $t1 - 0.001), '<', 0.0001,
195 "comma decimal: difference is ~1ms");
196
197 # RFC 2822 commas still work
198 my $rfc = str2time("Wed, 16 Jun 94 07:29:35 CST");
199 is($rfc, 771773375, "RFC 2822 comma after day name still works");
200 }
201
202
203 # --- Year inference for dates without an explicit year (GH #46) ---
204 # When no year is given, str2time should assume the most recent occurrence:
205 # a date in the future (month > now, OR same month but day > today) gets
206 # previous year; a date in the past (or today) gets current year.
207 {
208 my @lt = localtime();
209 my ($cur_day, $cur_month, $cur_year) = @lt[3, 4, 5];
210 $cur_year += 1900;
211
212 my @mon_names = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
213
214 # A future month (strictly > current month): should infer previous year.
215 if ($cur_month < 11) {
216 my $future_mon = $mon_names[$cur_month + 1];
217 my $t = str2time("$future_mon 4 01:04:16");
218 ok(defined $t, "future month '$future_mon 4': parses");
219 my @res = localtime($t);
220 is($res[5] + 1900, $cur_year - 1,
221 "future month ($future_mon) → previous year (GH#46)");
222 }
223
224 # A past month (strictly < current month): should infer current year.
225 if ($cur_month > 0) {
226 my $past_mon = $mon_names[$cur_month - 1];
227 my $t = str2time("$past_mon 4 01:04:16");
228 ok(defined $t, "past month '$past_mon 4': parses");
229 my @res = localtime($t);
230 is($res[5] + 1900, $cur_year,
231 "past month ($past_mon) → current year (GH#46)");
232 }
233
234 # Same month, day strictly in the future → previous year.
235 if ($cur_day <= 27) {
236 my $future_day = $cur_day + 1;
237 my $mon = $mon_names[$cur_month];
238 my $t = str2time(sprintf("%s %d 01:04:16", $mon, $future_day));
239 ok(defined $t, "future day same month '$mon $future_day': parses");
240 my @res = localtime($t);
241 is($res[5] + 1900, $cur_year - 1,
242 "same month, future day ($mon $future_day) → previous year (GH#46)");
243 }
244
245 # Same month, day strictly in the past → current year.
246 if ($cur_day >= 2) {
247 my $past_day = $cur_day - 1;
248 my $mon = $mon_names[$cur_month];
249 my $t = str2time(sprintf("%s %d 01:04:16", $mon, $past_day));
250 ok(defined $t, "past day same month '$mon $past_day': parses");
251 my @res = localtime($t);
252 is($res[5] + 1900, $cur_year,
253 "same month, past day ($mon $past_day) → current year (GH#46)");
254 }
255 }
256
257 done_testing;