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), <misha@rattler.kiev.ua> |
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
|
|
6975
|
use 5.8.0; |
|
2
|
|
|
|
|
17
|
|
|
2
|
|
|
|
|
100
|
|
31
|
2
|
|
|
2
|
|
12
|
use strict; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
66
|
|
32
|
2
|
|
|
2
|
|
10
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
71
|
|
33
|
|
|
|
|
|
|
|
34
|
2
|
|
|
2
|
|
12
|
use base 'Exporter'; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
205
|
|
35
|
|
|
|
|
|
|
|
36
|
2
|
|
|
2
|
|
11
|
use version; our $VERSION = '1.044'; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
20
|
|
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
|
|
2207
|
use POSIX; |
|
2
|
|
|
|
|
18356
|
|
|
2
|
|
|
|
|
14
|
|
52
|
2
|
|
|
2
|
|
10019
|
use Time::Local; |
|
2
|
|
|
|
|
5722
|
|
|
2
|
|
|
|
|
170
|
|
53
|
2
|
|
|
2
|
|
2040
|
use Time::HiRes qw(gettimeofday clock_gettime); |
|
2
|
|
|
|
|
3825
|
|
|
2
|
|
|
|
|
9
|
|
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
# Include parsing/formatting modules |
56
|
2
|
|
|
2
|
|
2198
|
use HTTP::Date qw(parse_date); |
|
2
|
|
|
|
|
4067
|
|
|
2
|
|
|
|
|
153
|
|
57
|
2
|
|
|
2
|
|
1770
|
use Date::Parse qw(str2time); |
|
2
|
|
|
|
|
11706
|
|
|
2
|
|
|
|
|
223
|
|
58
|
2
|
|
|
2
|
|
1903
|
use Date::Format qw(time2str); |
|
2
|
|
|
|
|
6387
|
|
|
2
|
|
|
|
|
1250
|
|
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
#=============================================================================== |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
=head1 EXPORTED FUNCTIONS |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=over |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
=item B<date_now_array([TIME])> |
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<date_now([TIME])> |
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<date_now_iso8601([TIME])> |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
Return date as ISO 8601 string. |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
20011223T14:39:53Z |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
L<http://en.wikipedia.org/wiki/ISO_8601> |
108
|
|
|
|
|
|
|
L<http://www.w3.org/TR/NOTE-datetime> |
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<date_strip(DATE)> |
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<date_date(DATE)> |
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<date_time(DATE)> |
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<time_from_string($string)> |
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<date_from_string($string)> |
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<date_inc([INCREMENT, [TIME]])> |
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<date_inc_string([INCREMENT, [TIME]])> |
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__ |
252
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
=back |
254
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
=head1 EXAMPLES |
256
|
|
|
|
|
|
|
|
257
|
|
|
|
|
|
|
None yet |
258
|
|
|
|
|
|
|
|
259
|
|
|
|
|
|
|
=head1 BUGS |
260
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
Unknown yet |
262
|
|
|
|
|
|
|
|
263
|
|
|
|
|
|
|
=head1 SEE ALSO |
264
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
L<Date::Parse>, L<Date::Format> |
266
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
=head1 TODO |
268
|
|
|
|
|
|
|
|
269
|
|
|
|
|
|
|
Import stuff from Wono project |
270
|
|
|
|
|
|
|
|
271
|
|
|
|
|
|
|
=head1 AUTHOR |
272
|
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
Valentyn Solomko <val@pere.org.ua> |
274
|
|
|
|
|
|
|
|
275
|
|
|
|
|
|
|
Michael Bochkaryov <misha@rattler.kiev.ua> |
276
|
|
|
|
|
|
|
|
277
|
|
|
|
|
|
|
=cut |
278
|
|
|
|
|
|
|
|
279
|
|
|
|
|
|
|
|