blib/lib/PHP/DateTime.pm

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