File: time.t

package info (click to toggle)
libdatetime-format-iso8601-perl 0.17-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 400 kB
  • sloc: perl: 1,092; sh: 23; makefile: 2
file content (202 lines) | stat: -rw-r--r-- 6,060 bytes parent folder | download | duplicates (3)
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
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_time = '23:20:50';

my %tests = (

    # These formats are unambiguous and are parsed as times by parse_datetime
    parse_datetime => [
        [qw( hh:mm:ss 23:20:50 )],
        [qw( hh:mm 23:20 23:20:00 )],
        [ 'hhmmss,ss',   '232050,5',   { nanosecond => 500_000_000 } ],
        [ 'hh:mm:ss,ss', '23:20:50,5', { nanosecond => 500_000_000 } ],
        [ 'hhmm,mm',     '2320,8',     '23:20:48' ],
        [ 'hh:mm,mm',    '23:20,8',    '23:20:48' ],
        [ 'hh,hh',       '23,3',       '23:18:00' ],
        [qw( -mm:ss -20:50 00:20:50 )],
        [ '-mmss,s',  '-2050,5',  '00:20:50', { nanosecond => 500_000_000 } ],
        [ '-mm:ss,s', '-20:50,5', '00:20:50', { nanosecond => 500_000_000 } ],
        [ '-mm,m',    '-20,8',    '00:20:48' ],
        [ '--ss,s',   '--50,5',   '00:00:50', { nanosecond => 500_000_000 } ],
        [ qw( hhmmssZ 232050Z ), { time_zone => 'UTC' } ],
        [
            qw( hhmmss.ssZ 232050.5Z ),
            {
                nanosecond => 500_000_000,
                time_zone  => 'UTC',
            }
        ],
        [qw( hh:mm:ssZ 23:20:50Z )],
        [
            qw( hhmmssZ 23:20:50.5Z ),
            {
                nanosecond => 500_000_000,
                time_zone  => 'UTC',
            }
        ],
        [ qw( hhmmZ 2320Z 23:20:00 ),   { time_zone => 'UTC' } ],
        [ qw( hh:mmZ 23:20Z 23:20:00 ), { time_zone => 'UTC' } ],
        [ qw( hhZ 23Z 23:00:00 ),       { time_zone => 'UTC' } ],
        [
            qw( hhmmss[+/-]hhmm 152746+0100 15:27:46 ),
            { time_zone => '+0100' }
        ],
        [
            qw( hhmmss[+/-]hhmm 152746-0500 15:27:46 ),
            { time_zone => '-0500' }
        ],
        [
            qw( hh:mm:ss[+/-]hhmm 15:27:46+01:00 15:27:46 ),
            { time_zone => '+0100' }
        ],
        [
            qw( hh:mm:ss[+/-]hhmm 15:27:46-05:00 15:27:46 ),
            { time_zone => '-0500' }
        ],
        [ qw( hhmmss[+/-]hh 152746+01 15:27:46 ), { time_zone => '+0100' } ],
        [ qw( hhmmss[+/-]hh 152746-05 15:27:46 ), { time_zone => '-0500' } ],
        [
            qw( hh:mm:ss[+/-]hh 15:27:46+01 15:27:46 ),
            { time_zone => '+0100' }
        ],
        [
            qw( hh:mm:ss[+/-]hh 15:27:46-05 15:27:46 ),
            { time_zone => '-0500' }
        ],
        [
            qw( hhmmss.ss[+/-]hhmm 152746.5+0100 15:27:46 ),
            {
                nano_second => 500_000_000,
                time_zone   => '+0100',
            }
        ],
        [
            qw( hhmmss.ss[+/-]hhmm 152746.5-0500 15:27:46 ),
            {
                nano_second => 500_000_000,
                time_zone   => '-0500',
            }
        ],
        [
            qw( hh:mm:ss.ss[+/-]hh:mm 15:27:46.5+01:00 15:27:46 ),
            {
                nano_second => 500_000_000,
                time_zone   => '+0100',
            }
        ],
        [
            qw( hh:mm:ss.ss[+/-]hh:mm 15:27:46.5-05:00 15:27:46 ),
            {
                nano_second => 500_000_000,
                time_zone   => '-0500',
            }
        ],
    ],

    # These formats are ambiguous and would be parsed as dates by parse_datetime.
    parse_time => [
        [qw( hhmmss 232050 )],
        [qw( hhmm 2320 23:20:00 )],
        [qw( hh 23 23:00:00 )],
        [qw( -mmss -2050 00:20:50 )],
        [qw( -mm -20 00:20:00 )],
        [qw( --ss --50 00:00:50 )],
    ],
);

subtest(
    'time formats with base_datetime' => sub {
        my $iso8601
            = DateTime::Format::ISO8601->new( base_datetime => $base_dt );
        for my $method ( sort keys %tests ) {
            for my $t ( @{ $tests{$method} } ) {
                my @copy   = @{$t};
                my $format = shift @copy;
                subtest(
                    $format => sub {
                        _test_time( $iso8601, $method, @copy );
                    }
                );
            }
        }
    }
);

subtest(
    'time formats without base_datetime' => sub {
        my $epoch
            = DateTime->new( year => 2000, month => 1, time_zone => 'UTC' )
            ->epoch;

        no warnings 'redefine';
        ## no critic (Variables::ProtectPrivateVars)
        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 $method ( sort keys %tests ) {
                        for my $t ( @{ $tests{$method} } ) {
                            my @copy   = @{$t};
                            my $format = shift @copy;
                            subtest(
                                $format => sub {
                                    _test_time( $iso8601, $method, @copy );
                                }
                            );
                        }
                    }
                }
            );
        }
    }
);

sub _test_time {
    my $parser   = shift;
    my $method   = shift;
    my $to_parse = shift;
    my @rest     = @_;

    my $expect = $default_expect_time;
    if ( @rest && !ref $rest[0] ) {
        $expect = shift @rest;
    }
    my $extra = shift @rest;

    my $dt = $parser->$method($to_parse);

    is(
        $dt->hms,
        $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();