-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathStamp.pm
248 lines (165 loc) · 5.08 KB
/
Stamp.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#
# Common: Shared library for voice-based applications.
#
# Copyright (C) 2010 Nokia Corporation.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Authors: Billy Odero, Jonathan Ledlie
package Nokia::Common::Stamp;
=head1 NAME
Nokia::Common::Stamp - Date/Time formatting and conversions.
=head1 DESCRIPTION
This module manages all time/date conversions
=head1 METHODS
=cut
use Exporter;
@ISA = ('Exporter');
@EXPORT = ('year_month_day','year_month_day_dir','year_month',
'year_month_day_plus_n_days_dir');
my $one_hour_delay = 1;
my $SEC_PER_DAY = 60*60*24;
my $SEC_PER_WEEK = 604800;
######################################################################
=head2 year_month_day
Returns the current time into a date-time value of the form yyyymmdd
=cut
sub year_month_day {
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
localtime(time);
$year += 1900;
$mon++;
my $stamp = sprintf ("$year%02d%02d", $mon, $mday);
return $stamp;
}
######################################################################
=head2 year_month_day_plus_n_days_dir
Returns a directory path of the form yyyy/mm/dd/ formed from the current
date-time
=over 4
=item Args:
$days: delta/delay adjustment to the current time
=back
=cut
sub year_month_day_plus_n_days_dir {
my ($days) = @_;
my $delay = $days * $SEC_PER_DAY;
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
localtime(time+($delay));
$year += 1900;
$mon++;
my $stamp = sprintf ("$year/%02d/%02d/", $mon, $mday);
return $stamp;
}
######################################################################
=head2 year_month_day_dir
Returns a directory path of the form yyyy/mm/dd/ formed from the current
date-time
=over 4
=item Args:
$days: delta/delay adjustment to the current time
=back
=cut
sub year_month_day_dir {
my ($days) = @_;
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
localtime(time);
$year += 1900;
$mon++;
my $stamp = sprintf ("$year/%02d/%02d/", $mon, $mday);
return $stamp;
}
######################################################################
=head2 year_month
Returns the current date-time in the form C<yyyy-mm>
=cut
sub year_month {
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
localtime(time);
$year += 1900;
$mon++;
my $stamp = sprintf ("$year-%02d", $mon);
return $stamp;
}
######################################################################
=head2 epoch2stamp
Converts the supplied epoch time to a timestamp value of the form C<yyyy-mm-dd hh:mm>
=over 4
=item Args:
$epoch: The epoch time to be formatted
=back
=cut
sub epoch2stamp {
my ($epoch) = @_;
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
localtime($epoch);
$year += 1900;
$mon++;
my $stamp = sprintf ("$year-%02d-%02d %02d:%02d", $mon,
$mday,$hour,$min);
return $stamp;
}
######################################################################
=head2 epoch2year_mo_day
Converts the supplied epoch time to a date/time value of the form C<yyyy-mm-dd>
=over 4
=item Args:
$epoch: The epoch time to be formatted
=back
=cut
sub epoch2year_mo_day {
my ($epoch) = @_;
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
localtime($epoch);
$year += 1900;
$mon++;
my $stamp = sprintf ("$year-%02d-%02d", $mon, $mday);
return $stamp;
}
######################################################################
=head2 file2epochdelta
Gets the statistics from the supplied file and returns the timestamp value of the
date the file was last modified.
=over 4
=item Args:
$file: The filepath to the file whose date property is to be used
=back
=cut
sub file2epochdelta {
my ($file) = @_;
my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
$atime,$mtime,$ctime,$blksize,$blocks)
= stat($file) or die ("Could not stat file $file");
if ($file =~ /(\d\d\d\d\d\d\d\d\d\d)/) {
my $epoch = $1;
if ($one_hour_delay) {
$epoch = $epoch + 3600;
}
my $epochdelta = $mtime - $epoch;
#print "ctime: ".&epoch2stamp($ctime)."\n";
#print "atime: ".&epoch2stamp($atime)."\n";
#print "mtime: ".&epoch2stamp($mtime)."\n";
#print "name: ".&epoch2stamp($epoch)."\n";
#print "ep $epochdelta\n";
return $epochdelta
} else {
die ("File name does not contain epoch time: $filename");
}
}
######################################################################
=head1 AUTHORS
Billy Odero, Jonathan Ledlie
Copyright (C) 2010 Nokia Corporation.
=cut
1;