blib/lib/Astro/App/Satpass2/ParseTime/ISO8601.pm

filter: Statementstmt 64.8% 70 / 108 Branchbran 53.7% 29 / 54 Conditioncond 61.9% 13 / 21 MC/DCmcdc 41.6% 5 / 12 Subroutinesub 64.7% 11 / 17 Podpod 100% 5 / 5 totaltotal 61.2% 133 / 217 CC 37 SCAR 25.0
CC
37
Cov
61%
__ANON__
20.0
_interpret_zone
18.2
parse_time_absolute
17.8
CRAP 12.2
« blib/lib/Astro/App/Satpass2/ParseTime.pm ↑ Summary blib/lib/Astro/App/Satpass2/Utils.pm »
1 package Astro::App::Satpass2::ParseTime::ISO8601;
2  
3 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 9 ▶use Astro::App::Satpass2::Utils qw{
Subroutine BEGIN: called CC=1 SCAR=0.0
7 back_end __back_end_class_name_of_record __parse_class_and_args
8 HAVE_DATETIME
9 @CARP_NOT
10 9 };
11 9 ▶use Astro::Coord::ECI::Utils 0.112 qw{
Subroutine BEGIN: called CC=1 SCAR=0.0
12 9 looks_like_number SECSPERDAY greg_time_gm greg_time_local };
13  
14 9 ▶use parent qw{ Astro::App::Satpass2::ParseTime };
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 9 our $VERSION = '0.059';
17  
18 9 my $package = __PACKAGE__;
19  
20 14 ▶sub attribute_names {
Subroutine attribute_names: called CC=1 SCAR=0.0
Pod documented
21 14 my ( $self ) = @_;
22 14 return ( $self->SUPER::attribute_names(), qw{ back_end } );
23 }
24  
25 0 ▶sub class_name_of_record {
Subroutine class_name_of_record: not called CC=1 SCAR=6.9
Pod documented
26 0 my ( $self ) = @_;
27 0 return $self->__back_end_class_name_of_record(
28 $self->SUPER::class_name_of_record() );
29 }
30  
31 9 my $zone_re = qr{ (?i: ( Z | UT | GMT ) |
32 ( [+-] ) ( [0-9]{1,2} ) :? ( [0-9]{1,2} )? ) }smx;
33  
34 18 ▶sub delegate {
Subroutine delegate: called CC=1 SCAR=0.0
Pod documented
35 18 return __PACKAGE__;
36 }
37  
38 {
39  
40 9 my %special_day_offset = (
41 yesterday => -SECSPERDAY(),
42 today => 0,
43 tomorrow => SECSPERDAY(),
44 );
45  
46 0 ▶ my $era_ad = sub { return $_[0] };
Subroutine __ANON__: not called CC=1 SCAR=1.2
47 0 ▶ my $era_bc = sub { return 1 - $_[0] };
Subroutine __ANON__: not called CC=1 SCAR=1.2
48 9 my %era_cvt = (
49 AD => $era_ad,
50 BC => $era_bc,
51 BCE => $era_bc,
52 CE => $era_ad,
53 );
54  
55 # Note that we have to reverse sort the keys because otherwise 'BC'
56 # gets matched before we have a chance to try 'BCE'.
57 9 my $era_re = qr< (?: @{[
58 9 join ' | ', reverse sort keys %era_cvt
59 ]} ) >smxi;
60  
61 0 ▶ my $make_epoch = HAVE_DATETIME ? sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
62 0 my ( $self, $zone, $offset, @date ) = @_;
63 0 ▶ $zone ||= 'local';
Condition0%0/2
$zone ||= "local"
l!l
00
MC/DC0%0/1
$zone ||= "local"
$zone
Truth table0%0/2
A
A= $zone
Aresult
11
00
64 0 ▶ if ( defined( my $special = $special_day_offset{$date[0]} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $special = $special_day_offset{$date[0]})) 0 0 0
65 0 my $dt = DateTime->today(
66 time_zone => $zone,
67 );
68 0 splice @date, 0, 3, $dt->year(), $dt->month(), $dt->day();
69 0 $offset += $special;
70 }
71 0 my %dt_arg;
72 0 @dt_arg{ qw<
73 year month day hour minute second nanosecond
74 > } = @date;
75 0 $dt_arg{nanosecond} *= 1_000_000_000;
76 0 $dt_arg{time_zone} = $zone;
77 $self->{_back_end}
78 and return $self->{_back_end}{class}->new(
79 %dt_arg,
80 0 ▶ @{ $self->{_back_end}{arg} },
Branch: 1 branch
BranchTrueFalseTotal
$self->{"_back_end"} and return $self->{"_back_end"}{"class"}->new(%dt_arg, @{$$self{"_back_end"}{"arg"};})->epoch + $offset 0 0 0
81 )->epoch() + $offset;
82 0 return DateTime->new( %dt_arg )->epoch() + $offset;
83 90 ▶ } : sub {
Subroutine __ANON__: called CC=5 SCAR=20.4
84 90 my ( undef, $zone, $offset, @date ) = @_;
85 90 ▶ if ( defined( my $special = $special_day_offset{$date[0]} )
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $special = $special_day_offset{$date[0]})) { } 0 90 90
86 ) {
87 0 ▶ my @today = $zone ? gmtime : localtime;
Branch: 1 branch
BranchTrueFalseTotal
$zone ? : 0 0 0
88 0 splice @date, 0, 3, @today[ 5, 4, 3 ];
89 0 $date[0] += 1900;
90 0 $offset += $special;
91 } else {
92 90 --$date[1];
93 }
94 90 $offset += pop @date;
95 90 ▶ if ( defined $zone ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $zone) { } 63 27 90
96 63 ▶ 'UTC' eq $zone
Branch: 1 branch
BranchTrueFalseTotal
"UTC" eq $zone and return greg_time_gm(reverse(@date)) + $offset 63 0 63
97 and return greg_time_gm( reverse @date ) + $offset;
98 0 local $ENV{TZ} = $zone; # The best we can do.
99 0 return greg_time_local( reverse @date ) + $offset;
100 } else {
101 27 return greg_time_local( reverse @date ) + $offset;
102 }
103 9 };
104  
105 90 ▶ sub parse_time_absolute {
Subroutine parse_time_absolute: called CC=15 SCAR=28.8
Pod documented
106 90 my ( $self, $string ) = @_;
107  
108 90 my @date;
109  
110 my $special_only;
111  
112 # ISO 8601 date
113 90 ▶ if ( $string =~ m< \A
Branch: 2 branches
BranchTrueFalseTotal
if ($string =~ / \A ( ( [0-9]+ ) \s* ( $era_re ) [^0-9]? | # year $1, $2 era $3 [0-9]{4} [^0-9]? | [0-9]+ [^0-9] ) (?: ( [0-9]{1,2} ) [^0-9]? # month: $4 (?: ( [0-9]{1,2} ) [^0-9]? # day: $5 )? )? /gmsx) { } 90 0 90
elsif ($string =~ / \A ( yesterday | today | tomorrow ) \b [^0-9]? # day: $1 /gimsx) { } 0 0 0
114 ( ( [0-9]+ ) \s* ( $era_re ) [^0-9]? | # year $1, $2 era $3
115 [0-9]{4} [^0-9]? |
116 [0-9]+ [^0-9] )
117 (?: ( [0-9]{1,2} ) [^0-9]? # month: $4
118 (?: ( [0-9]{1,2} ) [^0-9]? # day: $5
119 )?
120 )?
121 >smxg ) {
122  
123 90 ▶ if ( $3 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($3) { } 0 90 90
124 0 @date = ( $era_cvt{ uc $3 }->( $2 + 0 ), $4, $5 );
125 } else {
126 90 @date = ( $1, $4, $5 );
127 90 $date[0] =~ s/ [^0-9] \z //smx;
128 90 ▶ $date[0] < 70
Branch: 1 branch
BranchTrueFalseTotal
$date[0] < 70 and $date[0] += 2000 10 80 90
129 and $date[0] += 2000;
130 90 ▶ $date[0] < 100
Branch: 1 branch
BranchTrueFalseTotal
$date[0] < 100 and $date[0] += 1900 0 90 90
131 and $date[0] += 1900;
132 }
133  
134 90 ▶ defined $date[1]
Branch: 1 branch
BranchTrueFalseTotal
defined $date[1] or $date[1] = 1 86 4 90
135 or $date[1] = 1;
136 90 ▶ defined $date[2]
Branch: 1 branch
BranchTrueFalseTotal
defined $date[2] or $date[2] = 1 78 12 90
137 or $date[2] = 1;
138  
139 # special-case 'yesterday', 'today', and 'tomorrow'.
140 } elsif ( $string =~ m{ \A
141 ( yesterday | today | tomorrow ) \b [^0-9]? # day: $1
142 }smxgi ) {
143 # Handle this when we make the epoch, since we do not yet
144 # know the zone.
145 0 @date = ( lc $1, 0, 0 );
146 0 $special_only = 1;
147  
148 } else {
149  
150 0 return;
151  
152 }
153  
154 90 ▶ if ( $string =~ m< \G
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ / \G ( [0-9]{1,2} ) [^0-9+-]? # hour: $1 (?: ( [0-9]{1,2} ) [^0-9+-]? # minute: $2 (?: ( [0-9]{1,2} ) [^0-9+-]? # second: $3 ( [0-9]* ) # fract: $4 )? )? /cgmsx) { } 70 20 90
155 ( [0-9]{1,2} ) [^0-9+-]? # hour: $1
156 (?: ( [0-9]{1,2} ) [^0-9+-]? # minute: $2
157 (?: ( [0-9]{1,2} ) [^0-9+-]? # second: $3
158 ( [0-9]* ) # fract: $4
159 )?
160 )?
161 >smxgc ) {
162 70 ▶ push @date, $1, $2 || 0, $3 || 0, $4 ? ".$4" : 0;
Branch: 1 branch
BranchTrueFalseTotal
$4 ? : 0 70 70
Condition100%4/4
$2 || 0
l!l
628
$3 || 0
l!l
5416
MC/DC100%1/1
$2 || 0
$2
MC/DC100%1/1
$3 || 0
$3
Truth table100%2/2
A
A= $2
Aresult
11
00
Truth table100%2/2
A
A= $3
Aresult
11
00
163 70 $special_only = 0;
164 } else {
165 20 push @date, ( 0 ) x 4;
166 }
167  
168 # We might have gobbled part of the zone.
169 90 ▶ not $special_only
Branch: 1 branch
BranchTrueFalseTotal
not $special_only and $string =~ / \G (?<= [^0-9] ) /cgmsx and pos $string -= 1 58 32 90
Condition66%2/3
not $special_only and $string =~ / \G (?<= [^0-9] ) /cgmsx
!ll&&!rl&&r
03258
MC/DC50%1/2
not $special_only and $string =~ / \G (?<= [^0-9] ) /cgmsx
not $special_only $string =~ / \G (?<= [^0-9] ) /cgmsx
Truth table66%2/3
not(A) and B
A= not $special_onlyB= $string =~ / \G (?<= [^0-9] ) /cgmsx
ABresult
0X0
100
111
170 and $string =~ m/ \G (?<= [^0-9] ) /smxgc
171 and pos $string -= 1;
172 90 my ( $zone ) = $string =~ m/ \G ( .* ) /smxgc;
173  
174 90 my ( $z, $offset ) = $self->_interpret_zone( $zone );
175 90 ▶ defined $offset
Branch: 1 branch
BranchTrueFalseTotal
defined $offset or return 90 0 90
176 or return;
177  
178 90 return $make_epoch->( $self, $z, $offset, @date );
179 }
180  
181 }
182  
183 9 ▶sub _interpret_zone {
Subroutine _interpret_zone: called CC=12 SCAR=29.0
184 90 my ( $self, $zone, $fatal ) = @_;
185 90 ▶ defined $zone
Branch: 1 branch
BranchTrueFalseTotal
defined $zone and $zone =~ s/ \A \s+ //msx 90 0 90
186 and $zone =~ s/ \A \s+ //smx;
187 $zone
188 90 ▶ or return ( @{ $self->{$package}{tz} || [ undef, 0 ] } );
Branch: 1 branch
BranchTrueFalseTotal
$zone or return @{$self->{$package}{"tz"} or [undef, 0];} 63 27 90
Condition50%1/2
$self->{$package}{'tz'} || [undef, 0]
l!l
027
MC/DC0%0/1
$self->{$package}{'tz'} || [undef, 0]
$self->{$package}{'tz'}
Truth table50%1/2
A
A= $self->{$package}{'tz'}
Aresult
11
00
189 63 ▶ if ( $zone =~ m/ \A $zone_re \z /smxo ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($zone =~ / \A $zone_re \z /mosx) { } 63 0 63
190 63 ▶ $1
Branch: 1 branch
BranchTrueFalseTotal
$1 and return "UTC", 0 58 5 63
191 and return ( UTC => 0 );
192 5 ▶ my $offset = ( ( $3 || 0 ) * 60 + ( $4 || 0 ) ) * 60;
Condition75%3/4
$3 || 0
l!l
50
$4 || 0
l!l
23
MC/DC0%0/1
$3 || 0
$3
MC/DC100%1/1
$4 || 0
$4
Truth table50%1/2
A
A= $3
Aresult
11
00
Truth table100%2/2
A
A= $4
Aresult
11
00
193 5 ▶ $2
Branch: 1 branch
BranchTrueFalseTotal
$2 and "-" eq $2 or $offset = -$offset 2 3 5
Condition66%2/3
$2 and "-" eq $2
!ll&&!rl&&r
032
MC/DC50%1/2
$2 and "-" eq $2
$2 "-" eq $2
Truth table66%2/3
A and B
A= $2B= "-" eq $2
ABresult
0X0
100
111
194 and '-' eq $2
195 or $offset = - $offset;
196 5 return ( UTC => $offset );
197 } else {
198 0 HAVE_DATETIME
199 or return ( $zone => 0 ); # On the user's head be it.
200  
201 0 ▶ DateTime::TimeZone->is_valid_name( $zone )
Branch: 1 branch
BranchTrueFalseTotal
"DateTime::TimeZone"->is_valid_name($zone) and return $zone, 0 0 0 0
202 and return ( $zone => 0 );
203  
204 0 ▶ $fatal
Branch: 1 branch
BranchTrueFalseTotal
$fatal and $self->wail("Invalid time zone '${zone}'") 0 0 0
205 and $self->wail( "Invalid time zone '$zone'" );
206 0 return;
207 }
208 }
209  
210 7 ▶sub tz {
Subroutine tz: called CC=4 SCAR=15.9
Pod documented
211 7 my ( $self, @args ) = @_;
212 7 ▶ if ( @args ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 7 0 7
213 7 ▶ if ( defined $args[0] && $args[0] ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args[0] and $args[0] ne '') { } 0 7 7
Condition33%1/3
defined $args[0] and $args[0] ne ""
!ll&&!rl&&r
700
MC/DC0%0/2
defined $args[0] and $args[0] ne ""
defined $args[0] $args[0] ne ""
Truth table33%1/3
A and B
A= defined $args[0]B= $args[0] ne ""
ABresult
0X0
100
111
214 $self->{$package}{tz} = [
215 0 $self->_interpret_zone( $args[0], 1 ) ];
216 } else {
217 7 delete $self->{$package}{tz};
218 }
219 }
220 7 return $self->SUPER::tz( @args );
221 }
222  
223 0 ▶sub __back_end_default {
Subroutine __back_end_default: not called CC=2 SCAR=17.9
224 0 my ( undef, $cls ) = @_; # Invocant ($self) unused
225 0 ▶ defined $cls
Branch: 1 branch
BranchTrueFalseTotal
defined $cls and return $cls 0 0 0
226 and return $cls;
227 0 return 'DateTime';
228 }
229  
230 0 ▶sub __back_end_validate {
Subroutine __back_end_validate: not called CC=1 SCAR=6.9
231 0 my ( undef, $cls, @arg ) = @_; # Invocant ($self) unused
232 0 $cls->now( @arg );
233 0 return;
234 }
235  
236 9 1;
237  
238 =head1 NAME
239  
240 Astro::App::Satpass2::ParseTime::ISO8601 - Astro::App::Satpass2 minimal ISO-8601 parser
241  
242 =head1 SYNOPSIS
243  
244 No user-serviceable parts inside.
245  
246 =head1 DETAILS
247  
248 This class parses ISO-8601 dates. It does not do ordinal days or weeks,
249 but it is rather permissive on punctuation, and permits the convenience
250 dates C<'yesterday'>, C<'today'>, and C<'tomorrow'>.
251  
252 This class understands ISO-8601 time zone specifications of the form
253 'Z', 'UT', 'GMT' and C<[+-]\d{1,2}:?\d{,2}>, but it knows nothing about
254 shifts for summer time. So C<2009/7/1 12:00:00 -5> is 5:00 PM GMT, not
255 4:00 PM. Other zones will be accepted, but may not do what you want. See
256 below.
257  
258 As an extension to the ISO-8601 standard, years can be followed by an
259 era specification, which is one of C<'AD'>, C<'BC'>, C<'BCE'>, or
260 C<'CE'> without regard to case. The era indicator may be separated from
261 the year by white space, and be followed by a non-digit separator
262 character.
263  
264 Unless the era is specified, years less than C<70> will have C<2000>
265 added, and years at least equal to C<70> but less than C<100> will have
266 C<1900> added.
267  
268 If L<DateTime|DateTime> can be loaded, it will be used to get an epoch
269 from the parsed date, including zone.
270  
271 If L<DateTime|DateTime> can B<not> be loaded, L<Time::Local|Time::Local>
272 will be used to get an epoch from the parsed date.
273 L<Time::Local|Time::Local> has its own quirks when it sees a year in the
274 distant past. Zones other than C<UTC>, C<GMT>, C<Z>, and numeric offsets
275 will be handled by setting C<$ENV{TZ}> to the specified zone before
276 converting from local time to epoch. If this works for you, fine. If
277 not, tough. B<You have been warned!>
278  
279 =head1 METHODS
280  
281 This class supports no public methods over and above those documented in
282 its superclass
283 L<Astro::App::Satpass2::ParseTime|Astro::App::Satpass2::ParseTime>.
284  
285 =head1 SUPPORT
286  
287 Support is by the author. Please file bug reports at
288 L<https://github.com/trwyant/perl-Astro-App-Satpass2/issues> or in
289 electronic mail to the author.
290  
291 =head1 AUTHOR
292  
293 Thomas R. Wyant, III F<harryfmudd at comcast dot net>
294  
295 =head1 COPYRIGHT AND LICENSE
296  
297 Copyright (C) 2009-2026 by Thomas R. Wyant, III
298  
299 This program is free software; you can redistribute it and/or modify it
300 under the same terms as Perl 5.10.0. For more details, see the full text
301 of the licenses in the files F<LICENSE-Artistic> and F<LICENSE-GPL>.
302  
303 This program is distributed in the hope that it will be useful, but
304 without any warranty; without even the implied warranty of
305 merchantability or fitness for a particular purpose.
306  
307 =cut
308  
309 __END__
« blib/lib/Astro/App/Satpass2/ParseTime.pm ↑ Summary blib/lib/Astro/App/Satpass2/Utils.pm »