FFI/tm.pm

filter: Statementstmt 79.0% 83 / 105 Branchbran 44.1% 15 / 34 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 86.3% 19 / 22 Podpod 75.0% 9 / 12 totaltotal 69.9% 128 / 183 CC 20 SCAR 25.6
CC
20
Cov
69%
_to_object
36.0
from_object
8.8
epoch
6.0
CRAP 12.9
↑ Summary FFI.pm »
1 package Time::FFI::tm;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use FFI::Platypus::Record ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Module::Runtime ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Time::Local ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 our $VERSION = '2.002';
11  
12 3 my @tm_members = qw(sec min hour mday mon year wday yday isdst);
13  
14 FFI::Platypus::Record::record_layout_1(
15 3 (map { (int => $_) } @tm_members),
16 long => 'gmtoff',
17 string => 'zone',
18 );
19  
20 {
21 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
22 3 *{"tm_$_"} = \&$_ for @tm_members, 'gmtoff', 'zone';
23 }
24  
25 4 ▶sub from_list {
Subroutine from_list: called CC=1 SCAR=0.0
Pod documented
26 4 my ($class, @args) = @_;
27 4 my %attr = map { ($tm_members[$_] => $args[$_]) } 0..$#tm_members;
28 4 return $class->new(\%attr);
29 }
30  
31 4 ▶sub from_object {
Subroutine from_object: called CC=6 SCAR=21.8
Pod documented
32 4 my ($class, $obj) = @_;
33 4 ▶ if ($obj->isa('Time::Piece')) {
Branch: 4 branches
BranchTrueFalseTotal
if ($obj->isa('Time::Piece')) { } 2 2 4
elsif ($obj->isa('Time::Moment')) { } 0 2 2
elsif ($obj->isa('DateTime')) { } 0 2 2
elsif ($obj->isa('Time::FFI::tm') or $obj->isa('Time::tm')) { } 2 0 2
34 2 return $class->new(
35 year => $obj->year - 1900,
36 mon => $obj->mon - 1,
37 mday => $obj->mday,
38 hour => $obj->hour,
39 min => $obj->min,
40 sec => $obj->sec,
41 isdst => -1,
42 );
43 } elsif ($obj->isa('Time::Moment')) {
44 0 return $class->new(
45 year => $obj->year - 1900,
46 mon => $obj->month - 1,
47 mday => $obj->day_of_month,
48 hour => $obj->hour,
49 min => $obj->minute,
50 sec => $obj->second,
51 isdst => -1,
52 );
53 } elsif ($obj->isa('DateTime')) {
54 0 return $class->new(
55 year => $obj->year - 1900,
56 mon => $obj->month - 1,
57 mday => $obj->day,
58 hour => $obj->hour,
59 min => $obj->minute,
60 sec => $obj->second,
61 isdst => -1,
62 );
63 ▶ } elsif ($obj->isa('Time::FFI::tm') or $obj->isa('Time::tm')) {
Condition33%1/3
$obj->isa("Time::FFI::tm") or $obj->isa("Time::tm")
l!l&&r!l&&!r
020
MC/DC0%0/2
$obj->isa("Time::FFI::tm") or $obj->isa("Time::tm")
$obj->isa("Time::FFI::tm") $obj->isa("Time::tm")
Truth table33%1/3
A or B
A= $obj->isa("Time::FFI::tm")B= $obj->isa("Time::tm")
ABresult
1X1
011
000
64 2 my %attr = map { ($_ => $obj->$_) } qw(sec min hour mday mon year wday yday isdst);
65 2 return $class->new(\%attr);
66 } else {
67 0 my $class = ref $obj;
68 0 Carp::croak "Cannot convert from unrecognized object class $class";
69 }
70 }
71  
72 2 ▶sub to_list {
Subroutine to_list: called CC=1 SCAR=0.0
Pod documented
73 2 my ($self) = @_;
74 2 return map { $self->$_ } @tm_members;
75 }
76  
77 0 ▶sub to_object {
Subroutine to_object: not called CC=1 SCAR=6.9
Pod undocumented
78 0 Carp::carp '->to_object is deprecated; use ->to_object_as_local or ->to_object_as_utc';
79 0 return _to_object(@_);
80 }
81  
82 3 ▶sub to_object_as_local {
Subroutine to_object_as_local: called CC=1 SCAR=0.0
Pod documented
83 3 my ($self, $class) = @_;
84 3 return _to_object($self, $class, 1);
85 }
86  
87 2 ▶sub to_object_as_utc {
Subroutine to_object_as_utc: called CC=1 SCAR=0.0
Pod documented
88 2 my ($self, $class) = @_;
89 2 return _to_object($self, $class, 0);
90 }
91  
92 5 ▶sub _to_object {
Subroutine _to_object: called CC=12 SCAR=35.8
93 5 my ($self, $class, $islocal) = @_;
94 5 Module::Runtime::require_module $class;
95 5 ▶ if ($class->isa('Time::Piece')) {
Branch: 4 branches
BranchTrueFalseTotal
if ($class->isa('Time::Piece')) { } 3 2 5
elsif ($class->isa('Time::Moment')) { } 0 2 2
elsif ($class->isa('DateTime')) { } 0 2 2
elsif ($class->isa('Time::FFI::tm') or $class->isa('Time::tm')) { } 2 0 2
96 3 ▶ my ($epoch) = $islocal ? _mktime($self) : _timegm($self);
Branch: 1 branch
BranchTrueFalseTotal
$islocal ? : 2 1 3
97 3 ▶ return $islocal ? scalar $class->localtime($epoch) : scalar $class->gmtime($epoch);
Branch: 1 branch
BranchTrueFalseTotal
$islocal ? : 2 1 3
98 } elsif ($class->isa('Time::Moment')) {
99 0 my $tm = $self;
100 0 my $epoch;
101 0 ▶ ($epoch, $tm) = _mktime($self) if $islocal;
Branch: 1 branch
BranchTrueFalseTotal
if $islocal 0 0 0
102 0 my $moment = $class->new(
103 year => $tm->year + 1900,
104 month => $tm->mon + 1,
105 day => $tm->mday,
106 hour => $tm->hour,
107 minute => $tm->min,
108 second => $tm->sec,
109 );
110 0 ▶ return $islocal ? $moment->with_offset_same_local(($moment->epoch - $epoch) / 60) : $moment;
Branch: 1 branch
BranchTrueFalseTotal
$islocal ? : 0 0 0
111 } elsif ($class->isa('DateTime')) {
112 0 my $tm = $self;
113 0 ▶ (undef, $tm) = _mktime($self) if $islocal;
Branch: 1 branch
BranchTrueFalseTotal
if $islocal 0 0 0
114 0 ▶ return $class->new(
Branch: 1 branch
BranchTrueFalseTotal
$islocal ? : 0 0 0
115 year => $tm->year + 1900,
116 month => $tm->mon + 1,
117 day => $tm->mday,
118 hour => $tm->hour,
119 minute => $tm->min,
120 second => $tm->sec,
121 time_zone => $islocal ? 'local' : 'UTC',
122 );
123 ▶ } elsif ($class->isa('Time::FFI::tm') or $class->isa('Time::tm')) {
Condition33%1/3
$class->isa("Time::FFI::tm") or $class->isa("Time::tm")
l!l&&r!l&&!r
020
MC/DC0%0/2
$class->isa("Time::FFI::tm") or $class->isa("Time::tm")
$class->isa("Time::FFI::tm") $class->isa("Time::tm")
Truth table33%1/3
A or B
A= $class->isa("Time::FFI::tm")B= $class->isa("Time::tm")
ABresult
1X1
011
000
124 2 my %attr = map { ($_ => $self->$_) } qw(sec min hour mday mon year wday yday isdst);
125 2 return $class->new(%attr);
126 } else {
127 0 Carp::croak "Cannot convert to unrecognized object class $class";
128 }
129 }
130  
131 0 ▶sub epoch {
Subroutine epoch: not called CC=2 SCAR=17.9
Pod undocumented
132 0 my ($self, $islocal) = @_;
133 0 Carp::carp '->epoch is deprecated; use ->epoch_as_local or ->epoch_as_utc';
134 0 ▶ my ($epoch) = $islocal ? _mktime($self) : _timegm($self);
Branch: 1 branch
BranchTrueFalseTotal
$islocal ? : 0 0 0
135 0 return $epoch;
136 }
137  
138 4 ▶sub epoch_as_local {
Subroutine epoch_as_local: called CC=1 SCAR=0.0
Pod documented
139 4 my ($self) = @_;
140 4 my ($epoch) = _mktime($self);
141 4 return $epoch;
142 }
143  
144 4 ▶sub epoch_as_utc {
Subroutine epoch_as_utc: called CC=1 SCAR=0.0
Pod documented
145 4 my ($self) = @_;
146 4 my ($epoch) = _timegm($self);
147 4 return $epoch;
148 }
149  
150 0 ▶sub normalized {
Subroutine normalized: not called CC=2 SCAR=17.9
Pod undocumented
151 0 my ($self, $islocal) = @_;
152 0 Carp::carp '->normalized is deprecated; use ->normalized_as_local or ->normalized_as_utc';
153 0 ▶ return $islocal ? $self->normalized_as_local : $self->normalized_as_utc;
Branch: 1 branch
BranchTrueFalseTotal
$islocal ? : 0 0 0
154 }
155 3 *with_extra = \&normalized;
156  
157 1 ▶sub normalized_as_local {
Subroutine normalized_as_local: called CC=1 SCAR=0.0
Pod documented
158 1 my ($self) = @_;
159 1 my (undef, $new) = _mktime($self);
160 1 return $new;
161 }
162  
163 1 ▶sub normalized_as_utc {
Subroutine normalized_as_utc: called CC=1 SCAR=0.0
Pod documented
164 1 my ($self) = @_;
165 1 my ($epoch) = _timegm($self);
166 1 require Time::FFI;
167 1 my $new = Time::FFI::gmtime($epoch);
168 1 bless $new, ref $self;
169 1 return $new;
170 }
171  
172 7 ▶sub _mktime {
Subroutine _mktime: called CC=1 SCAR=0.0
173 7 my ($self) = @_;
174 7 require Time::FFI;
175 7 my %attr = map { ($_ => $self->$_) } qw(sec min hour mday mon year);
176 7 $attr{isdst} = -1;
177 7 my $new = (ref $self)->new(\%attr);
178 7 return (Time::FFI::mktime($new), $new);
179 }
180  
181 6 ▶sub _timegm {
Subroutine _timegm: called CC=2 SCAR=7.0
182 6 my ($self) = @_;
183 6 my $year = $self->year;
184 6 ▶ $year += 1900 if $year >= 0; # avoid timegm year heuristic
Branch: 1 branch
BranchTrueFalseTotal
if $year >= 0 6 0 6
185 6 my @vals = ((map { $self->$_ } qw(sec min hour mday mon)), $year);
186 6 return scalar Time::Local::timegm(@vals);
187 }
188  
189 3 1;
190  
191 =head1 NAME
192  
193 Time::FFI::tm - POSIX tm record structure
194  
195 =head1 SYNOPSIS
196  
197 use Time::FFI::tm;
198  
199 my $tm = Time::FFI::tm->new(
200 year => 95, # years since 1900
201 mon => 0, # 0 == January
202 mday => 1,
203 hour => 13,
204 min => 25,
205 sec => 59,
206 isdst => -1, # allow DST status to be determined by the system
207 );
208 $tm->mday($tm->mday + 1); # add a day
209  
210 my $in_local = $tm->normalized_as_local;
211 say $in_local->isdst; # now knows if DST is active
212  
213 my $tm = Time::FFI::tm->from_list(CORE::localtime(time));
214  
215 my $epoch = POSIX::mktime($tm->to_list);
216 my $epoch = $tm->epoch_as_local;
217  
218 my $tm = Time::FFI::tm->from_object(Time::Moment->now);
219 my $datetime = $tm->to_object_as_local('DateTime');
220  
221 =head1 DESCRIPTION
222  
223 This L<FFI::Platypus::Record> class represents the C<tm> struct defined by
224 F<time.h> and used by functions such as L<mktime(3)> and L<strptime(3)>. This
225 is used by L<Time::FFI> to provide access to such structures.
226  
227 The structure does not store an explicit time zone, so you must specify whether
228 to interpret it as local or UTC time whenever rendering it to an actual
229 datetime.
230  
231 =head1 ATTRIBUTES
232  
233 The integer components of the C<tm> struct are stored as settable attributes
234 that default to 0.
235  
236 Note that 0 is out of the standard range for the C<mday> value (often
237 indicating the last day of the previous month), and C<isdst> should be set to a
238 negative value if unknown, so these values should always be specified
239 explicitly.
240  
241 Each attribute also has a corresponding alias starting with C<tm_> to match the
242 standard C<tm> struct member names.
243  
244 =head2 sec
245  
246 Seconds [0,60].
247  
248 =head2 min
249  
250 Minutes [0,59].
251  
252 =head2 hour
253  
254 Hour [0,23].
255  
256 =head2 mday
257  
258 Day of month [1,31].
259  
260 =head2 mon
261  
262 Month of year [0,11].
263  
264 =head2 year
265  
266 Years since 1900.
267  
268 =head2 wday
269  
270 Day of week [0,6] (Sunday =0).
271  
272 =head2 yday
273  
274 Day of year [0,365].
275  
276 =head2 isdst
277  
278 Daylight Savings flag. (0: off, positive: on, negative: unknown)
279  
280 =head2 gmtoff
281  
282 Seconds east of UTC. (May not be available on all systems)
283  
284 =head2 zone
285  
286 Timezone abbreviation. (Read only string, may not be available on all systems)
287  
288 =head1 METHODS
289  
290 =head2 new
291  
292 my $tm = Time::FFI::tm->new;
293 my $tm = Time::FFI::tm->new(year => $year, ...);
294 my $tm = Time::FFI::tm->new({year => $year, ...});
295  
296 Construct a new B<Time::FFI::tm> object representing a C<tm> struct.
297  
298 =head2 from_list
299  
300 my $tm = Time::FFI::tm->from_list($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst);
301  
302 Construct a new B<Time::FFI::tm> object from the passed list of time
303 attributes, in the same order returned by L<perlfunc/localtime>. Missing or
304 undefined values will be interpreted as the default of 0, but see
305 L</ATTRIBUTES>.
306  
307 =head2 from_object
308  
309 my $tm = Time::FFI::tm->from_object($obj);
310  
311 I<Current API since version 2.000.>
312  
313 Construct a new B<Time::FFI::tm> object from the passed datetime object's local
314 datetime components. Currently L<Time::Piece>, L<Time::Moment>, L<DateTime>,
315 L<Time::tm>, and L<Time::FFI::tm> objects (and subclasses) are recognized. The
316 original time zone and any fractional seconds will not be represented in the
317 resulting structure.
318  
319 =head2 to_list
320  
321 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = $tm->to_list;
322  
323 Return the list of time attributes in the structure, in the same order returned
324 by L<perlfunc/localtime>.
325  
326 =head2 to_object_as_local
327  
328 =head2 to_object_as_utc
329  
330 my $piece = $tm->to_object_as_local('Time::Piece');
331 my $moment = $tm->to_object_as_utc('Time::Moment');
332  
333 I<Since version 2.002.>
334  
335 Return an object of the specified class. Currently L<Time::Piece>,
336 L<Time::Moment>, and L<DateTime> (or subclasses) are recognized. Depending on
337 the method called, the time attributes are interpreted in the local time zone
338 or in UTC.
339  
340 When interpreted as a local time, values outside the standard ranges are
341 accepted; this is not currently supported for UTC times.
342  
343 You may also specify L<Time::tm> or L<Time::FFI::tm> (or subclasses), in which
344 case C<to_object_as_local> and C<to_object_as_utc> produce the same result with
345 the time attributes copied as-is.
346  
347 =head2 epoch_as_local
348  
349 =head2 epoch_as_utc
350  
351 my $epoch = $tm->epoch_as_local;
352 my $epoch = $tm->epoch_as_utc
353  
354 I<Since version 2.002.>
355  
356 Translate the time structure into a Unix epoch timestamp (seconds since
357 1970-01-01 UTC). Depending on the method called, the time attributes are
358 interpreted in the local time zone or in UTC.
359  
360 When interpreted as a local time, values outside the standard ranges are
361 accepted; this is not currently supported for UTC times.
362  
363 =head2 normalized_as_local
364  
365 =head2 normalized_as_utc
366  
367 my $new = $tm->normalized_as_local;
368 my $new = $tm->normalized_as_utc;
369  
370 I<Since version 2.002.>
371  
372 Return a new B<Time::FFI::tm> object representing the same time, but with
373 C<wday>, C<yday>, C<isdst>, and (if supported) C<gmtoff> and C<zone> set
374 appropriately. Depending on the method called, the time attributes are
375 interpreted in the local time zone or in UTC.
376  
377 When interpreted as a local time, values outside the standard ranges will also
378 be normalized; this is not currently supported for UTC times.
379  
380 =head1 BUGS
381  
382 Report any issues on the public bugtracker.
383  
384 =head1 AUTHOR
385  
386 Dan Book <dbook@cpan.org>
387  
388 =head1 COPYRIGHT AND LICENSE
389  
390 This software is Copyright (c) 2019 by Dan Book.
391  
392 This is free software, licensed under:
393  
394 The Artistic License 2.0 (GPL Compatible)
395  
396 =head1 SEE ALSO
397  
398 L<Time::FFI>, L<Time::tm>
399  
400 =for Pod::Coverage with_extra to_object epoch normalized tm_sec tm_min tm_hour tm_mday tm_mon tm_year tm_wday tm_yday tm_isdst tm_gmtoff tm_zone
↑ Summary FFI.pm »