File Coverage

blib/lib/PHP/DateTime.pm
Criterion Covered Total %
statement 64 71 90.1
branch 26 42 61.9
condition 2 3 66.6
subroutine 12 12 100.0
pod 5 5 100.0
total 109 133 81.9


line stmt bran cond sub pod time code
1             package PHP::DateTime;
2              
3             $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     1   200678 use strict;
  1         2  
  1         24  
37 1     1   4 use warnings;
  1         2  
  1         21  
38              
39 1     1   366 use Time::DaysInMonth qw();
  1         318  
  1         19  
40 1     1   378 use Time::Timezone qw();
  1         1279  
  1         23  
41 1     1   399 use Time::HiRes qw();
  1         1139  
  1         22  
42 1     1   367 use Time::Local qw();
  1         1767  
  1         58  
43              
44             our $VERSION = '0.07';
45              
46             my $days_short = [qw( Sun Mon Tue Wed Thr Fri Sat )];
47             my $days_long = [qw( Sunday Monday Tuesday Wednesday Thursday Friday Saturday )];
48             my $months_short = [qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec )];
49             my $months_long = [qw( January February March April May June July August September October November December )];
50              
51 1     1   6 use Exporter qw( import );
  1         2  
  1         587  
52             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
69              
70             =cut
71              
72             sub checkdate {
73 2     2 1 5107 my($month,$day,$year) = @_;
74             return (
75 2   66     38 $year>=1 and $year<=32767 and
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
87              
88             =cut
89              
90             sub date {
91 1     1 1 5 my $format = shift;
92 1 50       14 my $esecs = (@_?shift():time());
93 1         2 my $tzoffset;
94 1 50       3 if(@_){
95 0         0 $tzoffset = shift;
96 0 0       0 if($tzoffset=~/^-?[0-9]+\.[0-9]+$/s){ $tzoffset=$tzoffset*60*60; }
  0 0       0  
97 0         0 elsif($tzoffset=~/^(-?)([0-9]+):([0-9]+)$/s){ $tzoffset=(($1*$2*60)+($1*$3))*60; }
98 0         0 else{ $tzoffset+=0; }
99             }else{
100 1         9 $tzoffset = Time::Timezone::tz_local_offset();
101             }
102 1         40 $esecs += $tzoffset;
103 1         3 my @times = gmtime($esecs);
104              
105 1         2 my $str;
106 1         10 my @chars = split(//,$format);
107 1         3 foreach (@chars){
108 63 100       159 if($_ eq 'D'){ $str.=$days_short->[$times[6]]; }
  1 100       2  
    100          
    100          
    100          
    100          
    100          
109 1         3 elsif($_ eq 'M'){ $str.=$months_short->[$times[4]]; }
110 1 50       4 elsif($_ eq 'd'){ $str.=($times[3]<10?'0':'').$times[3]; }
111 1         3 elsif($_ eq 'Y'){ $str.=$times[5]+1900; }
112 1 50       4 elsif($_ eq 'g'){ $str.=($times[2]==0?12:$times[2]-($times[2]>12?12:0)); }
    50          
113 1 50       4 elsif($_ eq 'i'){ $str.=($times[1]<10?'0':'').$times[1]; }
114 1 50       6 elsif($_ eq 'a'){ $str.=($times[2]>=12?'pm':'am'); }
115 56         64 else{ $str.=$_; }
116             }
117              
118 1         10 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
128              
129             =cut
130              
131             sub getdate {
132 1 50   1 1 5 my($esecs) = (@_?shift():time);
133 1         13 my @times = localtime($esecs);
134 1         10 @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 50       5 if(wantarray){ return @times; }
  1         6  
141 0         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
150              
151             =cut
152              
153             sub gettimeofday {
154 1     1 1 274 my($sec,$usec) = Time::HiRes::gettimeofday();
155 1         6 my $minuteswest = int((-1 * Time::Timezone::tz_local_offset())/60);
156 1 50       57 my $dsttime = ((localtime(time))[8]?1:0);
157 1         7 my %times = ( sec=>$sec,usec=>$usec,minuteswest=>$minuteswest,dsttime=>$dsttime );
158 1 50       4 if(wantarray){ return %times; }
  0         0  
159 1         7 else{ return {%times}; }
160             }
161              
162             =head2 mktime
163              
164             my $then = mktime( $hour, $min, $sec, $month, $day, $year );
165              
166             L
167              
168             =cut
169              
170             sub mktime {
171             # hour, minute, second, month, day, year, is_dst
172 2     2 1 1374 my $times = [ ( localtime(time) )[2,1,0,4,3,5] ];
173 2         6 $times->[3]++;
174 2         6 $times->[5]+=1900;
175              
176 2         12 for( my $i=0; $i<@$times; $i++ ){
177 12 50       27 last if(!@_);
178 12         20 $times->[$i] = shift;
179             }
180              
181 2         5 $times->[3]--;
182 2         4 $times->[5]-=1900;
183 2         13 my $esecs = Time::Local::timelocal( (@$times)[2,1,0,4,3,5] );
184              
185 2         176 return $esecs;
186             }
187              
188             1;
189             __END__