Debian Codegraph @

t/cpanrt-parse.t

raw
1 use strict;
2 use warnings;
3 use Test::More tests => 26;
4 use Date::Parse qw(strptime str2time);
5
6 # RT#48164: Date::Parse unable to set seconds correctly
7 {
8 for my $str ("2008.11.30 22:35 CET", "2008-11-30 22:35 CET") {
9 my @t = strptime($str);
10 my $t = join ":", map { defined($_) ? $_ : "-" } @t;
11 is($t, "-:35:22:30:10:108:3600:20", "RT#48164: seconds parsing for '$str'");
12 }
13 }
14
15 # RT#51664: Change in str2time behaviour between 1.16 and 1.19
16 {
17 ok(str2time('16 Oct 09') >= 0, "RT#51664: '16 Oct 09' parses to non-negative time");
18 }
19
20 # RT#84075: Date::Parse::str2time maps date in 1963 to 2063
21 {
22 my $this_year = 1900 + (gmtime(time))[5];
23 my $target_year = $this_year - 50;
24 my $date = "$target_year-01-01 00:00:00 UTC";
25 my $time = str2time($date);
26 my $year_parsed_as = 1900 + (gmtime($time))[5];
27 is($year_parsed_as, $target_year, "RT#84075: year $target_year not mapped to future");
28 }
29
30 # RT#70650: Date::Parse should not parse ludicrous strings like bare numbers
31 {
32 ok(!defined str2time('1'), "RT#70650: str2time('1') returns undef");
33 ok(!defined str2time('+01'), "RT#70650: str2time('+01') returns undef");
34 ok(!defined str2time('+0500'), "RT#70650: str2time('+0500') returns undef");
35 }
36
37 # RT#53413 / RT#105031 (GH#17): Date::Parse mangling 4-digit year dates
38 # str2time() must not map 4-digit pre-1970 years to future dates.
39 # The root cause: strptime() extracts a 2-digit year (subtracting 1900 from
40 # the 4-digit value) and stores the century separately. str2time() must
41 # reconstruct the full 4-digit year before calling Time::Local, whose
42 # 2-digit-year windowing would otherwise map e.g. year 24 (from 1924) to 2024,
43 # or year 65 (from 1965) to 2065.
44 {
45 my @cases = (
46 [ "1924-01-15 00:00:00 UTC", 1924, "year 1924 does not map to 2024" ],
47 [ "1963-06-16 00:00:00 UTC", 1963, "year 1963 does not map to 2063" ],
48 [ "1965-12-31 00:00:00 UTC", 1965, "year 1965 does not map to 2065" ],
49 [ "1966-01-01 00:00:00 UTC", 1966, "year 1966 does not map to future" ],
50 [ "1901-12-17 00:00:00 UTC", 1901, "year 1901 parses correctly" ],
51 [ "1935-01-24 00:00:00 UTC", 1935, "year 1935 does not map to future" ],
52 );
53
54 for my $c (@cases) {
55 my ($date, $expected_year, $desc) = @$c;
56 my $t = str2time($date);
57 if (!defined $t) {
58 fail("RT#53413: str2time('$date') returned undef");
59 next;
60 }
61 my $parsed_year = 1900 + (gmtime($t))[5];
62 is($parsed_year, $expected_year, "RT#53413: $desc");
63 }
64
65 # strptime() must return year as offset from 1900 with century captured separately
66 my @t = strptime("1924-01-15 00:00:00 UTC");
67 is($t[5], 24, "RT#53413: strptime year field is 24 for 1924 (offset from 1900)");
68 is($t[7], 19, "RT#53413: strptime century field is 19 for 1924");
69 }
70
71 # RT#92611: str2time should pick the most recent occurrence when no year given.
72 # A future month means the most recent occurrence was last year.
73 {
74 my @lt = localtime(time);
75 my $cur_month = $lt[4]; # 0-11
76 my $cur_year = 1900 + $lt[5];
77 my @months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
78
79 SKIP: {
80 # December → January crosses a year boundary; the heuristic is ambiguous there.
81 skip "RT#92611: skipping in December (year-boundary edge case)", 1
82 if $cur_month == 11;
83
84 my $future_month = $cur_month + 1; # guaranteed valid (0-10 → 1-11)
85 my $future_name = $months[$future_month];
86 my $t = str2time("15 $future_name");
87 my $got_year = 1900 + (localtime($t))[5];
88 is($got_year, $cur_year - 1,
89 "RT#92611: '15 $future_name' with no year resolves to previous year (most recent occurrence)");
90 }
91 }
92
93 # RT#53267 / GH#2: strptime('MONTH YEAR') puts year into $day
94 # 'December 2009' should give month=11, year=109 (2009-1900), day=undef
95 # not month=11, day=2009, year=undef
96 {
97 my ($ss,$mm,$hh,$day,$month,$year,$zone) = strptime('December 2009');
98 is($month, 11, "RT#53267: 'December 2009' gives month=11 (December)");
99 ok(!defined($day), "RT#53267: 'December 2009' gives day=undef (not 2009)");
100 is($year, 109, "RT#53267: 'December 2009' gives year=109 (2009-1900)");
101
102 # 4-digit year in same position
103 ($ss,$mm,$hh,$day,$month,$year,$zone) = strptime('Jan 1995');
104 is($month, 0, "RT#53267: 'Jan 1995' gives month=0 (January)");
105 ok(!defined($day), "RT#53267: 'Jan 1995' gives day=undef");
106 is($year, 95, "RT#53267: 'Jan 1995' gives year=95 (1995-1900)");
107
108 # normal 'MONTH DAY' must still work
109 ($ss,$mm,$hh,$day,$month,$year,$zone) = strptime('December 25');
110 is($month, 11, "RT#53267: 'December 25' still gives month=11");
111 is($day, 25, "RT#53267: 'December 25' still gives day=25");
112 }
113
114 # RT#125949: strptime returns negative month for certain inputs
115 {
116 my @t = strptime("199001");
117 my $month = $t[4];
118 ok(!defined($month) || $month >= 0,
119 "RT#125949: strptime('199001') month is not negative");
120
121 my $t = str2time("199001");
122 ok(!defined($t),
123 "RT#125949: str2time('199001') returns undef for ambiguous 6-digit input");
124 }