blib/lib/PHP/DateTime.pm

filter: Statementstmt 91.1% 72 / 79 Branchbran 63.6% 28 / 44 Conditioncond 53.3% 8 / 15 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 100% 12 / 12 Podpod 100% 5 / 5 totaltotal 78.2% 126 / 161 CC 29 SCAR 26.0
CC
29
Cov
78%
date
25.2
checkdate
8.5
getdate
3.2
CRAP 13.5
↑ Summary Build »
1 package PHP::DateTime;
2  
3 1 $PHP::DateTime::VERSION = '0.07';
4  
5 =head1 NAME
6  
7 PHP::DateTime - Clone of PHP's date and time functions.
8  
9 =head1 SYNOPSIS
10  
11 use PHP::DateTime;
12
13 if( checkdate($month,$day,$year) ){ print 'The date is good.'; }
14
15 print date( $format, $time );
16 print date( $format ); # Defaults to the current time.
17
18 @d = getdate(); # A list at the current time.
19 @d = getdate($time); # A list at the specified time.
20 $d = getdate($time); # An array ref at the specified time.
21
22 my @g = gettimeofday(); # A list.
23 my $g = gettimeofday(); # An array ref.
24
25 my $then = mktime( $hour, $min, $sec, $month, $day, $year );
26  
27 =head1 DESCRIPTION
28  
29 Duplicates some of PHP's date and time functions. Why? I can't remember.
30 It should be useful if you are trying to integrate your perl app with a php app.
31 Much like PHP this module gratuitously exports all its functions upon a use().
32 Neat, eh?
33  
34 =cut
35  
36 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 1 ▶use Time::DaysInMonth qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
40 1 ▶use Time::Timezone qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
41 1 ▶use Time::HiRes qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
42 1 ▶use Time::Local qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 1 our $VERSION = '0.07';
45  
46 1 my $days_short = [qw( Sun Mon Tue Wed Thr Fri Sat )];
47 1 my $days_long = [qw( Sunday Monday Tuesday Wednesday Thursday Friday Saturday )];
48 1 my $months_short = [qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec )];
49 1 my $months_long = [qw( January February March April May June July August September October November December )];
50  
51 1 ▶use Exporter qw( import );
Subroutine BEGIN: called CC=1 SCAR=0.0
52 1 our @EXPORT = qw(
53 checkdate date getdate gettimeofday mktime
54 );
55  
56 =head1 METHODS
57  
58 All of these methods should match PHP's methods exactly.
59  
60 - Months are 1-12.
61 - Days are 1-31.
62 - Years are in four digit format (1997, not 97).
63  
64 =head2 checkdate
65  
66 if( checkdate($month,$day,$year) ){ print 'The date is good.'; }
67  
68 L<http://php.net/manual/en/function.checkdate.php>
69  
70 =cut
71  
72 sub checkdate {
73 2 ▶ my($month,$day,$year) = @_;
Subroutine checkdate: called CC=6 SCAR=21.4
Pod documented
74 return (
75 2 ▶ $year>=1 and $year<=32767 and
Condition53%8/15
$year >= 1 && $year <= 32767 && $month >= 1 && $month <= 12 && $day >= 1 && $day <= Time::DaysInMonth::days_in($year, $month)
!ll&&!rl&&r
101
$year >= 1 and $year <= 32767 and $month >= 1 and $month <= 12 and $day >= 1
!ll&&!rl&&r
101
$year >= 1 and $year <= 32767 and $month >= 1 and $month <= 12
!ll&&!rl&&r
011
$year >= 1 and $year <= 32767 and $month >= 1
!ll&&!rl&&r
002
$year >= 1 and $year <= 32767
!ll&&!rl&&r
002
MC/DC16%1/6
$year >= 1 && $year <= 32767 && $month >= 1 && $month <= 12 && $day >= 1 && $day <= Time::DaysInMonth::days_in($year, $month)
$year >= 1 $year <= 32767 $month >= 1 $month <= 12 $day >= 1 $day <= Time::DaysInMonth::days_in($year, $month)
Truth table28%2/7
A and B and C and D and E && F
A= $year >= 1B= $year <= 32767C= $month >= 1D= $month <= 12E= $day >= 1F= $day <= Time::DaysInMonth::days_in($year, $month)
ABCDEFresult
0XXXXX0
10XXXX0
110XXX0
1110XX0
11110X0
1111100
1111111
76 $month>=1 and $month<=12 and
77 $day>=1 and $day <= Time::DaysInMonth::days_in($year,$month)
78 );
79 }
80  
81 =head2 date
82  
83 print date( $format, $time );
84 print date( $format ); # Defaults to the current time.
85  
86 L<http://php.net/manual/en/function.date.php>
87  
88 =cut
89  
90 sub date {
91 1 ▶ my $format = shift;
Subroutine date: called CC=18 SCAR=32.3
Pod documented
92 1 ▶ my $esecs = (@_?shift():time());
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 1 0 1
93 1 my $tzoffset;
94 1 ▶ if(@_){
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 1 1
95 0 $tzoffset = shift;
96 0 ▶ if($tzoffset=~/^-?[0-9]+\.[0-9]+$/s){ $tzoffset=$tzoffset*60*60; }
Branch: 2 branches
BranchTrueFalseTotal
if ($tzoffset =~ /^-?[0-9]+\.[0-9]+$/s) { } 0 0 0
elsif ($tzoffset =~ /^(-?)([0-9]+):([0-9]+)$/s) { } 0 0 0
97 0 elsif($tzoffset=~/^(-?)([0-9]+):([0-9]+)$/s){ $tzoffset=(($1*$2*60)+($1*$3))*60; }
98 0 else{ $tzoffset+=0; }
99 }else{
100 1 $tzoffset = Time::Timezone::tz_local_offset();
101 }
102 1 $esecs += $tzoffset;
103 1 my @times = gmtime($esecs);
104  
105 1 my $str;
106 1 my @chars = split(//,$format);
107 1 foreach (@chars){
108 63 ▶ if($_ eq 'D'){ $str.=$days_short->[$times[6]]; }
Branch: 7 branches
BranchTrueFalseTotal
if ($_ eq 'D') { } 1 62 63
elsif ($_ eq 'M') { } 1 61 62
elsif ($_ eq 'd') { } 1 60 61
elsif ($_ eq 'Y') { } 1 59 60
elsif ($_ eq 'g') { } 1 58 59
elsif ($_ eq 'i') { } 1 57 58
elsif ($_ eq 'a') { } 1 56 57
109 1 elsif($_ eq 'M'){ $str.=$months_short->[$times[4]]; }
110 1 ▶ elsif($_ eq 'd'){ $str.=($times[3]<10?'0':'').$times[3]; }
Branch: 1 branch
BranchTrueFalseTotal
$times[3] < 10 ? : 1 0 1
111 1 elsif($_ eq 'Y'){ $str.=$times[5]+1900; }
112 1 ▶ elsif($_ eq 'g'){ $str.=($times[2]==0?12:$times[2]-($times[2]>12?12:0)); }
Branch: 2 branches
BranchTrueFalseTotal
$times[2] == 0 ? : 0 1 1
$times[2] > 12 ? : 0 1 1
113 1 ▶ elsif($_ eq 'i'){ $str.=($times[1]<10?'0':'').$times[1]; }
Branch: 1 branch
BranchTrueFalseTotal
$times[1] < 10 ? : 0 1 1
114 1 ▶ elsif($_ eq 'a'){ $str.=($times[2]>=12?'pm':'am'); }
Branch: 1 branch
BranchTrueFalseTotal
$times[2] >= 12 ? : 0 1 1
115 56 else{ $str.=$_; }
116 }
117  
118 1 return $str;
119 }
120  
121 =head2 getdate
122  
123 @d = getdate(); # A list at the current time.
124 @d = getdate($time); # A list at the specified time.
125 $d = getdate($time); # An array ref at the specified time.
126  
127 L<http://php.net/manual/en/function.getdate.php>
128  
129 =cut
130  
131 sub getdate {
132 1 ▶ my($esecs) = (@_?shift():time);
Subroutine getdate: called CC=3 SCAR=11.8
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 1 0 1
133 1 my @times = localtime($esecs);
134 1 @times = (
135 $times[0],$times[1],$times[2],
136 $times[3],$times[6],$times[4]+1,$times[5]+1900,$times[6],
137 $days_long->[$times[6]],$months_long->[$times[4]],
138 $esecs
139 );
140 1 ▶ if(wantarray){ return @times; }
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 1 0 1
141 0 else{ return [@times]; }
142 }
143  
144 =head2 gettimeofday
145  
146 my %g = gettimeofday(); # A hash.
147 my $g = gettimeofday(); # An hash ref.
148  
149 L<http://php.net/manual/en/function.gettimeofday.php>
150  
151 =cut
152  
153 sub gettimeofday {
154 1 ▶ my($sec,$usec) = Time::HiRes::gettimeofday();
Subroutine gettimeofday: called CC=3 SCAR=11.6
Pod documented
155 1 my $minuteswest = int((-1 * Time::Timezone::tz_local_offset())/60);
156 1 ▶ my $dsttime = ((localtime(time))[8]?1:0);
Branch: 1 branch
BranchTrueFalseTotal
(localtime time)[8] ? : 0 1 1
157 1 my %times = ( sec=>$sec,usec=>$usec,minuteswest=>$minuteswest,dsttime=>$dsttime );
158 1 ▶ if(wantarray){ return %times; }
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 1 1
159 1 else{ return {%times}; }
160 }
161  
162 =head2 mktime
163  
164 my $then = mktime( $hour, $min, $sec, $month, $day, $year );
165  
166 L<http://php.net/manual/en/function.mktime.php>
167  
168 =cut
169  
170 sub mktime {
171 # hour, minute, second, month, day, year, is_dst
172 2 ▶ my $times = [ ( localtime(time) )[2,1,0,4,3,5] ];
Subroutine mktime: called CC=3 SCAR=11.0
Pod documented
173 2 $times->[3]++;
174 2 $times->[5]+=1900;
175  
176 2 ▶ for( my $i=0; $i<@$times; $i++ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$times) 12 2 14
177 12 ▶ last if(!@_);
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 12 12
178 12 $times->[$i] = shift;
179 }
180  
181 2 $times->[3]--;
182 2 $times->[5]-=1900;
183 2 my $esecs = Time::Local::timelocal( (@$times)[2,1,0,4,3,5] );
184  
185 2 return $esecs;
186 }
187  
188 1 1;
189 __END__
↑ Summary Build »