1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
|
use strict;
use warnings;
use Test2::V0;
use DateTime::Format::ISO8601;
my $base_year = 2000;
my $base_month = '01';
my $base_dt = DateTime->new( year => $base_year, month => $base_month );
my $default_expect_datetime = '1985-04-12T10:15:30';
my @tests = (
[qw( YYYYMMDDThhmmss 19850412T101530 )],
[qw( YYYY-MM-DDThh:mm:ss 1985-04-12T10:15:30 )],
[
qw( YYYYMMDDThhmmss.ss 19850412T101530.5 ),
{ nanosecond => 500_000_000 }
],
[
qw( YYYY-MM-DDThh:mm:ss.ss 1985-04-12T10:15:30.5 ),
{ nanosecond => 500_000_000 }
],
[ qw ( YYYYMMDDThhmmssZ 19850412T101530Z ), { time_zone => 'UTC' } ],
[
qw( YYYY-MM-DDThh:mm:ssZ 1985-04-12T10:15:30Z ),
{ time_zone => 'UTC' }
],
[
qw( YYYYMMDDThhmmss.ssZ 19850412T101530.5Z ),
{
nanosecond => 500_000_000,
time_zone => 'UTC',
}
],
[
qw( YYYY-MM-DDThh:mm:ss.ssZ 1985-04-12T10:15:30.5Z ),
{
nanosecond => 500_000_000,
time_zone => 'UTC',
}
],
[
qw( YYYYMMDDThhmmss+hhmm 19850412T101530+0400 ),
{ time_zone => '+0400' }
],
[
qw( YYYY-MM-DDThh:mm+hh:mm 1985-04-12T10:15+04:00 1985-04-12T10:15:00 ),
{ time_zone => '+0400' }
],
[
qw( YYYY-MM-DDThh:mm+hh:mm 1985-04-12T10:15-04:00 1985-04-12T10:15:00 ),
{ time_zone => '-0400' }
],
[
qw( YYYYMMDDThhmm+hhmm 19850412T1015+0400 1985-04-12T10:15:00 ),
{ time_zone => '+0400' }
],
[
qw( YYYYMMDDThhmm+hhmm 19850412T1015-0400 1985-04-12T10:15:00 ),
{ time_zone => '-0400' }
],
[
qw( YYYY-MM-DDThh:mm:ss+hh:mm 1985-04-12T10:15:30+04:00 ),
{ time_zone => '+0400' }
],
[
qw( YYYY-MM-DDThh:mm:ss.ss+hh 1985-04-12T10:15:30.5+04 ),
{
nanosecond => 500_000_000,
time_zone => '+0400',
}
],
[
qw( YYYYMMDDThhmmss.ss+hh 19850412T101530.5+04 ),
{
nanosecond => 500_000_000,
time_zone => '+0400',
}
],
[
qw( YYYYMMDDThhmmss.ss+hhmm 19850412T101530.5+0400 ),
{
nanosecond => 500_000_000,
time_zone => '+0400',
}
],
[
qw( YYYY-MM-DDThh:mm:ss.ss+hh:mm 1985-04-12T10:15:30.5+04:00 ),
{
nanosecond => 500_000_000,
time_zone => '+0400',
}
],
[ qw( YYYYMMDDThhmmss+hh 19850412T101530+04 ), { time_zone => '+0400' } ],
[
qw( YYYY-MM-DDThh:mm:ss+hh 1985-04-12T10:15:30+04 ),
{ time_zone => '+0400' }
],
[qw( YYYYMMDDThhmm 19850412T1015 1985-04-12T10:15:00 )],
[qw( YYYY-MM-DDThh:mm 1985-04-12T10:15 1985-04-12T10:15:00 )],
[qw( YYYYMMDDThhmmZ 19850412T1015Z 1985-04-12T10:15:00 )],
[qw( YYYY-MM-DDThh:mmZ 1985-04-12T10:15Z 1985-04-12T10:15:00 )],
[qw( YYYYDDDThhmm 1985102T1015Z 1985-04-12T10:15:00 )],
[qw( YYYY-DDDThh:mm 1985-102T10:15Z 1985-04-12T10:15:00 )],
[
qw( YYYYDDDThhmmZ 1985102T1015Z 1985-04-12T10:15:00 ),
{ time_zone => 'UTC' }
],
[
qw( YYYY-DDDThh:mmZ 1985-102T10:15Z 1985-04-12T10:15:00 ),
{ time_zone => 'UTC' }
],
[
qw( YYYYWwwDThhmm+hhmm 1985W155T1015+0400 1985-04-12T10:15:00 ),
{ time_zone => '+0400' }
],
[
qw( YYYY-Www-DThh:mm+hh 1985-W15-5T10:15+04 1985-04-12T10:15:00 ),
{ time_zone => '+0400' }
],
[
# Expect for DT GH #145 HGuillemet = DTFI GH #22
qw( YYYYMMDDThhmmss.ssZ 2025-02-17T11:14:00.065341560Z ),
'2025-02-17T11:14:00',
{ nanosecond => 65_341_560, time_zone => 'UTC', },
],
);
subtest(
'datetime formats with base_datetime' => sub {
my $iso8601
= DateTime::Format::ISO8601->new( base_datetime => $base_dt );
for my $t (@tests) {
my @copy = @{$t};
my $format = shift @copy;
subtest(
$format => sub {
_test_time( $iso8601, @copy );
}
);
}
}
);
subtest(
'datetime formats without base_datetime' => sub {
my $epoch
= DateTime->new( year => 2000, month => 1, time_zone => 'UTC' )
->epoch;
## no critic (Variables::ProtectPrivateVars)
no warnings 'redefine';
local *DateTime::_core_time = sub {$epoch};
my $iso8601 = DateTime::Format::ISO8601->new;
for my $parser ( $iso8601, 'DateTime::Format::ISO8601' ) {
my $st = 'parse with ' . ( ref $parser ? 'object' : 'class' );
subtest(
$st,
sub {
for my $t (@tests) {
my @copy = @{$t};
my $format = shift @copy;
subtest(
$format => sub {
_test_time( $iso8601, @copy );
}
);
}
}
);
}
}
);
sub _test_time {
my $parser = shift;
my $to_parse = shift;
my @rest = @_;
my $expect = $default_expect_datetime;
if ( @rest && !ref $rest[0] ) {
$expect = shift @rest;
}
my $extra = shift @rest;
my $dt = $parser->parse_datetime($to_parse);
is(
$dt->datetime,
$expect,
"$to_parse = $expect",
);
return unless $extra;
if ( $extra->{nanosecond} ) {
is(
$dt->nanosecond,
$extra->{nanosecond},
"nanosecond = $extra->{nanosecond}",
);
}
if ( $extra->{time_zone} ) {
is(
$dt->time_zone->name, $extra->{time_zone},
"tz = $extra->{time_zone}"
);
}
}
done_testing();
|