blib/lib/Time/timegm.pm

filter: Statementstmt 70.0% 35 / 50 Branchbran 7.1% 1 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 90.0% 9 / 10 Podpod 0.0% 0 / 1 totaltotal 60.0% 45 / 75 CC 8 SCAR 35.4
CC
8
Cov
56%
timegm_pp
72.0
BEGIN
1.0
BEGIN
1.0
CRAP 34.4
↑ Summary lib/Time/timegm.xs »
1 # You may distribute under the terms of either the GNU General Public License
2 # or the Artistic License (the same terms as Perl itself)
3 #
4 # (C) Paul Evans, 2012 -- leonerd@leonerd.org.uk
5  
6 package Time::timegm;
7  
8 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 our $VERSION = '0.01';
12  
13 2 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2 our @EXPORT_OK = qw(
16 timegm
17 );
18  
19 2 ▶unless( eval {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { require XSLoader; XSLoader::load(__PACKAGE__, $Time::timegm::VERSION); defined timegm(0, 0, 0, 0, 1, 70) } }) 0 2 2
20 2 require XSLoader;
21 2 XSLoader::load( __PACKAGE__, $VERSION );
22 2 defined Time::timegm::timegm( 0, 0, 0, 0, 1, 70 )
23 } ) {
24 # Fallback on pureperl implementation
25 0 require POSIX;
26 2 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
27 0 *timegm = \&timegm_pp;
28 }
29  
30 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 2 ▶use POSIX qw( mktime );
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 # Number of seconds in a POSIX day
35 2 ▶use constant DAY => 24*60*60;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 2 ▶use constant HOUR => 60*60;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 2 ▶use constant MIN => 60;
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 =head1 NAME
40  
41 C<Time::timegm> - a UTC version of C<mktime()>
42  
43 =head1 SYNOPSIS
44  
45 use Time::timegm qw( timegm );
46  
47 my $epoch = timegm( 0, 0, 0, 14, 6-1, 2012-1900 );
48  
49 print "2012-06-14 00:00:00 UTC happened at ",
50 scalar localtime($epoch), " localtime\n";
51  
52 =head1 DESCRIPTION
53  
54 The L<POSIX> standard provides three functions for converting between integer
55 epoch values and 6-component "broken-down" time representations. C<localtime>
56 and C<gmtime> convert an epoch into the 6 components of seconds, minutes,
57 hours, day of month, month and year, in either local timezone or UTC. The
58 C<mktime> function converts a local broken-down time into an epoch value.
59 However, C<POSIX> does not provide a UTC version of this.
60  
61 This module provides a function C<timegm> which has this ability.
62  
63 Unlike some other CPAN implementations of this behaviour, this version does
64 not re-implement the time handling logic internally. It reuses the C<mktime>
65 and C<gmtime> functions provided by the system to ensure its results are
66 always consistent with the other functions.
67  
68 =cut
69  
70 =head1 FUNCTIONS
71  
72 =cut
73  
74 =head2 $epoch = timegm( $sec, $min, $hour, $mday, $mon, $year )
75  
76 Returns the epoch integer value representing the time given by the 6
77 broken-down components.
78  
79 As with C<POSIX::mktime> it is I<not> required that these values be within
80 their "valid" ranges. This function will normalise values out of range. For
81 example, the 25th hour of a day is normalised to the 1st hour of the following
82 day; or the 0th month is normalised to the 12th month of the preceeding year.
83  
84 =cut
85  
86 # Cache "$year/$mon" => $epoch of the time that month begins
87 2 my %start_of_month;
88  
89 sub timegm_pp
90 0 ▶{
Subroutine timegm_pp: not called CC=8 SCAR=42.8
Pod undocumented
91 0 my ( $sec, $min, $hour, $mday, $mon, $year ) = @_;
92  
93 # Epoch times + UTC always align day boundaries at multiples of 86400. The
94 # structure is mathematically regular within a month. Therefore we can
95 # calculate the epoch time for a given time in UTC by finding the start of
96 # the month then adding seconds
97  
98 0 ▶ $mon-=12, $year++ while $mon >= 12;
Branch: 1 branch
BranchTrueFalseTotal
$mon >= 12 and do { $mon -= 12, ++$year } 0 0 0
99 0 ▶ $mon+=12, $year-- while $mon < 0;
Branch: 1 branch
BranchTrueFalseTotal
$mon < 0 and do { $mon += 12, --$year } 0 0 0
100  
101 0 my $monstart = $start_of_month{"$year/$mon"};
102 0 ▶ unless( defined $monstart ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $monstart) 0 0 0
103 0 $monstart = mktime( 0, 0, 0, 1, $mon, $year );
104 0 $monstart -= $monstart % DAY;
105  
106 0 my @gmtime = gmtime $monstart;
107 0 ▶ $gmtime[$_] == 0 or croak "Expected midnight GMT, did not get it - " . scalar( gmtime $monstart ) for 0 .. 2;
Branch: 1 branch
BranchTrueFalseTotal
$gmtime[$_] == 0 or croak('Expected midnight GMT, did not get it - ' . scalar(gmtime $monstart)) 0 0 0
108  
109 # Might have to round forward rather than backward
110 0 ▶ $monstart += DAY, @gmtime = gmtime $monstart if $gmtime[3] > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $gmtime[3] > 1 0 0 0
111  
112 0 ▶ $gmtime[3] == 1 or croak "Expected 1st of month GMT, did not get it - " . scalar( gmtime $monstart );
Branch: 1 branch
BranchTrueFalseTotal
$gmtime[3] == 1 or croak('Expected 1st of month GMT, did not get it - ' . scalar(gmtime $monstart)) 0 0 0
113  
114 0 $start_of_month{"$year/$mon"} = $monstart;
115 }
116  
117 0 return $monstart + ($mday-1)*DAY + $hour*HOUR + $min*MIN + $sec;
118 }
119  
120 =head1 COMPARISON WITH Time::Local
121  
122 The L<Time::Local> module also provides a function called C<timegm()> with
123 similar behaviour to this one. The differences are:
124  
125 =over 2
126  
127 =item *
128  
129 C<Time::timegm::timegm()> handles denormalised values (that is, seconds or
130 minutes outside of the range 0 to 59, hours outside 0 to 23, etc..) by
131 adjusting the next largest unit (such that 61 seconds is 1 second of the next
132 minute, etc). C<Time::Local::timegm()> croaks on out-of-range input.
133 C<Time::Local> also provides a function C<timegm_nocheck()> which does not
134 croak but it is documented that the behavior is unspecified on out-of-range
135 values.
136  
137 =item *
138  
139 C<Time::timegm::timegm()> is implemented by a light XS wrapper around the
140 C<timegm(3)> or C<_mkgmtime(3)> function provided by the platform's C library
141 if such a function is provided, so its behaviour is consistent with the rest
142 of the platform. C<Time::Local> re-implements the logic in perl code.
143 C<Time::timegm> will fall back to a perl implementation only if the XS one
144 cannot be used.
145  
146 =back
147  
148 =head1 AUTHOR
149  
150 Paul Evans <leonerd@leonerd.org.uk>
151  
152 =cut
153  
154 2 0x55AA;
↑ Summary lib/Time/timegm.xs »