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