line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#=============================================================================== |
2
|
|
|
|
|
|
|
# |
3
|
|
|
|
|
|
|
# FILE: DateTime.pm |
4
|
|
|
|
|
|
|
# |
5
|
|
|
|
|
|
|
# DESCRIPTION: Common date/time processing utilities for NetSDS |
6
|
|
|
|
|
|
|
# |
7
|
|
|
|
|
|
|
# AUTHOR: Michael Bochkaryov (Rattler), |
8
|
|
|
|
|
|
|
# COMPANY: Net.Style |
9
|
|
|
|
|
|
|
# CREATED: 25.04.2008 15:55:01 EEST |
10
|
|
|
|
|
|
|
#=============================================================================== |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
=head1 NAME |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
NetSDS::Util::DateTime - common date/time processing routines |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
=head1 SYNOPSIS |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
use NetSDS::Util::DateTime; |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
print "Current date: " . date_now(); |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
=head1 DESCRIPTION |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
This package provides set of routines for date and time processing. |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
=cut |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
package NetSDS::Util::DateTime; |
29
|
|
|
|
|
|
|
|
30
|
2
|
|
|
2
|
|
4858
|
use 5.8.0; |
|
2
|
|
|
|
|
14
|
|
|
2
|
|
|
|
|
78
|
|
31
|
2
|
|
|
2
|
|
9
|
use strict; |
|
2
|
|
|
|
|
1
|
|
|
2
|
|
|
|
|
65
|
|
32
|
2
|
|
|
2
|
|
8
|
use warnings; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
57
|
|
33
|
|
|
|
|
|
|
|
34
|
2
|
|
|
2
|
|
9
|
use base 'Exporter'; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
264
|
|
35
|
|
|
|
|
|
|
|
36
|
2
|
|
|
2
|
|
10
|
use version; our $VERSION = '1.044'; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
14
|
|
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
our @EXPORT = qw( |
39
|
|
|
|
|
|
|
date_now_array |
40
|
|
|
|
|
|
|
date_now |
41
|
|
|
|
|
|
|
date_now_iso8601 |
42
|
|
|
|
|
|
|
date_strip |
43
|
|
|
|
|
|
|
date_date |
44
|
|
|
|
|
|
|
date_time |
45
|
|
|
|
|
|
|
time_from_string |
46
|
|
|
|
|
|
|
date_from_string |
47
|
|
|
|
|
|
|
date_inc |
48
|
|
|
|
|
|
|
date_inc_string |
49
|
|
|
|
|
|
|
); |
50
|
|
|
|
|
|
|
|
51
|
2
|
|
|
2
|
|
1158
|
use POSIX; |
|
2
|
|
|
|
|
11361
|
|
|
2
|
|
|
|
|
9
|
|
52
|
2
|
|
|
2
|
|
5523
|
use Time::Local; |
|
2
|
|
|
|
|
2878
|
|
|
2
|
|
|
|
|
113
|
|
53
|
2
|
|
|
2
|
|
1089
|
use Time::HiRes qw(gettimeofday clock_gettime); |
|
2
|
|
|
|
|
2706
|
|
|
2
|
|
|
|
|
8
|
|
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
# Include parsing/formatting modules |
56
|
2
|
|
|
2
|
|
1359
|
use HTTP::Date qw(parse_date); |
|
2
|
|
|
|
|
2876
|
|
|
2
|
|
|
|
|
156
|
|
57
|
2
|
|
|
2
|
|
871
|
use Date::Parse qw(str2time); |
|
2
|
|
|
|
|
7968
|
|
|
2
|
|
|
|
|
221
|
|
58
|
2
|
|
|
2
|
|
947
|
use Date::Format qw(time2str); |
|
2
|
|
|
|
|
4556
|
|
|
2
|
|
|
|
|
1060
|
|
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
#=============================================================================== |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
=head1 EXPORTED FUNCTIONS |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=over |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
=item B |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
Returns array of date items for given date. |
69
|
|
|
|
|
|
|
If source date is not set current date used. |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
=cut |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
#----------------------------------------------------------------------- |
74
|
|
|
|
|
|
|
sub date_now_array { |
75
|
0
|
0
|
|
0
|
1
|
|
my ( $sec, $min, $hor, $mdy, $mon, $yer ) = localtime( (@_) ? $_[0] : time ); |
76
|
|
|
|
|
|
|
|
77
|
0
|
|
|
|
|
|
return ( $yer + 1900, $mon + 1, $mdy, $hor, $min, $sec ); |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
#*********************************************************************** |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
=item B |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
Return [given] date as string. |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
2001-12-23 14:39:53 |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
=cut |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
#----------------------------------------------------------------------- |
91
|
|
|
|
|
|
|
sub date_now { |
92
|
0
|
|
|
0
|
1
|
|
my ( $tm, $zn ) = @_; |
93
|
|
|
|
|
|
|
|
94
|
0
|
0
|
0
|
|
|
|
return ($zn) |
|
|
|
0
|
|
|
|
|
95
|
|
|
|
|
|
|
? time2str( "%Y-%m-%d %T %z", $tm || time ) |
96
|
|
|
|
|
|
|
: time2str( "%Y-%m-%d %T", $tm || time ); |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
#*********************************************************************** |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
=item B |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
Return date as ISO 8601 string. |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
20011223T14:39:53Z |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
L |
108
|
|
|
|
|
|
|
L |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
=cut |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
#----------------------------------------------------------------------- |
113
|
|
|
|
|
|
|
sub date_now_iso8601 { |
114
|
0
|
|
|
0
|
1
|
|
my ($tm) = @_; |
115
|
|
|
|
|
|
|
|
116
|
0
|
|
0
|
|
|
|
return time2str( "%Y%m%dT%H%M%S%z", $tm || time ); |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
#*********************************************************************** |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
=item B |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
Trim miliseconds from date. |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
=cut |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
#----------------------------------------------------------------------- |
128
|
|
|
|
|
|
|
sub date_strip { |
129
|
0
|
|
|
0
|
1
|
|
my ($date) = @_; |
130
|
|
|
|
|
|
|
|
131
|
0
|
0
|
|
|
|
|
$date =~ s/\.\d+// if ($date); |
132
|
|
|
|
|
|
|
|
133
|
0
|
|
|
|
|
|
return $date; |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
#*********************************************************************** |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=item B |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
Trim time part from date. |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=cut |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
#----------------------------------------------------------------------- |
145
|
|
|
|
|
|
|
sub date_date { |
146
|
0
|
|
|
0
|
1
|
|
my ($date) = @_; |
147
|
|
|
|
|
|
|
|
148
|
0
|
0
|
|
|
|
|
$date =~ s/[\sT]+.+$// if ($date); |
149
|
|
|
|
|
|
|
|
150
|
0
|
|
|
|
|
|
return $date; |
151
|
|
|
|
|
|
|
} |
152
|
|
|
|
|
|
|
#*********************************************************************** |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=item B |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
Trim date part from date. |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=cut |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
#----------------------------------------------------------------------- |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
sub date_time { |
163
|
0
|
|
|
0
|
1
|
|
my ($date) = @_; |
164
|
|
|
|
|
|
|
|
165
|
0
|
0
|
|
|
|
|
unless ( defined ($date) ) { |
166
|
0
|
|
|
|
|
|
return undef; |
167
|
|
|
|
|
|
|
} |
168
|
|
|
|
|
|
|
|
169
|
0
|
|
|
|
|
|
my ($dateonly, $time) = split (/ /, $date); |
170
|
|
|
|
|
|
|
|
171
|
0
|
|
|
|
|
|
return $time; |
172
|
|
|
|
|
|
|
} |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
#*********************************************************************** |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
=item B |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
Return parsed date/time structure. |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
=cut |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
#----------------------------------------------------------------------- |
183
|
|
|
|
|
|
|
sub time_from_string { |
184
|
0
|
|
|
0
|
1
|
|
my ($str) = @_; |
185
|
|
|
|
|
|
|
|
186
|
0
|
0
|
|
|
|
|
unless ($str) { |
187
|
0
|
|
|
|
|
|
return undef; |
188
|
|
|
|
|
|
|
} |
189
|
|
|
|
|
|
|
|
190
|
0
|
|
|
|
|
|
my $tm = Date::Parse::str2time($str); |
191
|
0
|
0
|
|
|
|
|
if ($tm) { |
192
|
0
|
|
|
|
|
|
return $tm; |
193
|
|
|
|
|
|
|
} |
194
|
|
|
|
|
|
|
|
195
|
0
|
|
|
|
|
|
$tm = parse_date($str); |
196
|
0
|
0
|
|
|
|
|
if ($tm) { |
197
|
0
|
|
|
|
|
|
return Date::Parse::str2time($tm); |
198
|
|
|
|
|
|
|
} |
199
|
|
|
|
|
|
|
|
200
|
0
|
|
|
|
|
|
return undef; |
201
|
|
|
|
|
|
|
} |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
#*********************************************************************** |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
=item B |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
Return date from string representation. |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
=cut |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
#----------------------------------------------------------------------- |
212
|
|
|
|
|
|
|
sub date_from_string { |
213
|
0
|
|
|
0
|
1
|
|
my ($str) = @_; |
214
|
|
|
|
|
|
|
|
215
|
0
|
|
|
|
|
|
return date_now( time_from_string($str) ); |
216
|
|
|
|
|
|
|
} |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
#*********************************************************************** |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
=item B |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
Return date incremented with given number of seconds. |
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
=cut |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
#----------------------------------------------------------------------- |
227
|
|
|
|
|
|
|
sub date_inc { |
228
|
0
|
|
|
0
|
1
|
|
my ( $inc, $tm ) = @_; |
229
|
0
|
|
0
|
|
|
|
$tm ||= time; |
230
|
|
|
|
|
|
|
|
231
|
0
|
|
|
|
|
|
return date_now( $tm + $inc ); |
232
|
|
|
|
|
|
|
} |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
#*********************************************************************** |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
=item B |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
Return string representation of date incremented with given number of seconds. |
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
=cut |
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
#----------------------------------------------------------------------- |
243
|
|
|
|
|
|
|
sub date_inc_string { |
244
|
0
|
|
|
0
|
1
|
|
my ( $inc, $tm ) = @_; |
245
|
|
|
|
|
|
|
|
246
|
0
|
0
|
|
|
|
|
return ($tm) ? date_inc( $inc, time_from_string($tm) ) : date_inc($inc); |
247
|
|
|
|
|
|
|
} |
248
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
1; |
250
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
__END__ |