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