blib/lib/Zuzu/Module/Time.pm

filter: Statementstmt 30.7% 112 / 364 Branchbran 10.8% 19 / 176 Conditioncond 5.5% 8 / 145 MC/DCmcdc 1.3% 1 / 76 Subroutinesub 14.9% 16 / 107 Podpod 0.0% 0 / 1 totaltotal 17.9% 156 / 869 CC 159 SCAR 40.0
CC
159
Cov
20%
_parse_rfc5322_zone
240.0
_parse_rfc5322
240.0
_same_wall_epoch
182.0
CRAP 54.5
« blib/lib/Zuzu/Module/String.pm ↑ Summary blib/lib/Zuzu/Module/DNS.pm »
1 package Zuzu::Module::Time;
2  
3 11 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 11 our $VERSION = '0.007002';
6  
7 11 ▶use DateTime::Lite ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 11 ▶use DateTime::Lite::TimeZone ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 ▶use POSIX qw( strftime );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 11 ▶use Time::Local qw( timegm );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 11 ▶use Zuzu::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 11 ▶use Zuzu::Util::NativeHelpers qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
14 native_class
15 native_function
16 native_object
17 11 );
18  
19 11 my @MONTH_ABBR = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec );
20 11 my @DAY_ABBR = qw( Mon Tue Wed Thu Fri Sat Sun );
21 11 my %MONTH_BY_NAME = (
22 jan => 1, january => 1,
23 feb => 2, february => 2,
24 mar => 3, march => 3,
25 apr => 4, april => 4,
26 may => 5,
27 jun => 6, june => 6,
28 jul => 7, july => 7,
29 aug => 8, august => 8,
30 sep => 9, sept => 9, september => 9,
31 oct => 10, october => 10,
32 nov => 11, november => 11,
33 dec => 12, december => 12,
34 );
35 11 my %WEEKDAY_BY_NAME = (
36 sun => 0, sunday => 0,
37 mon => 1, monday => 1,
38 tue => 2, tues => 2, tuesday => 2,
39 wed => 3, weds => 3, wednesday => 3,
40 thu => 4, thur => 4, thurs => 4, thursday => 4,
41 fri => 5, friday => 5,
42 sat => 6, saturday => 6,
43 );
44  
45 0 ▶sub _floor_epoch {
Subroutine _floor_epoch: not called CC=3 SCAR=24.8
46 0 my ( $epoch ) = @_;
47 0 my $int = int($epoch);
48 0 ▶ return $int - 1 if $epoch < 0 and $epoch != $int;
Branch: 1 branch
BranchTrueFalseTotal
if $epoch < 0 and $epoch != $int 0 0 0
Condition0%0/3
$epoch < 0 and $epoch != $int
!ll&&!rl&&r
000
MC/DC0%0/2
$epoch < 0 and $epoch != $int
$epoch < 0 $epoch != $int
Truth table0%0/3
A and B
A= $epoch < 0B= $epoch != $int
ABresult
0X0
100
111
49 0 return $int;
50 }
51  
52 0 ▶sub _fractional_part {
Subroutine _fractional_part: not called CC=1 SCAR=6.9
53 0 my ( $epoch ) = @_;
54 0 return $epoch - _floor_epoch($epoch);
55 }
56  
57 3 ▶sub _zone_name {
Subroutine _zone_name: called CC=9 SCAR=23.4
58 3 my ( $value ) = @_;
59 3 ▶ return 'UTC' if !defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 3 3
60 return $value->slots->{_name}
61 if ref($value) and $value->can('slots')
62 3 ▶ and exists $value->slots->{_name};
Branch: 1 branch
BranchTrueFalseTotal
if ref $value and $value->can("slots") and exists $value->slots->{"_name"} 1 2 3
Condition66%4/6
ref $value and $value->can("slots") and exists $value->slots->{"_name"}
!ll&&!rl&&r
201
ref $value and $value->can("slots")
!ll&&!rl&&r
201
MC/DC33%1/3
ref $value and $value->can("slots") and exists $value->slots->{"_name"}
ref $value $value->can("slots") exists $value->slots->{"_name"}
Truth table50%2/4
A and B and C
A= ref $valueB= $value->can("slots")C= exists $value->slots->{"_name"}
ABCresult
0XX0
10X0
1100
1111
63 2 my $name = "$value";
64 2 ▶ return 'UTC' if $name eq '' or uc($name) eq 'Z';
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "" or uc $name eq "Z" 0 2 2
Condition33%1/3
$name eq "" or uc $name eq "Z"
l!l&&r!l&&!r
002
MC/DC0%0/2
$name eq "" or uc $name eq "Z"
$name eq "" uc $name eq "Z"
Truth table33%1/3
A or B
A= $name eq ""B= uc $name eq "Z"
ABresult
1X1
011
000
65 2 ▶ return 'UTC' if uc($name) eq 'GMT';
Branch: 1 branch
BranchTrueFalseTotal
if uc $name eq "GMT" 0 2 2
66 2 ▶ return 'local' if lc($name) eq 'local';
Branch: 1 branch
BranchTrueFalseTotal
if lc $name eq "local" 1 1 2
67 1 return $name;
68 }
69  
70 3 ▶sub _timezone {
Subroutine _timezone: called CC=3 SCAR=11.0
71 3 my ( $value ) = @_;
72 3 my $name = _zone_name($value);
73 3 my $timezone = DateTime::Lite::TimeZone->new( name => $name );
74 3 ▶ _runtime_error( DateTime::Lite::TimeZone->error || "invalid timezone: $name" )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $timezone 1 2 3
Condition50%1/2
'DateTime::Lite::TimeZone'->error || "invalid timezone: $name"
l!l
10
MC/DC0%0/1
'DateTime::Lite::TimeZone'->error || "invalid timezone: $name"
'DateTime::Lite::TimeZone'->error
Truth table50%1/2
A
A= 'DateTime::Lite::TimeZone'->error
Aresult
11
00
75 if !defined $timezone;
76 2 return $timezone;
77 }
78  
79 3 ▶sub _timezone_label {
Subroutine _timezone_label: called CC=1 SCAR=0.0
80 3 my ( $value ) = @_;
81 3 return _timezone($value)->name;
82 }
83  
84 0 ▶sub _offset_to_zone {
Subroutine _offset_to_zone: not called CC=2 SCAR=17.9
85 0 my ( $offset ) = @_;
86 0 ▶ my $sign = $offset < 0 ? '-' : '+';
Branch: 1 branch
BranchTrueFalseTotal
$offset < 0 ? : 0 0 0
87 0 $offset = abs($offset);
88 0 return sprintf '%s%02d%02d',
89 $sign,
90 int( $offset / 3600 ),
91 int( ( $offset % 3600 ) / 60 );
92 }
93  
94 0 ▶sub _parse_tz_offset {
Subroutine _parse_tz_offset: not called CC=7 SCAR=40.3
95 0 my ( $value ) = @_;
96 0 ▶ return 0 if not defined $value or $value =~ /\A(?:UTC|UT|GMT|Z)\z/i;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value or $value =~ /\A(?:UTC|UT|GMT|Z)\z/i 0 0 0
Condition0%0/3
not defined $value or $value =~ /\A(?:UTC|UT|GMT|Z)\z/i
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $value or $value =~ /\A(?:UTC|UT|GMT|Z)\z/i
not defined $value $value =~ /\A(?:UTC|UT|GMT|Z)\z/i
Truth table0%0/3
not(A) or B
A= not defined $valueB= $value =~ /\A(?:UTC|UT|GMT|Z)\z/i
ABresult
1X1
011
000
97 0 ▶ return if $value !~ /\A([+-])(\d\d):?(\d\d)\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /\A([+-])(\d\d):?(\d\d)\z/ 0 0 0
98 0 my ( $sign, $hours, $minutes ) = ( $1, $2, $3 );
99 0 ▶ return if $hours > 23 or $minutes > 59;
Branch: 1 branch
BranchTrueFalseTotal
if $hours > 23 or $minutes > 59 0 0 0
Condition0%0/3
$hours > 23 or $minutes > 59
l!l&&r!l&&!r
000
MC/DC0%0/2
$hours > 23 or $minutes > 59
$hours > 23 $minutes > 59
Truth table0%0/3
A or B
A= $hours > 23B= $minutes > 59
ABresult
1X1
011
000
100 0 my $offset = $hours * 3600 + $minutes * 60;
101 0 ▶ return $sign eq '-' ? -$offset : $offset;
Branch: 1 branch
BranchTrueFalseTotal
$sign eq '-' ? : 0 0 0
102 }
103  
104 0 ▶sub _strip_trailing_period {
Subroutine _strip_trailing_period: not called CC=1 SCAR=6.9
105 0 my ( $value ) = @_;
106 0 $value =~ s/\.+\z//;
107 0 return lc $value;
108 }
109  
110 0 ▶sub _leap_year {
Subroutine _leap_year: not called CC=4 SCAR=30.0
111 0 my ( $year ) = @_;
112 0 ▶ return 0 if $year % 4;
Branch: 1 branch
BranchTrueFalseTotal
if $year % 4 0 0 0
113 0 ▶ return 1 if $year % 100;
Branch: 1 branch
BranchTrueFalseTotal
if $year % 100 0 0 0
114 0 ▶ return $year % 400 == 0 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$year % 400 == 0 ? : 0 0 0
115 }
116  
117 0 ▶sub _days_in_month {
Subroutine _days_in_month: not called CC=5 SCAR=34.0
118 0 my ( $year, $month ) = @_;
119 0 ▶ return 31 if grep { $_ == $month } qw( 1 3 5 7 8 10 12 );
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ == $month;} 1, 3, 5, 7, 8, 10, 12 0 0 0
120 0 ▶ return 30 if grep { $_ == $month } qw( 4 6 9 11 );
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ == $month;} 4, 6, 9, 11 0 0 0
121 0 ▶ return _leap_year($year) ? 29 : 28 if $month == 2;
Branch: 2 branches
BranchTrueFalseTotal
if $month == 2 0 0 0
_leap_year($year) ? : 0 0 0
122 0 return 0;
123 }
124  
125 0 ▶sub _parse_rfc5322_zone {
Subroutine _parse_rfc5322_zone: not called CC=15 SCAR=54.8
126 0 my ( $zone ) = @_;
127 0 my $offset = _parse_tz_offset($zone);
128 0 ▶ return $offset if defined $offset;
Branch: 1 branch
BranchTrueFalseTotal
if defined $offset 0 0 0
129  
130 0 my $key = uc $zone;
131 0 ▶ return -5 * 3600 if $key eq 'EST';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "EST" 0 0 0
132 0 ▶ return -4 * 3600 if $key eq 'EDT';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "EDT" 0 0 0
133 0 ▶ return -6 * 3600 if $key eq 'CST';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "CST" 0 0 0
134 0 ▶ return -5 * 3600 if $key eq 'CDT';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "CDT" 0 0 0
135 0 ▶ return -7 * 3600 if $key eq 'MST';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "MST" 0 0 0
136 0 ▶ return -6 * 3600 if $key eq 'MDT';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "MDT" 0 0 0
137 0 ▶ return -8 * 3600 if $key eq 'PST';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "PST" 0 0 0
138 0 ▶ return -7 * 3600 if $key eq 'PDT';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "PDT" 0 0 0
139  
140 0 ▶ if ( length($key) == 1 and $key ne 'J' and $key =~ /\A[A-Z]\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $key == 1 and $key ne "J" and $key =~ /\A[A-Z]\z/) 0 0 0
Condition0%0/6
length $key == 1 and $key ne "J" and $key =~ /\A[A-Z]\z/
!ll&&!rl&&r
000
length $key == 1 and $key ne "J"
!ll&&!rl&&r
000
MC/DC0%0/3
length $key == 1 and $key ne "J" and $key =~ /\A[A-Z]\z/
length $key == 1 $key ne "J" $key =~ /\A[A-Z]\z/
Truth table0%0/4
A and B and C
A= length $key == 1B= $key ne "J"C= $key =~ /\A[A-Z]\z/
ABCresult
0XX0
10X0
1100
1111
141 0 my $pos = index 'ABCDEFGHIKLMNOPQRSTUVWXYZ', $key;
142 0 ▶ return if $pos < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $pos < 0 0 0 0
143 0 ▶ return ( $pos + 1 ) * 3600 if $pos < 12;
Branch: 1 branch
BranchTrueFalseTotal
if $pos < 12 0 0 0
144 0 return -( $pos - 11 ) * 3600;
145 }
146  
147 0 return;
148 }
149  
150 1 ▶sub _wrap_timezone {
Subroutine _wrap_timezone: called CC=1 SCAR=0.0
151 1 my ( $class_obj, $zone ) = @_;
152 1 my $name = _timezone_label($zone);
153 1 return native_object(
154 class => $class_obj,
155 slots => { _name => $name },
156 const => { _name => 1 },
157 );
158 }
159  
160 0 ▶sub _wrap_duration {
Subroutine _wrap_duration: not called CC=8 SCAR=42.8
161 0 my ( $class_obj, %parts ) = @_;
162 return native_object(
163 class => $class_obj,
164 slots => {
165 _years => 0 + ( $parts{years} // 0 ),
166 _months => 0 + ( $parts{months} // 0 ),
167 _weeks => 0 + ( $parts{weeks} // 0 ),
168 _days => 0 + ( $parts{days} // 0 ),
169 _hours => 0 + ( $parts{hours} // 0 ),
170 _minutes => 0 + ( $parts{minutes} // 0 ),
171 0 ▶ _seconds => 0 + ( $parts{seconds} // 0 ),
Condition0%0/14
$parts{"years"} // 0
l!l
00
$parts{"months"} // 0
l!l
00
$parts{"weeks"} // 0
l!l
00
$parts{"days"} // 0
l!l
00
$parts{"hours"} // 0
l!l
00
$parts{"minutes"} // 0
l!l
00
$parts{"seconds"} // 0
l!l
00
MC/DC0%0/1
$parts{"years"} // 0
$parts{"years"}
MC/DC0%0/1
$parts{"months"} // 0
$parts{"months"}
MC/DC0%0/1
$parts{"weeks"} // 0
$parts{"weeks"}
MC/DC0%0/1
$parts{"days"} // 0
$parts{"days"}
MC/DC0%0/1
$parts{"hours"} // 0
$parts{"hours"}
MC/DC0%0/1
$parts{"minutes"} // 0
$parts{"minutes"}
MC/DC0%0/1
$parts{"seconds"} // 0
$parts{"seconds"}
Truth table0%0/2
A
A= $parts{"years"}
Aresult
11
00
Truth table0%0/2
A
A= $parts{"months"}
Aresult
11
00
Truth table0%0/2
A
A= $parts{"weeks"}
Aresult
11
00
Truth table0%0/2
A
A= $parts{"days"}
Aresult
11
00
Truth table0%0/2
A
A= $parts{"hours"}
Aresult
11
00
Truth table0%0/2
A
A= $parts{"minutes"}
Aresult
11
00
Truth table0%0/2
A
A= $parts{"seconds"}
Aresult
11
00
172 },
173 const => {
174 _years => 1,
175 _months => 1,
176 _weeks => 1,
177 _days => 1,
178 _hours => 1,
179 _minutes => 1,
180 _seconds => 1,
181 },
182 );
183 }
184  
185 0 ▶sub _wrap_format {
Subroutine _wrap_format: not called CC=3 SCAR=24.8
186 0 my ( $class_obj, %parts ) = @_;
187 return native_object(
188 class => $class_obj,
189 slots => {
190 _kind => $parts{kind} // 'strftime',
191 _pattern => $parts{pattern} // '',
192 _timezone => $parts{timezone},
193 },
194 0 ▶ const => {
Condition0%0/4
$parts{"kind"} // "strftime"
l!l
00
$parts{"pattern"} // ""
l!l
00
MC/DC0%0/1
$parts{"kind"} // "strftime"
$parts{"kind"}
MC/DC0%0/1
$parts{"pattern"} // ""
$parts{"pattern"}
Truth table0%0/2
A
A= $parts{"kind"}
Aresult
11
00
Truth table0%0/2
A
A= $parts{"pattern"}
Aresult
11
00
195 _kind => 1,
196 _pattern => 1,
197 _timezone => 1,
198 },
199 );
200 }
201  
202 2 ▶sub _wrap_time {
Subroutine _wrap_time: called CC=4 SCAR=16.1
203 2 my ( $class_obj, $epoch, $zone ) = @_;
204 2 ▶ $epoch = $epoch->epoch + 0 if ref($epoch) and $epoch->can('epoch');
Branch: 1 branch
BranchTrueFalseTotal
if ref $epoch and $epoch->can("epoch") 0 2 2
Condition33%1/3
ref $epoch and $epoch->can("epoch")
!ll&&!rl&&r
200
MC/DC0%0/2
ref $epoch and $epoch->can("epoch")
ref $epoch $epoch->can("epoch")
Truth table33%1/3
A and B
A= ref $epochB= $epoch->can("epoch")
ABresult
0X0
100
111
205 2 ▶ return native_object(
Branch: 1 branch
BranchTrueFalseTotal
defined $epoch ? : 2 0 2
206 class => $class_obj,
207 slots => {
208 _epoch => 0 + ( defined $epoch ? $epoch : 0 ),
209 _timezone => _timezone_label($zone),
210 },
211 const => {
212 _epoch => 1,
213 _timezone => 1,
214 },
215 );
216 }
217  
218 0 ▶sub _epoch_from_self { $_[0]->slots->{_epoch} }
Subroutine _epoch_from_self: not called CC=1 SCAR=6.9
219 0 ▶sub _zone_from_self { $_[0]->slots->{_timezone} // 'UTC' }
Subroutine _zone_from_self: not called CC=2 SCAR=17.9
Condition0%0/2
$_[0]->slots->{"_timezone"} // "UTC"
l!l
00
MC/DC0%0/1
$_[0]->slots->{"_timezone"} // "UTC"
$_[0]->slots->{"_timezone"}
Truth table0%0/2
A
A= $_[0]->slots->{"_timezone"}
Aresult
11
00
220  
221 0 ▶sub _datetime_from_epoch {
Subroutine _datetime_from_epoch: not called CC=1 SCAR=6.9
222 0 my ( $epoch, $zone ) = @_;
223 0 return DateTime::Lite->from_epoch(
224 epoch => $epoch,
225 time_zone => _timezone($zone),
226 );
227 }
228  
229 0 ▶sub _datetime_from_self {
Subroutine _datetime_from_self: not called CC=1 SCAR=6.9
230 0 my ( $self ) = @_;
231 0 return _datetime_from_epoch( _epoch_from_self($self), _zone_from_self($self) );
232 }
233  
234 0 ▶sub _same_wall_epoch {
Subroutine _same_wall_epoch: not called CC=13 SCAR=52.0
235 0 my ( $zone, %parts ) = @_;
236 0 my $tz = _timezone($zone);
237 0 ▶ my $frac = delete $parts{fraction} // 0;
Condition0%0/2
delete $parts{"fraction"} // 0
l!l
00
MC/DC0%0/1
delete $parts{"fraction"} // 0
delete $parts{"fraction"}
Truth table0%0/2
A
A= delete $parts{"fraction"}
Aresult
11
00
238  
239 0 for my $minute_shift ( 0 .. 180 ) {
240 0 my %try = %parts;
241 0 ▶ if ( $minute_shift ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($minute_shift) 0 0 0
242 my $base = timegm(
243 $parts{second}, $parts{minute}, $parts{hour},
244 $parts{day}, $parts{month} - 1, $parts{year}
245 0 );
246 0 my @utc = gmtime( $base + $minute_shift * 60 );
247 0 @try{qw( second minute hour day month year )} =
248 ( $utc[0], $utc[1], $utc[2], $utc[3], $utc[4] + 1, $utc[5] + 1900 );
249 }
250  
251 my $naive = timegm(
252 $try{second}, $try{minute}, $try{hour},
253 $try{day}, $try{month} - 1, $try{year}
254 0 );
255 0 my %offsets;
256 0 for my $probe ( -172800, -86400, -3600, 0, 3600, 86400, 172800 ) {
257 0 $offsets{ $tz->offset_for_datetime(
258 DateTime::Lite->from_epoch( epoch => $naive + $probe, time_zone => 'UTC' )
259 ) } = 1;
260 }
261 0 my @epochs;
262 0 for my $offset ( keys %offsets ) {
263 0 my $candidate = $naive - $offset;
264 0 my $dt = DateTime::Lite->from_epoch( epoch => $candidate, time_zone => $tz );
265 push @epochs, $candidate
266 if $dt->year == $try{year}
267 and $dt->month == $try{month}
268 and $dt->day == $try{day}
269 and $dt->hour == $try{hour}
270 and $dt->minute == $try{minute}
271 0 ▶ and $dt->second == $try{second};
Branch: 1 branch
BranchTrueFalseTotal
if $dt->year == $try{"year"} and $dt->month == $try{"month"} and $dt->day == $try{"day"} and $dt->hour == $try{"hour"} and $dt->minute == $try{"minute"} and $dt->second == $try{"second"} 0 0 0
Condition0%0/15
$dt->year == $try{"year"} and $dt->month == $try{"month"} and $dt->day == $try{"day"} and $dt->hour == $try{"hour"} and $dt->minute == $try{"minute"} and $dt->second == $try{"second"}
!ll&&!rl&&r
000
$dt->year == $try{"year"} and $dt->month == $try{"month"} and $dt->day == $try{"day"} and $dt->hour == $try{"hour"} and $dt->minute == $try{"minute"}
!ll&&!rl&&r
000
$dt->year == $try{"year"} and $dt->month == $try{"month"} and $dt->day == $try{"day"} and $dt->hour == $try{"hour"}
!ll&&!rl&&r
000
$dt->year == $try{"year"} and $dt->month == $try{"month"} and $dt->day == $try{"day"}
!ll&&!rl&&r
000
$dt->year == $try{"year"} and $dt->month == $try{"month"}
!ll&&!rl&&r
000
MC/DC0%0/6
$dt->year == $try{"year"} and $dt->month == $try{"month"} and $dt->day == $try{"day"} and $dt->hour == $try{"hour"} and $dt->minute == $try{"minute"} and $dt->second == $try{"second"}
$dt->year == $try{"year"} $dt->month == $try{"month"} $dt->day == $try{"day"} $dt->hour == $try{"hour"} $dt->minute == $try{"minute"} $dt->second == $try{"second"}
Truth table0%0/7
A and B and C and D and E and F
A= $dt->year == $try{"year"}B= $dt->month == $try{"month"}C= $dt->day == $try{"day"}D= $dt->hour == $try{"hour"}E= $dt->minute == $try{"minute"}F= $dt->second == $try{"second"}
ABCDEFresult
0XXXXX0
10XXXX0
110XXX0
1110XX0
11110X0
1111100
1111111
272 }
273 0 ▶ return ( sort { $a <=> $b } @epochs )[0] + $frac if @epochs;
Branch: 1 branch
BranchTrueFalseTotal
if @epochs 0 0 0
274 }
275  
276 0 my $dt = DateTime::Lite->new( %parts, time_zone => $tz );
277 0 return $dt->epoch + $frac;
278 }
279  
280 0 ▶sub _calendar_add {
Subroutine _calendar_add: not called CC=1 SCAR=6.9
281 0 my ( $class_obj, $self, %delta ) = @_;
282 0 my $epoch = _epoch_from_self($self);
283 0 my $dt = _datetime_from_self($self)->clone;
284 0 $dt->add(%delta);
285 0 my %parts = (
286 year => $dt->year,
287 month => $dt->month,
288 day => $dt->day,
289 hour => $dt->hour,
290 minute => $dt->minute,
291 second => $dt->second,
292 fraction => _fractional_part($epoch),
293 );
294 0 return _wrap_time(
295 $class_obj,
296 _same_wall_epoch( _zone_from_self($self), %parts ),
297 _zone_from_self($self),
298 );
299 }
300  
301 0 ▶sub _duration_parts {
Subroutine _duration_parts: not called CC=9 SCAR=45.0
302 0 my ( $duration, $sign ) = @_;
303 0 ▶ $sign //= 1;
Condition0%0/2
$sign //= 1
l!l
00
MC/DC0%0/1
$sign //= 1
$sign
Truth table0%0/2
A
A= $sign
Aresult
11
00
304 return (
305 years => $sign * ( $duration->slots->{_years} // 0 ),
306 months => $sign * ( $duration->slots->{_months} // 0 ),
307 weeks => $sign * ( $duration->slots->{_weeks} // 0 ),
308 days => $sign * ( $duration->slots->{_days} // 0 ),
309 hours => $sign * ( $duration->slots->{_hours} // 0 ),
310 minutes => $sign * ( $duration->slots->{_minutes} // 0 ),
311 0 ▶ seconds => $sign * ( $duration->slots->{_seconds} // 0 ),
Condition0%0/14
$duration->slots->{"_years"} // 0
l!l
00
$duration->slots->{"_months"} // 0
l!l
00
$duration->slots->{"_weeks"} // 0
l!l
00
$duration->slots->{"_days"} // 0
l!l
00
$duration->slots->{"_hours"} // 0
l!l
00
$duration->slots->{"_minutes"} // 0
l!l
00
$duration->slots->{"_seconds"} // 0
l!l
00
MC/DC0%0/1
$duration->slots->{"_years"} // 0
$duration->slots->{"_years"}
MC/DC0%0/1
$duration->slots->{"_months"} // 0
$duration->slots->{"_months"}
MC/DC0%0/1
$duration->slots->{"_weeks"} // 0
$duration->slots->{"_weeks"}
MC/DC0%0/1
$duration->slots->{"_days"} // 0
$duration->slots->{"_days"}
MC/DC0%0/1
$duration->slots->{"_hours"} // 0
$duration->slots->{"_hours"}
MC/DC0%0/1
$duration->slots->{"_minutes"} // 0
$duration->slots->{"_minutes"}
MC/DC0%0/1
$duration->slots->{"_seconds"} // 0
$duration->slots->{"_seconds"}
Truth table0%0/2
A
A= $duration->slots->{"_years"}
Aresult
11
00
Truth table0%0/2
A
A= $duration->slots->{"_months"}
Aresult
11
00
Truth table0%0/2
A
A= $duration->slots->{"_weeks"}
Aresult
11
00
Truth table0%0/2
A
A= $duration->slots->{"_days"}
Aresult
11
00
Truth table0%0/2
A
A= $duration->slots->{"_hours"}
Aresult
11
00
Truth table0%0/2
A
A= $duration->slots->{"_minutes"}
Aresult
11
00
Truth table0%0/2
A
A= $duration->slots->{"_seconds"}
Aresult
11
00
312 );
313 }
314  
315 0 ▶sub _duration_seconds {
Subroutine _duration_seconds: not called CC=1 SCAR=6.9
316 0 my ( %parts ) = @_;
317 0 return $parts{seconds} + $parts{minutes} * 60 + $parts{hours} * 3600;
318 }
319  
320 0 ▶sub _add_duration {
Subroutine _add_duration: not called CC=1 SCAR=6.9
321 0 my ( $class_obj, $self, $duration, $sign ) = @_;
322 0 my %parts = _duration_parts( $duration, $sign );
323 0 my $out = $self;
324 0 $out = _wrap_time(
325 $class_obj,
326 _epoch_from_self($out) + _duration_seconds(%parts),
327 _zone_from_self($out),
328 );
329 $out = _calendar_add(
330 $class_obj,
331 $out,
332 years => $parts{years},
333 months => $parts{months},
334 weeks => $parts{weeks},
335 days => $parts{days},
336 0 );
337 0 return $out;
338 }
339  
340 0 ▶sub _format_rfc3339 {
Subroutine _format_rfc3339: not called CC=1 SCAR=6.9
341 0 my ( $dt ) = @_;
342 0 return $dt->strftime('%Y-%m-%dT%H:%M:%S%z') =~ s/([+-]\d\d)(\d\d)\z/$1:$2/r;
343 }
344  
345 0 ▶sub _format_rfc5322 {
Subroutine _format_rfc5322: not called CC=2 SCAR=17.9
346 0 my ( $dt, $include_weekday ) = @_;
347 0 ▶ my $format = $include_weekday ? '%a, %d %b %Y %H:%M:%S %z' : '%d %b %Y %H:%M:%S %z';
Branch: 1 branch
BranchTrueFalseTotal
$include_weekday ? : 0 0 0
348 0 return $dt->strftime($format);
349 }
350  
351 0 ▶sub _parse_rfc3339_or_iso {
Subroutine _parse_rfc3339_or_iso: not called CC=9 SCAR=45.0
352 0 my ( $value, $default_zone, $need_zone ) = @_;
353 0 ▶ if ( $value =~ /\A
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /\A (\d{4})-(\d\d)-(\d\d) (?:[Tt ](\d\d):(\d\d)(?::(\d\d)(?:\.\d+)?)?)? (?:\s*(Z|[+-]\d\d:?\d\d))? \z/x) 0 0 0
354 (\d{4})-(\d\d)-(\d\d)
355 (?:[Tt ](\d\d):(\d\d)(?::(\d\d)(?:\.\d+)?)?)?
356 (?:\s*(Z|[+-]\d\d:?\d\d))?
357 \z/x ) {
358 0 ▶ my ( $year, $month, $day, $hour, $minute, $second, $offset ) =
Condition0%0/6
$4 // 0
l!l
00
$5 // 0
l!l
00
$6 // 0
l!l
00
MC/DC0%0/1
$4 // 0
$4
MC/DC0%0/1
$5 // 0
$5
MC/DC0%0/1
$6 // 0
$6
Truth table0%0/2
A
A= $4
Aresult
11
00
Truth table0%0/2
A
A= $5
Aresult
11
00
Truth table0%0/2
A
A= $6
Aresult
11
00
359 ( $1, $2, $3, $4 // 0, $5 // 0, $6 // 0, $7 );
360 0 ▶ _runtime_error("Time.parse() requires a timezone")
Branch: 1 branch
BranchTrueFalseTotal
if $need_zone and not defined $offset and not defined $default_zone 0 0 0
Condition0%0/6
$need_zone and not defined $offset and not defined $default_zone
!ll&&!rl&&r
000
$need_zone and not defined $offset
!ll&&!rl&&r
000
MC/DC0%0/3
$need_zone and not defined $offset and not defined $default_zone
$need_zone not defined $offset not defined $default_zone
Truth table0%0/4
A and not(B) and not(C)
A= $need_zoneB= not defined $offsetC= not defined $default_zone
ABCresult
0XX0
10X0
1100
1111
361 if $need_zone and !defined $offset and !defined $default_zone;
362 0 ▶ my $zone = defined $offset ? _offset_to_zone( _parse_tz_offset($offset) ) : $default_zone;
Branch: 1 branch
BranchTrueFalseTotal
defined $offset ? : 0 0 0
363 0 my $epoch = _same_wall_epoch(
364 $zone,
365 year => 0 + $year,
366 month => 0 + $month,
367 day => 0 + $day,
368 hour => 0 + $hour,
369 minute => 0 + $minute,
370 second => 0 + $second,
371 );
372 0 return ( $epoch, $zone );
373 }
374 0 return;
375 }
376  
377 0 ▶sub _parse_rfc5322 {
Subroutine _parse_rfc5322: not called CC=15 SCAR=54.8
378 0 my ( $value ) = @_;
379 0 ▶ if ( $value =~ /\A
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /\A (?:([A-Za-z.]+),\s*)? (\d{1,2})\s+([A-Za-z.]+)\s+(\d{2}|\d{4}) \s+(\d{1,2}):(\d\d)(?::(\d\d))? \s+([+-]\d\d:?\d\d|[A-Za-z]{1,5}) \z/xi) 0 0 0
380 (?:([A-Za-z.]+),\s*)?
381 (\d{1,2})\s+([A-Za-z.]+)\s+(\d{2}|\d{4})
382 \s+(\d{1,2}):(\d\d)(?::(\d\d))?
383 \s+([+-]\d\d:?\d\d|[A-Za-z]{1,5})
384 \z/xi ) {
385 0 ▶ my ( $weekday, $day, $mon, $year, $hour, $minute, $second, $zone ) =
Condition0%0/2
$7 // 0
l!l
00
MC/DC0%0/1
$7 // 0
$7
Truth table0%0/2
A
A= $7
Aresult
11
00
386 ( $1, $2, $3, $4, $5, $6, $7 // 0, $8 );
387 0 ▶ if ( defined $weekday ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $weekday) 0 0 0
388 _runtime_error("invalid weekday")
389 0 ▶ if !exists $WEEKDAY_BY_NAME{ _strip_trailing_period($weekday) };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $WEEKDAY_BY_NAME{_strip_trailing_period($weekday)} 0 0 0
390 }
391 0 ▶ my $month = $MONTH_BY_NAME{ _strip_trailing_period($mon) }
Branch: 1 branch
BranchTrueFalseTotal
my $month = $MONTH_BY_NAME{_strip_trailing_period($mon)} or _runtime_error("invalid month") 0 0 0
392 or _runtime_error("invalid month");
393 0 ▶ $year += $year >= 50 ? 1900 : 2000 if length($year) == 2;
Branch: 2 branches
BranchTrueFalseTotal
if length $year == 2 0 0 0
$year >= 50 ? : 0 0 0
394 0 ▶ _runtime_error("invalid year") if $year < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $year < 1 0 0 0
395 0 ▶ _runtime_error("invalid time") if $hour > 23 or $minute > 59 or $second > 59;
Branch: 1 branch
BranchTrueFalseTotal
if $hour > 23 or $minute > 59 or $second > 59 0 0 0
Condition0%0/6
$hour > 23 or $minute > 59 or $second > 59
l!l&&r!l&&!r
000
$hour > 23 or $minute > 59
l!l&&r!l&&!r
000
MC/DC0%0/3
$hour > 23 or $minute > 59 or $second > 59
$hour > 23 $minute > 59 $second > 59
Truth table0%0/4
A or B or C
A= $hour > 23B= $minute > 59C= $second > 59
ABCresult
1XX1
01X1
0011
0000
396 0 ▶ _runtime_error("invalid date")
Branch: 1 branch
BranchTrueFalseTotal
if $day < 1 or $day > _days_in_month($year, $month) 0 0 0
Condition0%0/3
$day < 1 or $day > _days_in_month($year, $month)
l!l&&r!l&&!r
000
MC/DC0%0/2
$day < 1 or $day > _days_in_month($year, $month)
$day < 1 $day > _days_in_month($year, $month)
Truth table0%0/3
A or B
A= $day < 1B= $day > _days_in_month($year, $month)
ABresult
1X1
011
000
397 if $day < 1 or $day > _days_in_month( $year, $month );
398 0 my $offset = _parse_rfc5322_zone($zone);
399 0 ▶ _runtime_error("invalid time zone") if !defined $offset;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $offset 0 0 0
400 0 my $zone_name = _offset_to_zone($offset);
401 0 my $epoch = _same_wall_epoch(
402 $zone_name,
403 year => 0 + $year,
404 month => $month,
405 day => 0 + $day,
406 hour => 0 + $hour,
407 minute => 0 + $minute,
408 second => 0 + $second,
409 );
410 0 return ( $epoch, $zone_name );
411 }
412 0 return;
413 }
414  
415 0 ▶sub _parse_time_value {
Subroutine _parse_time_value: not called CC=3 SCAR=24.8
416 0 my ( $value, $zone, $require_zone ) = @_;
417 0 my @parsed = _parse_rfc3339_or_iso( $value, $zone, $require_zone );
418 0 ▶ return @parsed if @parsed;
Branch: 1 branch
BranchTrueFalseTotal
if @parsed 0 0 0
419 0 @parsed = _parse_rfc5322($value);
420 0 ▶ return @parsed if @parsed;
Branch: 1 branch
BranchTrueFalseTotal
if @parsed 0 0 0
421 0 _runtime_error("Error parsing time");
422 }
423  
424 1 ▶sub _runtime_error {
Subroutine _runtime_error: called CC=1 SCAR=0.0
425 1 my ( $message ) = @_;
426 1 die Zuzu::Error->new_runtime(
427 message => $message,
428 file => '<std/time>',
429 line => 0,
430 );
431 }
432  
433 0 ▶sub _parse_legacy_strptime {
Subroutine _parse_legacy_strptime: not called CC=4 SCAR=30.0
434 0 my ( $value, $format ) = @_;
435 0 ▶ if ( $value =~ /\A(?:[A-Za-z]+\.?\s+)?(\d{1,2})(?:st|nd|rd|th)\s+([A-Za-z]{3}),\s+(\d{4})\z/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /\A(?:[A-Za-z]+\.?\s+)?(\d{1,2})(?:st|nd|rd|th)\s+([A-Za-z]{3}),\s+(\d{4})\z/i) 0 0 0
436 0 my ( $day, $mon, $year ) = ( $1, lc $2, $3 );
437 0 ▶ my $month = $MONTH_BY_NAME{$mon} or die "Exception: unable to parse month\n";
Branch: 1 branch
BranchTrueFalseTotal
my $month = $MONTH_BY_NAME{$mon} or die "Exception: unable to parse month\n" 0 0 0
438 0 return _same_wall_epoch(
439 'UTC',
440 year => 0 + $year,
441 month => $month,
442 day => 0 + $day,
443 hour => 0,
444 minute => 0,
445 second => 0,
446 );
447 }
448 0 my @parsed = _parse_rfc3339_or_iso( $value, 'UTC', 0 );
449 0 ▶ return $parsed[0] if @parsed;
Branch: 1 branch
BranchTrueFalseTotal
if @parsed 0 0 0
450 0 die "Exception: unable to parse time string\n";
451 }
452  
453 70 ▶sub IMPORT {
Subroutine IMPORT: called CC=9 SCAR=37.5
Pod undocumented
454 70 my ( $class, $runtime ) = @_;
455  
456 70 my $time_class = native_class( name => 'Time' );
457 70 my $zone_class = native_class( name => 'TimeZone' );
458 70 my $duration_class = native_class( name => 'Duration' );
459 70 my $format_class = native_class( name => 'TimeFormat' );
460 70 my $parser_class = native_class( name => 'TimeParser' );
461  
462 0 ▶ $zone_class->native_constructor( sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
463 0 my ( $rt, $klass, $positional, $named ) = @_;
464 0 ▶ my $name = @{ $positional // [] } ? $positional->[0] : $named->{name};
Branch: 1 branch
BranchTrueFalseTotal
@{$positional // [];} ? : 0 0 0
Condition0%0/2
$positional // []
l!l
00
MC/DC0%0/1
$positional // []
$positional
Truth table0%0/2
A
A= $positional
Aresult
11
00
465 0 return _wrap_timezone( $klass, _zone_name($name) );
466 70 } );
467  
468 0 ▶ $duration_class->native_constructor( sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
469 0 my ( $rt, $klass, $positional, $named ) = @_;
470 return _wrap_duration(
471 $klass,
472 years => $named->{years},
473 months => $named->{months},
474 weeks => $named->{weeks},
475 days => $named->{days},
476 hours => $named->{hours},
477 minutes => $named->{minutes},
478 0 ▶ seconds => @{ $positional // [] } ? $positional->[0] : $named->{seconds},
Condition0%0/2
$positional // []
l!l
00
MC/DC0%0/1
$positional // []
$positional
Truth table0%0/2
A
A= $positional
Aresult
11
00
479 0 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
@{$positional // [];} ? : 0 0 0
480 70 } );
481  
482 0 ▶ $format_class->native_constructor( sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
483 0 my ( $rt, $klass, $positional, $named ) = @_;
484 0 ▶ my $pattern = @{ $positional // [] } ? $positional->[0] : $named->{pattern};
Branch: 1 branch
BranchTrueFalseTotal
@{$positional // [];} ? : 0 0 0
Condition0%0/2
$positional // []
l!l
00
MC/DC0%0/1
$positional // []
$positional
Truth table0%0/2
A
A= $positional
Aresult
11
00
485 return _wrap_format(
486 $klass,
487 kind => $named->{kind} // 'strftime',
488 pattern => defined $pattern ? "$pattern" : '',
489 0 ▶ timezone => defined $named->{timezone} ? _timezone_label( $named->{timezone} ) : undef,
Branch: 2 branches
BranchTrueFalseTotal
defined $pattern ? : 0 0 0
defined $named->{'timezone'} ? : 0 0 0
Condition0%0/2
$named->{"kind"} // "strftime"
l!l
00
MC/DC0%0/1
$named->{"kind"} // "strftime"
$named->{"kind"}
Truth table0%0/2
A
A= $named->{"kind"}
Aresult
11
00
490 );
491 70 } );
492  
493 2 ▶ $time_class->native_constructor( sub {
Subroutine __ANON__: called CC=5 SCAR=16.6
494 2 my ( $rt, $klass, $positional, $named ) = @_;
495 2 ▶ my $epoch = @{ $positional // [] } ? $positional->[0] : $named->{epoch};
Branch: 1 branch
BranchTrueFalseTotal
@{$positional // [];} ? : 2 0 2
Condition50%1/2
$positional // []
l!l
20
MC/DC0%0/1
$positional // []
$positional
Truth table50%1/2
A
A= $positional
Aresult
11
00
496 2 ▶ $epoch = time if !defined $epoch;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $epoch 0 2 2
497 2 ▶ my $zone = exists $named->{timezone} ? $named->{timezone} : 'local';
Branch: 1 branch
BranchTrueFalseTotal
exists $named->{'timezone'} ? : 1 1 2
498 2 return _wrap_time( $klass, $epoch, $zone );
499 70 } );
500  
501 $time_class->static_methods->{parse} = native_function(
502 name => 'parse',
503 accepts_named => 1,
504 0 ▶ native => sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
505 0 my ( $klass, $raw, @rest ) = @_;
506 0 ▶ my $named = ref( $rest[-2] ) eq 'HASH' ? $rest[-2] : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $rest[-2] eq 'HASH' ? : 0 0 0
507 0 ▶ my $value = defined $raw ? "$raw" : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $raw ? : 0 0 0
508 0 ▶ my $zone = exists $named->{timezone} ? _timezone_label( $named->{timezone} ) : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $named->{'timezone'} ? : 0 0 0
509 0 my ( $epoch, $parsed_zone ) = _parse_time_value( $value, $zone, 1 );
510 0 ▶ return _wrap_time( $time_class, $epoch, $zone // $parsed_zone );
Condition0%0/3
$zone // $parsed_zone
l!l&&r!l&&!r
000
MC/DC0%0/2
$zone // $parsed_zone
$zone $parsed_zone
Truth table0%0/3
A // B
A= $zoneB= $parsed_zone
ABresult
1X1
011
000
511 },
512 70 );
513  
514 $zone_class->static_methods->{utc} = native_function(
515 name => 'utc',
516 1 ▶ native => sub { _wrap_timezone( $zone_class, 'UTC' ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
517 70 );
518 $zone_class->static_methods->{local} = native_function(
519 name => 'local',
520 0 ▶ native => sub { _wrap_timezone( $zone_class, 'local' ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
521 70 );
522 $zone_class->static_methods->{named} = native_function(
523 name => 'named',
524 0 ▶ native => sub { _wrap_timezone( $zone_class, $_[1] ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
525 70 );
526 $zone_class->static_methods->{offset} = native_function(
527 name => 'offset',
528 0 ▶ native => sub { _wrap_timezone( $zone_class, _offset_to_zone( 0 + ( $_[1] // 0 ) ) ) },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Condition0%0/2
$_[1] // 0
l!l
00
MC/DC0%0/1
$_[1] // 0
$_[1]
Truth table0%0/2
A
A= $_[1]
Aresult
11
00
529 70 );
530  
531 70 for my $part ( qw( seconds minutes hours days weeks months years ) ) {
532 $duration_class->static_methods->{$part} = native_function(
533 name => $part,
534 0 ▶ native => sub { _wrap_duration( $duration_class, $part => $_[1] // 0 ) },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Condition0%0/2
$_[1] // 0
l!l
00
MC/DC0%0/1
$_[1] // 0
$_[1]
Truth table0%0/2
A
A= $_[1]
Aresult
11
00
535 490 );
536 }
537  
538 $format_class->static_methods->{iso8601} = native_function(
539 name => 'iso8601',
540 0 ▶ native => sub { _wrap_format( $format_class, kind => 'iso8601' ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
541 70 );
542 $format_class->static_methods->{rfc3339} = native_function(
543 name => 'rfc3339',
544 0 ▶ native => sub { _wrap_format( $format_class, kind => 'rfc3339' ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
545 70 );
546 $format_class->static_methods->{rfc5322} = native_function(
547 name => 'rfc5322',
548 0 ▶ native => sub { _wrap_format( $format_class, kind => 'rfc5322' ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
549 70 );
550 $format_class->static_methods->{strftime} = native_function(
551 name => 'strftime',
552 accepts_named => 1,
553 0 ▶ native => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
554 0 my ( $klass, $pattern, @rest ) = @_;
555 0 ▶ my $named = ref( $rest[-2] ) eq 'HASH' ? $rest[-2] : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $rest[-2] eq 'HASH' ? : 0 0 0
556 return _wrap_format(
557 $format_class,
558 kind => 'strftime',
559 pattern => defined $pattern ? "$pattern" : '',
560 0 ▶ timezone => exists $named->{timezone} ? _timezone_label( $named->{timezone} ) : undef,
Branch: 2 branches
BranchTrueFalseTotal
defined $pattern ? : 0 0 0
exists $named->{'timezone'} ? : 0 0 0
561 );
562 },
563 70 );
564  
565 my %zone_methods = (
566 0 ▶ name => sub { $_[0]->slots->{_name} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
567 0 ▶ to_String => sub { $_[0]->slots->{_name} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
568 70 );
569 70 for my $name ( keys %zone_methods ) {
570 $zone_class->methods->{$name} = native_function(
571 name => $name,
572 140 native => $zone_methods{$name},
573 );
574 }
575  
576 my %duration_methods = map {
577 70 my $slot = "_$_";
578 0 ▶ $_ => sub { $_[0]->slots->{$slot} // 0 }
Subroutine __ANON__: not called CC=2 SCAR=17.9
Condition0%0/2
$_[0]->slots->{$slot} // 0
l!l
00
MC/DC0%0/1
$_[0]->slots->{$slot} // 0
$_[0]->slots->{$slot}
Truth table0%0/2
A
A= $_[0]->slots->{$slot}
Aresult
11
00
579 490 } qw( years months weeks days hours minutes seconds );
580 70 for my $name ( keys %duration_methods ) {
581 $duration_class->methods->{$name} = native_function(
582 name => $name,
583 490 native => $duration_methods{$name},
584 );
585 }
586  
587 my %methods = (
588 0 ▶ sec => sub { _datetime_from_self( $_[0] )->second },
Subroutine __ANON__: not called CC=1 SCAR=6.9
589 0 ▶ min => sub { _datetime_from_self( $_[0] )->minute },
Subroutine __ANON__: not called CC=1 SCAR=6.9
590 0 ▶ hour => sub { _datetime_from_self( $_[0] )->hour },
Subroutine __ANON__: not called CC=1 SCAR=6.9
591 0 ▶ day_of_month => sub { _datetime_from_self( $_[0] )->day_of_month },
Subroutine __ANON__: not called CC=1 SCAR=6.9
592 0 ▶ mon => sub { _datetime_from_self( $_[0] )->month },
Subroutine __ANON__: not called CC=1 SCAR=6.9
593 0 ▶ month => sub { $MONTH_ABBR[ _datetime_from_self( $_[0] )->month - 1 ] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
594 0 ▶ year => sub { _datetime_from_self( $_[0] )->year },
Subroutine __ANON__: not called CC=1 SCAR=6.9
595 0 ▶ yy => sub { sprintf '%02d', _datetime_from_self( $_[0] )->year % 100 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
596 0 ▶ day_of_week => sub { _datetime_from_self( $_[0] )->day_of_week },
Subroutine __ANON__: not called CC=1 SCAR=6.9
597 0 ▶ day => sub { $DAY_ABBR[ _datetime_from_self( $_[0] )->day_of_week - 1 ] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
598 0 ▶ day_of_year => sub { _datetime_from_self( $_[0] )->day_of_year - 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
599 0 ▶ month_last_day => sub { _datetime_from_self( $_[0] )->month_length },
Subroutine __ANON__: not called CC=1 SCAR=6.9
600 0 ▶ hms => sub { _datetime_from_self( $_[0] )->hms( @_ > 1 ? $_[1] : ':' ) },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 0 0 0
601 0 ▶ ymd => sub { _datetime_from_self( $_[0] )->ymd( @_ > 1 ? $_[1] : '-' ) },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 0 0 0
602 0 ▶ mdy => sub { _datetime_from_self( $_[0] )->mdy( @_ > 1 ? $_[1] : '-' ) },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 0 0 0
603 0 ▶ dmy => sub { _datetime_from_self( $_[0] )->dmy( @_ > 1 ? $_[1] : '-' ) },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 0 0 0
604 0 ▶ date => sub { _datetime_from_self( $_[0] )->ymd },
Subroutine __ANON__: not called CC=1 SCAR=6.9
605 0 ▶ time => sub { _datetime_from_self( $_[0] )->hms },
Subroutine __ANON__: not called CC=1 SCAR=6.9
606 0 ▶ datetime => sub { _datetime_from_self( $_[0] )->datetime },
Subroutine __ANON__: not called CC=1 SCAR=6.9
607 0 ▶ to_String => sub { _datetime_from_self( $_[0] )->datetime },
Subroutine __ANON__: not called CC=1 SCAR=6.9
608 0 ▶ cdate => sub { _datetime_from_self( $_[0] )->strftime('%a %b %e %H:%M:%S %Y') },
Subroutine __ANON__: not called CC=1 SCAR=6.9
609 0 ▶ epoch => sub { _epoch_from_self( $_[0] ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
610 0 ▶ tzoffset => sub { _datetime_from_self( $_[0] )->offset },
Subroutine __ANON__: not called CC=1 SCAR=6.9
611 0 ▶ is_leap_year => sub { _datetime_from_self( $_[0] )->is_leap_year ? 1 : 0 },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
_datetime_from_self($_[0])->is_leap_year ? : 0 0 0
612 0 ▶ week => sub { _datetime_from_self( $_[0] )->week_number },
Subroutine __ANON__: not called CC=1 SCAR=6.9
613 0 ▶ week_year => sub { _datetime_from_self( $_[0] )->week_year },
Subroutine __ANON__: not called CC=1 SCAR=6.9
614 0 ▶ julian_day => sub { _datetime_from_self( $_[0] )->jd },
Subroutine __ANON__: not called CC=1 SCAR=6.9
615 0 ▶ timezone => sub { _wrap_timezone( $zone_class, _zone_from_self( $_[0] ) ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
616 0 ▶ as_utc => sub { _wrap_time( $time_class, _epoch_from_self( $_[0] ), 'UTC' ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
617 0 ▶ as_local => sub { _wrap_time( $time_class, _epoch_from_self( $_[0] ), 'local' ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
618 0 ▶ to_iso8601 => sub { _format_rfc3339( _datetime_from_self( $_[0] ) ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
619 0 ▶ to_rfc3339 => sub { _format_rfc3339( _datetime_from_self( $_[0] ) ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
620 70 );
621  
622 70 for my $name ( sort keys %methods ) {
623 $time_class->methods->{$name} = native_function(
624 name => $name,
625 2240 native => $methods{$name},
626 );
627 }
628  
629 $time_class->methods->{to_rfc5322} = native_function(
630 name => 'to_rfc5322',
631 accepts_named => 1,
632 0 ▶ native => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
633 0 my ( $self, @rest ) = @_;
634 0 ▶ my $named = ref( $rest[-2] ) eq 'HASH' ? $rest[-2] : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $rest[-2] eq 'HASH' ? : 0 0 0
635 0 ▶ my $include = exists $named->{include_weekday} ? $named->{include_weekday} : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $named->{'include_weekday'} ? : 0 0 0
636 0 return _format_rfc5322( _datetime_from_self($self), $include );
637 },
638 70 );
639  
640 $time_class->methods->{strftime} = native_function(
641 name => 'strftime',
642 0 ▶ native => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
643 0 my ( $self, $format ) = @_;
644 0 ▶ return _datetime_from_self($self)->strftime( defined $format ? "$format" : '' );
Branch: 1 branch
BranchTrueFalseTotal
defined $format ? : 0 0 0
645 },
646 70 );
647  
648 $time_class->methods->{with_timezone} = native_function(
649 name => 'with_timezone',
650 0 ▶ native => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
651 0 my ( $self, $zone ) = @_;
652 0 return _wrap_time( $time_class, _epoch_from_self($self), $zone );
653 },
654 70 );
655  
656 $time_class->methods->{reinterpret_timezone} = native_function(
657 name => 'reinterpret_timezone',
658 0 ▶ native => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
659 0 my ( $self, $zone ) = @_;
660 0 my $dt = _datetime_from_self($self);
661 0 my $new_zone = _timezone_label($zone);
662 0 my $epoch = _same_wall_epoch(
663 $new_zone,
664 year => $dt->year,
665 month => $dt->month,
666 day => $dt->day,
667 hour => $dt->hour,
668 minute => $dt->minute,
669 second => $dt->second,
670 fraction => _fractional_part( _epoch_from_self($self) ),
671 );
672 0 return _wrap_time( $time_class, $epoch, $new_zone );
673 },
674 70 );
675  
676 70 for my $unit ( qw( seconds minutes hours ) ) {
677 210 ▶ my $seconds = $unit eq 'seconds' ? 1 : $unit eq 'minutes' ? 60 : 3600;
Branch: 2 branches
BranchTrueFalseTotal
$unit eq 'seconds' ? : 70 140 210
$unit eq 'minutes' ? : 70 70 140
678 $time_class->methods->{"add_$unit"} = native_function(
679 name => "add_$unit",
680 0 ▶ native => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
681 0 my ( $self, $count ) = @_;
682 0 ▶ return _wrap_time(
Condition0%0/2
$count // 0
l!l
00
MC/DC0%0/1
$count // 0
$count
Truth table0%0/2
A
A= $count
Aresult
11
00
683 $time_class,
684 _epoch_from_self($self) + ( 0 + ( $count // 0 ) ) * $seconds,
685 _zone_from_self($self),
686 );
687 },
688 210 );
689 $time_class->methods->{"subtract_$unit"} = native_function(
690 name => "subtract_$unit",
691 0 ▶ native => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
692 0 my ( $self, $count ) = @_;
693 0 ▶ return $time_class->methods->{"add_$unit"}->{_native}->( $self, -( 0 + ( $count // 0 ) ) );
Condition0%0/2
$count // 0
l!l
00
MC/DC0%0/1
$count // 0
$count
Truth table0%0/2
A
A= $count
Aresult
11
00
694 },
695 210 );
696 }
697  
698 70 for my $unit ( qw( days weeks months years ) ) {
699 $time_class->methods->{"add_$unit"} = native_function(
700 name => "add_$unit",
701 0 ▶ native => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
702 0 my ( $self, $count ) = @_;
703 0 ▶ return _calendar_add( $time_class, $self, $unit => 0 + ( $count // 0 ) );
Condition0%0/2
$count // 0
l!l
00
MC/DC0%0/1
$count // 0
$count
Truth table0%0/2
A
A= $count
Aresult
11
00
704 },
705 280 );
706 $time_class->methods->{"subtract_$unit"} = native_function(
707 name => "subtract_$unit",
708 0 ▶ native => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
709 0 my ( $self, $count ) = @_;
710 0 ▶ return _calendar_add( $time_class, $self, $unit => -( 0 + ( $count // 0 ) ) );
Condition0%0/2
$count // 0
l!l
00
MC/DC0%0/1
$count // 0
$count
Truth table0%0/2
A
A= $count
Aresult
11
00
711 },
712 280 );
713 }
714  
715 $time_class->methods->{add} = native_function(
716 name => 'add',
717 0 ▶ native => sub { _add_duration( $time_class, $_[0], $_[1], 1 ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
718 70 );
719 $time_class->methods->{subtract} = native_function(
720 name => 'subtract',
721 0 ▶ native => sub { _add_duration( $time_class, $_[0], $_[1], -1 ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
722 70 );
723 $time_class->methods->{elapsed_seconds_until} = native_function(
724 name => 'elapsed_seconds_until',
725 0 ▶ native => sub { _epoch_from_self( $_[1] ) - _epoch_from_self( $_[0] ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
726 70 );
727 $time_class->methods->{compare} = native_function(
728 name => 'compare',
729 0 ▶ native => sub { _epoch_from_self( $_[0] ) <=> _epoch_from_self( $_[1] ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
730 70 );
731 $time_class->methods->{is_before} = native_function(
732 name => 'is_before',
733 0 ▶ native => sub { _epoch_from_self( $_[0] ) < _epoch_from_self( $_[1] ) ? 1 : 0 },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
_epoch_from_self($_[0]) < _epoch_from_self($_[1]) ? : 0 0 0
734 70 );
735 $time_class->methods->{is_after} = native_function(
736 name => 'is_after',
737 0 ▶ native => sub { _epoch_from_self( $_[0] ) > _epoch_from_self( $_[1] ) ? 1 : 0 },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
_epoch_from_self($_[0]) > _epoch_from_self($_[1]) ? : 0 0 0
738 70 );
739 $time_class->methods->{format} = native_function(
740 name => 'format',
741 0 ▶ native => sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
742 0 my ( $self, $format ) = @_;
743 0 ▶ my $zone = $format->slots->{_timezone} // _zone_from_self($self);
Condition0%0/3
$format->slots->{"_timezone"} // _zone_from_self($self)
l!l&&r!l&&!r
000
MC/DC0%0/2
$format->slots->{"_timezone"} // _zone_from_self($self)
$format->slots->{"_timezone"} _zone_from_self($self)
Truth table0%0/3
A // B
A= $format->slots->{"_timezone"}B= _zone_from_self($self)
ABresult
1X1
011
000
744 0 my $dt = _datetime_from_epoch( _epoch_from_self($self), $zone );
745 0 my $kind = $format->slots->{_kind};
746 0 ▶ return _format_rfc3339($dt) if $kind eq 'iso8601' or $kind eq 'rfc3339';
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "iso8601" or $kind eq "rfc3339" 0 0 0
Condition0%0/3
$kind eq "iso8601" or $kind eq "rfc3339"
l!l&&r!l&&!r
000
MC/DC0%0/2
$kind eq "iso8601" or $kind eq "rfc3339"
$kind eq "iso8601" $kind eq "rfc3339"
Truth table0%0/3
A or B
A= $kind eq "iso8601"B= $kind eq "rfc3339"
ABresult
1X1
011
000
747 0 ▶ return _format_rfc5322( $dt, 1 ) if $kind eq 'rfc5322';
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "rfc5322" 0 0 0
748 0 ▶ return $dt->strftime( $format->slots->{_pattern} // '' );
Condition0%0/2
$format->slots->{"_pattern"} // ""
l!l
00
MC/DC0%0/1
$format->slots->{"_pattern"} // ""
$format->slots->{"_pattern"}
Truth table0%0/2
A
A= $format->slots->{"_pattern"}
Aresult
11
00
749 },
750 70 );
751  
752 $format_class->methods->{format} = native_function(
753 name => 'format',
754 0 ▶ native => sub { $time_class->methods->{format}->{_native}->( $_[1], $_[0] ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
755 70 );
756 $format_class->methods->{parse} = native_function(
757 name => 'parse',
758 accepts_named => 1,
759 0 ▶ native => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
760 0 my ( $self, $raw, @rest ) = @_;
761 0 ▶ my $named = ref( $rest[-2] ) eq 'HASH' ? $rest[-2] : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $rest[-2] eq 'HASH' ? : 0 0 0
762 0 my $zone = $self->slots->{_timezone};
763 0 ▶ $zone = _timezone_label( $named->{timezone} ) if exists $named->{timezone};
Branch: 1 branch
BranchTrueFalseTotal
if exists $named->{"timezone"} 0 0 0
764 0 my ( $epoch, $parsed_zone ) = _parse_time_value( "$raw", $zone, 1 );
765 0 ▶ return _wrap_time( $time_class, $epoch, $zone // $parsed_zone );
Condition0%0/3
$zone // $parsed_zone
l!l&&r!l&&!r
000
MC/DC0%0/2
$zone // $parsed_zone
$zone $parsed_zone
Truth table0%0/3
A // B
A= $zoneB= $parsed_zone
ABresult
1X1
011
000
766 },
767 70 );
768  
769 0 ▶ $parser_class->native_constructor( sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
770 0 my ( $rt, $klass, $positional, $named ) = @_;
771 0 ▶ my $format = @{ $positional // [] } ? $positional->[0] : $named->{format};
Branch: 1 branch
BranchTrueFalseTotal
@{$positional // [];} ? : 0 0 0
Condition0%0/2
$positional // []
l!l
00
MC/DC0%0/1
$positional // []
$positional
Truth table0%0/2
A
A= $positional
Aresult
11
00
772 0 ▶ $format = '%Y-%m-%d' if !defined $format;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $format 0 0 0
773 0 return native_object(
774 class => $klass,
775 slots => { _format => "$format" },
776 const => { _format => 1 },
777 );
778 70 } );
779  
780 $parser_class->methods->{parse} = native_function(
781 name => 'parse',
782 0 ▶ native => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
783 0 my ( $self, $raw ) = @_;
784 0 ▶ my $epoch = _parse_legacy_strptime( defined $raw ? "$raw" : '', $self->slots->{_format} );
Branch: 1 branch
BranchTrueFalseTotal
defined $raw ? : 0 0 0
785 0 return _wrap_time( $time_class, $epoch, 'UTC' );
786 },
787 70 );
788  
789 return {
790 70 Time => $time_class,
791 TimeZone => $zone_class,
792 Duration => $duration_class,
793 TimeFormat => $format_class,
794 TimeParser => $parser_class,
795 };
796 }
797  
798 11 1;
799  
800 =pod
801  
802 =head1 NAME
803  
804 Zuzu::Module::Time - std/time bindings for ZuzuScript.
805  
806 =head1 DESCRIPTION
807  
808 Implements the C<std/time> module, exporting immutable C<Time> objects and
809 timezone, duration, and formatting helpers. The original C<Time> and
810 C<TimeParser> API remains available.
811  
812 =head1 COPYRIGHT AND LICENCE
813  
814 B<< Zuzu::Module::Time >> is copyright Toby Inkster.
815  
816 It is free software; you may redistribute it and/or modify it under
817 the terms of either the Artistic License 1.0 or the GNU General Public
818 License version 2.
819  
820 =cut
« blib/lib/Zuzu/Module/String.pm ↑ Summary blib/lib/Zuzu/Module/DNS.pm »