line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Goo::Date; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
############################################################################### |
4
|
|
|
|
|
|
|
# Nigel Hamilton - handle dates |
5
|
|
|
|
|
|
|
# |
6
|
|
|
|
|
|
|
# Copyright Nigel Hamilton 2002 |
7
|
|
|
|
|
|
|
# All Rights Reserved |
8
|
|
|
|
|
|
|
# |
9
|
|
|
|
|
|
|
# Author: Nigel Hamilton |
10
|
|
|
|
|
|
|
# Filename: Goo::Date.pm |
11
|
|
|
|
|
|
|
# Description: Date handling functions |
12
|
|
|
|
|
|
|
# |
13
|
|
|
|
|
|
|
# Date Change |
14
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------- |
15
|
|
|
|
|
|
|
# 20/6/2002 Version 1 |
16
|
|
|
|
|
|
|
# |
17
|
|
|
|
|
|
|
############################################################################### |
18
|
|
|
|
|
|
|
|
19
|
1
|
|
|
1
|
|
12028
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
1605
|
|
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
############################################################################### |
23
|
|
|
|
|
|
|
# |
24
|
|
|
|
|
|
|
# get_date_ndays_ago - return the date n days go |
25
|
|
|
|
|
|
|
# |
26
|
|
|
|
|
|
|
############################################################################### |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
sub get_date_ndays_ago { |
29
|
|
|
|
|
|
|
|
30
|
0
|
|
|
0
|
1
|
|
my ($days) = @_; |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
# how many seconds in a day? |
33
|
|
|
|
|
|
|
# hh mm ss |
34
|
0
|
|
|
|
|
|
my $secondsperday = 24 * 60 * 60; |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
# how many total seconds? |
37
|
0
|
|
|
|
|
|
my $when = time() - ($days * $secondsperday); |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
# adjust localtime values accordingly |
40
|
0
|
|
|
|
|
|
my ($day, $month, $year) = (localtime($when))[ 3, 4, 5 ]; |
41
|
|
|
|
|
|
|
|
42
|
0
|
|
|
|
|
|
$month = $month + 1; |
43
|
0
|
|
|
|
|
|
$year = $year + 1900; |
44
|
|
|
|
|
|
|
|
45
|
0
|
|
|
|
|
|
return "$year-$month-$day"; |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
############################################################################### |
51
|
|
|
|
|
|
|
# |
52
|
|
|
|
|
|
|
# get_last_month - return the last month |
53
|
|
|
|
|
|
|
# |
54
|
|
|
|
|
|
|
############################################################################### |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
sub get_last_month { |
57
|
|
|
|
|
|
|
|
58
|
0
|
|
|
0
|
1
|
|
my ($month) = @_; |
59
|
|
|
|
|
|
|
|
60
|
0
|
0
|
|
|
|
|
if ($month > 12) { die("Invalid month: $month"); } |
|
0
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
|
62
|
0
|
0
|
|
|
|
|
if ($month == 1) { return 12; } |
|
0
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
|
64
|
0
|
|
|
|
|
|
return $month - 1; |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
############################################################################### |
70
|
|
|
|
|
|
|
# |
71
|
|
|
|
|
|
|
# get_current_year - return the year |
72
|
|
|
|
|
|
|
# |
73
|
|
|
|
|
|
|
############################################################################### |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
sub get_current_year { |
76
|
|
|
|
|
|
|
|
77
|
0
|
|
|
0
|
1
|
|
my ($day, $month, $year) = get_current_date(); |
78
|
|
|
|
|
|
|
|
79
|
0
|
|
|
|
|
|
return $year; |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
############################################################################### |
85
|
|
|
|
|
|
|
# |
86
|
|
|
|
|
|
|
# get_current_date_with_slashes - return a zero-filled date like dd/mm/yyyy |
87
|
|
|
|
|
|
|
# |
88
|
|
|
|
|
|
|
############################################################################### |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
sub get_current_date_with_slashes { |
91
|
|
|
|
|
|
|
|
92
|
0
|
|
|
0
|
1
|
|
my ($day, $month, $year) = get_current_date(); |
93
|
|
|
|
|
|
|
|
94
|
0
|
|
|
|
|
|
$month = get_zero_padded($month); |
95
|
0
|
|
|
|
|
|
$day = get_zero_padded($day); |
96
|
|
|
|
|
|
|
|
97
|
0
|
|
|
|
|
|
return "$day/$month/$year"; |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
############################################################################### |
103
|
|
|
|
|
|
|
# |
104
|
|
|
|
|
|
|
# convert - convert from yyyy-mm-dd to -> 10 jan 2002 |
105
|
|
|
|
|
|
|
# |
106
|
|
|
|
|
|
|
############################################################################### |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
sub convert { |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
# adjust localtime values accordingly |
111
|
0
|
|
|
0
|
1
|
|
my ($date) = @_; |
112
|
|
|
|
|
|
|
|
113
|
0
|
|
|
|
|
|
$date =~ m/^(.*?)-(.*?)-(.*?)$/; |
114
|
|
|
|
|
|
|
|
115
|
0
|
|
|
|
|
|
my $month = get_month_prefix($2); |
116
|
|
|
|
|
|
|
|
117
|
0
|
|
|
|
|
|
return "$3 $month $1"; |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
############################################################################### |
123
|
|
|
|
|
|
|
# |
124
|
|
|
|
|
|
|
# get_current_date_with_month_prefix - get the current date |
125
|
|
|
|
|
|
|
# |
126
|
|
|
|
|
|
|
############################################################################### |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
sub get_current_date_with_month_prefix { |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
# adjust localtime values accordingly |
131
|
0
|
|
|
0
|
1
|
|
my ($day, $month, $year) = (localtime)[ 3, 4, 5 ]; |
132
|
|
|
|
|
|
|
|
133
|
0
|
|
|
|
|
|
$month = $month + 1; |
134
|
|
|
|
|
|
|
|
135
|
0
|
|
|
|
|
|
$month = get_month_prefix($month); |
136
|
|
|
|
|
|
|
|
137
|
0
|
|
|
|
|
|
$year = $year + 1900; |
138
|
|
|
|
|
|
|
|
139
|
0
|
|
|
|
|
|
return $day . " " . $month . " " . $year; |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
############################################################################### |
145
|
|
|
|
|
|
|
# |
146
|
|
|
|
|
|
|
# get_current_date - get the current date |
147
|
|
|
|
|
|
|
# |
148
|
|
|
|
|
|
|
############################################################################### |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
sub get_current_date { |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
# adjust localtime values accordingly |
153
|
0
|
|
|
0
|
1
|
|
my ($day, $month, $year) = (localtime)[ 3, 4, 5 ]; |
154
|
|
|
|
|
|
|
|
155
|
0
|
|
|
|
|
|
return ($day, $month + 1, $year + 1900); |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
} |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
############################################################################## |
161
|
|
|
|
|
|
|
# |
162
|
|
|
|
|
|
|
# get_month_from_prefix - return a month number given a prefix |
163
|
|
|
|
|
|
|
# |
164
|
|
|
|
|
|
|
############################################################################## |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
sub get_month_from_prefix { |
167
|
|
|
|
|
|
|
|
168
|
0
|
|
|
0
|
1
|
|
my ($month) = @_; |
169
|
|
|
|
|
|
|
|
170
|
0
|
|
|
|
|
|
my $months = { 'Jan' => 1, |
171
|
|
|
|
|
|
|
'Feb' => 2, |
172
|
|
|
|
|
|
|
'Mar' => 3, |
173
|
|
|
|
|
|
|
'Apr' => 4, |
174
|
|
|
|
|
|
|
'May' => 5, |
175
|
|
|
|
|
|
|
'Jun' => 6, |
176
|
|
|
|
|
|
|
'Jul' => 7, |
177
|
|
|
|
|
|
|
'Aug' => 8, |
178
|
|
|
|
|
|
|
'Sep' => 9, |
179
|
|
|
|
|
|
|
'Oct' => 10, |
180
|
|
|
|
|
|
|
'Nov' => 11, |
181
|
|
|
|
|
|
|
'Dec' => 12 |
182
|
|
|
|
|
|
|
}; |
183
|
|
|
|
|
|
|
|
184
|
0
|
|
|
|
|
|
return $months->{$month}; |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
} |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
############################################################################## |
190
|
|
|
|
|
|
|
# |
191
|
|
|
|
|
|
|
# get_month_prefix - return a month prefix |
192
|
|
|
|
|
|
|
# |
193
|
|
|
|
|
|
|
############################################################################## |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
sub get_month_prefix { |
196
|
|
|
|
|
|
|
|
197
|
0
|
|
|
0
|
1
|
|
my ($month) = @_; |
198
|
|
|
|
|
|
|
|
199
|
0
|
|
|
|
|
|
my $months = { 1 => 'Jan', |
200
|
|
|
|
|
|
|
2 => 'Feb', |
201
|
|
|
|
|
|
|
3 => 'Mar', |
202
|
|
|
|
|
|
|
4 => 'Apr', |
203
|
|
|
|
|
|
|
5 => 'May', |
204
|
|
|
|
|
|
|
6 => 'Jun', |
205
|
|
|
|
|
|
|
7 => 'Jul', |
206
|
|
|
|
|
|
|
8 => 'Aug', |
207
|
|
|
|
|
|
|
9 => 'Sep', |
208
|
|
|
|
|
|
|
10 => 'Oct', |
209
|
|
|
|
|
|
|
11 => 'Nov', |
210
|
|
|
|
|
|
|
12 => 'Dec' |
211
|
|
|
|
|
|
|
}; |
212
|
|
|
|
|
|
|
|
213
|
0
|
|
|
|
|
|
return $months->{$month}; |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
} |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
############################################################################## |
219
|
|
|
|
|
|
|
# |
220
|
|
|
|
|
|
|
# get_month - return a month |
221
|
|
|
|
|
|
|
# |
222
|
|
|
|
|
|
|
############################################################################## |
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
sub get_month { |
225
|
|
|
|
|
|
|
|
226
|
0
|
|
|
0
|
1
|
|
my ($month) = @_; |
227
|
|
|
|
|
|
|
|
228
|
0
|
|
|
|
|
|
my $months = { 1 => 'January', |
229
|
|
|
|
|
|
|
2 => 'February', |
230
|
|
|
|
|
|
|
3 => 'March', |
231
|
|
|
|
|
|
|
4 => 'April', |
232
|
|
|
|
|
|
|
5 => 'May', |
233
|
|
|
|
|
|
|
6 => 'June', |
234
|
|
|
|
|
|
|
7 => 'July', |
235
|
|
|
|
|
|
|
8 => 'August', |
236
|
|
|
|
|
|
|
9 => 'September', |
237
|
|
|
|
|
|
|
10 => 'October', |
238
|
|
|
|
|
|
|
11 => 'November', |
239
|
|
|
|
|
|
|
12 => 'Dececember' |
240
|
|
|
|
|
|
|
}; |
241
|
|
|
|
|
|
|
|
242
|
0
|
|
|
|
|
|
return $months->{$month}; |
243
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
} |
245
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
############################################################################## |
248
|
|
|
|
|
|
|
# |
249
|
|
|
|
|
|
|
# get_date_from_time - return a date given a time |
250
|
|
|
|
|
|
|
# |
251
|
|
|
|
|
|
|
############################################################################## |
252
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
sub get_date_from_time { |
254
|
|
|
|
|
|
|
|
255
|
0
|
|
|
0
|
1
|
|
my ($time, $format) = @_; |
256
|
|
|
|
|
|
|
|
257
|
|
|
|
|
|
|
# adjust localtime values accordingly |
258
|
0
|
|
|
|
|
|
my ($hour, $day, $month, $year) = (localtime($time))[ 2, 3, 4, 5 ]; |
259
|
|
|
|
|
|
|
|
260
|
0
|
|
|
|
|
|
$year = $year + 1900; |
261
|
0
|
|
|
|
|
|
$month = $month + 1; |
262
|
|
|
|
|
|
|
|
263
|
0
|
|
|
|
|
|
$month = get_zero_padded($month); |
264
|
0
|
|
|
|
|
|
$day = get_zero_padded($day); |
265
|
0
|
|
|
|
|
|
$hour = get_zero_padded($hour); |
266
|
|
|
|
|
|
|
|
267
|
0
|
|
|
|
|
|
$format = lc($format); |
268
|
|
|
|
|
|
|
|
269
|
0
|
0
|
|
|
|
|
if ($format eq "yyyymmdd") { |
270
|
0
|
|
|
|
|
|
return $year . $month . $day; |
271
|
|
|
|
|
|
|
} |
272
|
|
|
|
|
|
|
|
273
|
0
|
0
|
|
|
|
|
if ($format eq "yyyymmddhh") { |
274
|
0
|
|
|
|
|
|
return $year . $month . $day . $hour; |
275
|
|
|
|
|
|
|
} |
276
|
|
|
|
|
|
|
|
277
|
|
|
|
|
|
|
} |
278
|
|
|
|
|
|
|
|
279
|
|
|
|
|
|
|
|
280
|
|
|
|
|
|
|
############################################################################## |
281
|
|
|
|
|
|
|
# |
282
|
|
|
|
|
|
|
# get_zero_padded - add padding to fit the format |
283
|
|
|
|
|
|
|
# |
284
|
|
|
|
|
|
|
############################################################################## |
285
|
|
|
|
|
|
|
|
286
|
|
|
|
|
|
|
sub get_zero_padded { |
287
|
|
|
|
|
|
|
|
288
|
0
|
|
|
0
|
1
|
|
my ($field) = @_; |
289
|
|
|
|
|
|
|
|
290
|
0
|
0
|
|
|
|
|
if (length($field) == 1) { $field = "0$field"; } |
|
0
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
|
292
|
0
|
|
|
|
|
|
return $field; |
293
|
|
|
|
|
|
|
|
294
|
|
|
|
|
|
|
} |
295
|
|
|
|
|
|
|
|
296
|
|
|
|
|
|
|
|
297
|
|
|
|
|
|
|
############################################################################## |
298
|
|
|
|
|
|
|
# |
299
|
|
|
|
|
|
|
# get_time - return the current time |
300
|
|
|
|
|
|
|
# |
301
|
|
|
|
|
|
|
############################################################################## |
302
|
|
|
|
|
|
|
|
303
|
|
|
|
|
|
|
sub get_time { |
304
|
|
|
|
|
|
|
|
305
|
|
|
|
|
|
|
# local time returns: Fri Apr 11 09:27:08 2002 in scalar context |
306
|
0
|
|
|
0
|
1
|
|
return localtime(); |
307
|
|
|
|
|
|
|
|
308
|
|
|
|
|
|
|
|
309
|
|
|
|
|
|
|
} |
310
|
|
|
|
|
|
|
|
311
|
|
|
|
|
|
|
|
312
|
|
|
|
|
|
|
############################################################################### |
313
|
|
|
|
|
|
|
# |
314
|
|
|
|
|
|
|
# get_today - synonym for get_current_date |
315
|
|
|
|
|
|
|
# |
316
|
|
|
|
|
|
|
############################################################################### |
317
|
|
|
|
|
|
|
|
318
|
|
|
|
|
|
|
sub get_today { |
319
|
|
|
|
|
|
|
|
320
|
0
|
|
|
0
|
1
|
|
return get_current_date(); |
321
|
|
|
|
|
|
|
|
322
|
|
|
|
|
|
|
} |
323
|
|
|
|
|
|
|
|
324
|
|
|
|
|
|
|
1; |
325
|
|
|
|
|
|
|
|
326
|
|
|
|
|
|
|
|
327
|
|
|
|
|
|
|
__END__ |
328
|
|
|
|
|
|
|
|
329
|
|
|
|
|
|
|
=head1 NAME |
330
|
|
|
|
|
|
|
|
331
|
|
|
|
|
|
|
Goo::Date - Date handling functions |
332
|
|
|
|
|
|
|
|
333
|
|
|
|
|
|
|
=head1 SYNOPSIS |
334
|
|
|
|
|
|
|
|
335
|
|
|
|
|
|
|
use Goo::Date; |
336
|
|
|
|
|
|
|
|
337
|
|
|
|
|
|
|
=head1 DESCRIPTION |
338
|
|
|
|
|
|
|
|
339
|
|
|
|
|
|
|
Simple date handling methods. |
340
|
|
|
|
|
|
|
|
341
|
|
|
|
|
|
|
=head1 METHODS |
342
|
|
|
|
|
|
|
|
343
|
|
|
|
|
|
|
=over |
344
|
|
|
|
|
|
|
|
345
|
|
|
|
|
|
|
=item get_date_ndays_ago |
346
|
|
|
|
|
|
|
|
347
|
|
|
|
|
|
|
return the date n days go |
348
|
|
|
|
|
|
|
|
349
|
|
|
|
|
|
|
=item get_last_month |
350
|
|
|
|
|
|
|
|
351
|
|
|
|
|
|
|
return the last month |
352
|
|
|
|
|
|
|
|
353
|
|
|
|
|
|
|
=item get_current_year |
354
|
|
|
|
|
|
|
|
355
|
|
|
|
|
|
|
return the year |
356
|
|
|
|
|
|
|
|
357
|
|
|
|
|
|
|
=item get_current_date_with_slashes |
358
|
|
|
|
|
|
|
|
359
|
|
|
|
|
|
|
return a zero-filled date like dd/mm/yyyy |
360
|
|
|
|
|
|
|
|
361
|
|
|
|
|
|
|
=item convert |
362
|
|
|
|
|
|
|
|
363
|
|
|
|
|
|
|
convert from yyyy-mm-dd to -> 10 jan 2002 |
364
|
|
|
|
|
|
|
|
365
|
|
|
|
|
|
|
=item get_current_date_with_month_prefix |
366
|
|
|
|
|
|
|
|
367
|
|
|
|
|
|
|
get the current date |
368
|
|
|
|
|
|
|
|
369
|
|
|
|
|
|
|
=item get_current_date |
370
|
|
|
|
|
|
|
|
371
|
|
|
|
|
|
|
get the current date |
372
|
|
|
|
|
|
|
|
373
|
|
|
|
|
|
|
=item get_month_from_prefix |
374
|
|
|
|
|
|
|
|
375
|
|
|
|
|
|
|
return a month number given a prefix |
376
|
|
|
|
|
|
|
|
377
|
|
|
|
|
|
|
=item get_month_prefix |
378
|
|
|
|
|
|
|
|
379
|
|
|
|
|
|
|
return a month prefix |
380
|
|
|
|
|
|
|
|
381
|
|
|
|
|
|
|
=item get_month |
382
|
|
|
|
|
|
|
|
383
|
|
|
|
|
|
|
return a month |
384
|
|
|
|
|
|
|
|
385
|
|
|
|
|
|
|
=item get_date_from_time |
386
|
|
|
|
|
|
|
|
387
|
|
|
|
|
|
|
return a date given a time |
388
|
|
|
|
|
|
|
|
389
|
|
|
|
|
|
|
=item get_zero_padded |
390
|
|
|
|
|
|
|
|
391
|
|
|
|
|
|
|
add padding to fit the format |
392
|
|
|
|
|
|
|
|
393
|
|
|
|
|
|
|
=item get_time |
394
|
|
|
|
|
|
|
|
395
|
|
|
|
|
|
|
return the current time |
396
|
|
|
|
|
|
|
|
397
|
|
|
|
|
|
|
=item get_today |
398
|
|
|
|
|
|
|
|
399
|
|
|
|
|
|
|
synonym for get_current_date |
400
|
|
|
|
|
|
|
|
401
|
|
|
|
|
|
|
|
402
|
|
|
|
|
|
|
=back |
403
|
|
|
|
|
|
|
|
404
|
|
|
|
|
|
|
=head1 AUTHOR |
405
|
|
|
|
|
|
|
|
406
|
|
|
|
|
|
|
Nigel Hamilton <nigel@trexy.com> |
407
|
|
|
|
|
|
|
|
408
|
|
|
|
|
|
|
=head1 SEE ALSO |
409
|
|
|
|
|
|
|
|