File Coverage

lib/DateStamp.pm
Criterion Covered Total %
statement 109 118 92.3
branch 48 62 77.4
condition n/a
subroutine 10 10 100.0
pod 6 6 100.0
total 173 196 88.2


line stmt bran cond sub pod time code
1             package DateStamp;
2              
3             # | PACKAGE | DateStamp
4             # | AUTHOR | Todd Wylie
5             # | EMAIL | perldev@monkeybytes.org
6             # | ID | $Id: DateStamp.pm 6 2006-01-13 23:00:39Z Todd Wylie $
7              
8 2     2   55042 use version; $VERSION = qv('1.0.4');
  2         4366  
  2         11  
9 2     2   153 use warnings;
  2         4  
  2         65  
10 2     2   10 use strict;
  2         8  
  2         55  
11 2     2   16 use Carp;
  2         5  
  2         3888  
12              
13             # --------------------------------------------------------------------------
14             # N E W (class CONSTRUCTOR)
15             # ==========================================================================
16             # USAGE : DateStamp->new();
17             # PURPOSE : constructor for DATE class
18             # RETURNS : object handle
19             # PARAMETERS : none
20             # THROWS : none
21             # COMMENTS : Loads the date object with values from localtime function;
22             # : does some cleaning-up of the values, too.
23             # SEE ALSO : return_year
24             # : return_month
25             # : return_day
26             # : return_time
27             # : return_date
28             # --------------------------------------------------------------------------
29             sub new {
30 1     1 1 16 my $class = shift;
31            
32             # Retrieve date & time from localtime function.
33             my (
34 1         200 $seconds,
35             $minutes,
36             $hour,
37             $month_day,
38             $month,
39             $year,
40             $week_day,
41             $year_day,
42             $daylight_savings_time,
43             ) = localtime();
44              
45             # Convert time values to desired format.
46 1         5 $year += 1900;
47 1         2 $month += 1;
48 1 50       10 $hour = $hour =~ /[10-24]/ ? $hour : '0' . $hour;
49 1 50       6 $month = $month =~ /1[012]/ ? $month : '0' . $month;
50 1 50       6 $month_day = $month_day =~ /\d\d/ ? $month_day : '0' . $month_day;
51 1 50       7 $seconds = $seconds =~ /\d\d/ ? $seconds : '0' . $seconds;
52 1 50       5 $minutes = $minutes =~ /\d\d/ ? $minutes : '0' . $minutes;
53              
54             # Day, month, time lookup tables.
55 1         10 my %day_alpha = (
56             '0' => 'Sunday',
57             '1' => 'Monday',
58             '2' => 'Tuesday',
59             '3' => 'Wednesday',
60             '4' => 'Thursday',
61             '5' => 'Friday',
62             '6' => 'Saturday',
63             );
64            
65 1         23 my %month_alpha = (
66             '01' => 'January',
67             '02' => 'February',
68             '03' => 'March',
69             '04' => 'April',
70             '05' => 'May',
71             '06' => 'June',
72             '07' => 'July',
73             '08' => 'August',
74             '09' => 'September',
75             '10' => 'October',
76             '11' => 'November',
77             '12' => 'December',
78             );
79            
80 1         45 my %time = (
81             '01' => ['1', 'a.m.'],
82             '02' => ['2', 'a.m.'],
83             '03' => ['3', 'a.m.'],
84             '04' => ['4', 'a.m.'],
85             '05' => ['5', 'a.m.'],
86             '06' => ['6', 'a.m.'],
87             '07' => ['7', 'a.m.'],
88             '08' => ['8', 'a.m.'],
89             '09' => ['9', 'a.m.'],
90             '10' => ['10', 'a.m.'],
91             '11' => ['11', 'a.m.'],
92             '12' => ['12', 'p.m.'],
93             '13' => ['1', 'p.m.'],
94             '14' => ['2', 'p.m.'],
95             '15' => ['3', 'p.m.'],
96             '16' => ['4', 'p.m.'],
97             '17' => ['5', 'p.m.'],
98             '18' => ['6', 'p.m.'],
99             '19' => ['7', 'p.m.'],
100             '20' => ['8', 'p.m.'],
101             '21' => ['9', 'p.m.'],
102             '22' => ['10', 'p.m.'],
103             '23' => ['11', 'p.m.'],
104             '24' => ['12', 'a.m.'],
105             '00' => ['12', 'a.m.'],
106             );
107            
108             # Abbreviate day/month.
109 1         4 my $day_abbrev = substr($day_alpha{$week_day}, 0,3);
110 1         4 my $month_abbrev = substr($month_alpha{$month}, 0,3);
111            
112             # Update the date object and return.
113 1         20 my $self = {
114             _seconds => $seconds,
115             _minutes => $minutes,
116             _hour => $hour,
117             _month_day => $month_day,
118             _month => $month,
119             _year => $year,
120             _week_day => $week_day,
121             _year_day => $year_day,
122             _day_alpha => $day_alpha{$week_day},
123             _day_abbrev => $day_abbrev,
124             _month_alpha => $month_alpha{$month},
125             _month_abbrev => $month_abbrev,
126             _time_12 => [ $time{$hour}[0], $time{$hour}[1] ],
127             };
128 1         4 bless($self, $class);
129 1         16 return($self);
130             }
131              
132             # --------------------------------------------------------------------------
133             # R E T U R N Y E A R (method)
134             # ==========================================================================
135             # USAGE : $date->return_year(length=>'long')
136             # PURPOSE : Returns year of type '05' or '2005'.
137             # RETURNS : Scalar.
138             # PARAMETERS : length=>'short'
139             # : length=>'long'
140             # THROWS : Croaks on bad/missing arguments.
141             # COMMENTS : Only numeric format available.
142             # SEE ALSO : n/a
143             # --------------------------------------------------------------------------
144             sub return_year {
145 9     9 1 31 my ($class, %args) = @_;
146 9         10 my $year;
147 9 100       35 if ($args{length} eq "short") {
    50          
148 1         10 $year = substr($class->{_year}, -2);
149             }
150             elsif ($args{length} eq "long") {
151 8         20 $year = $class->{_year};
152             }
153             else {
154 0         0 croak "DateStamp reports: Bad/missing \"length\" argument.\n";
155             }
156 9         35 return($year);
157             }
158              
159             # --------------------------------------------------------------------------
160             # R E T U R N M O N T H (method)
161             # ==========================================================================
162             # USAGE : $date->return_month(format=>'alpha', length=>'long')
163             # PURPOSE : Returns month of type '10', 'Oct', 'October'.
164             # RETURNS : Scalar.
165             # PARAMETERS : format=>'alpha'
166             # : format=>'numeric'
167             # : length=>'short'
168             # : length=>'long'
169             # THROWS : Croaks on bad/missing arguments.
170             # COMMENTS : Alpha can return short/long format.
171             # SEE ALSO : n/a
172             # --------------------------------------------------------------------------
173             sub return_month {
174 13     13 1 40 my ($class, %args) = @_;
175 13         19 my $month;
176 13 100       35 if ($args{format} eq "alpha") {
    50          
177 8 100       25 if ($args{length} eq "short") {
    50          
178 4         11 $month = $class->{_month_abbrev};
179             }
180             elsif ($args{length} eq "long") {
181 4         11 $month = $class->{_month_alpha};
182             }
183             else {
184 0         0 croak "DateStamp reports: Bad/missing \"length\" argument.\n";
185             }
186             }
187             elsif($args{format} eq "numeric") {
188 5         12 $month = $class->{_month};
189             }
190             else {
191 0         0 croak "DateStamp reports: Bad/missing \"format\" argument.\n";
192             }
193 13         46 return($month);
194             }
195              
196             # --------------------------------------------------------------------------
197             # R E T U R N D A Y (method)
198             # ==========================================================================
199             # USAGE : $date->return_day(format=>'alpha', length=>'long')
200             # PURPOSE : Returns day of type 'Friday', 'Fri', '24'.
201             # RETURNS : Scalar.
202             # PARAMETERS : format=>'alpha'
203             # : format=>'numeric'
204             # : length=>'short'
205             # : length=>'long'
206             # THROWS : Croaks on bad/missing arguments.
207             # COMMENTS : Alpha can return short/long format.
208             # SEE ALSO : n/a
209             # --------------------------------------------------------------------------
210             sub return_day {
211 14     14 1 197 my ($class, %args) = @_;
212 14         40 my $day;
213 14 100       44 if ($args{format} eq "alpha") {
    50          
214 4 100       17 if ($args{length} eq "short") {
    50          
215 2         6 $day = $class->{_day_abbrev};
216             }
217             elsif ($args{length} eq "long") {
218 2         5 $day = $class->{_day_alpha};
219             }
220             else {
221 0         0 croak "DateStamp reports: Bad/missing \"length\" argument.\n";
222             }
223             }
224             elsif($args{format} eq "numeric") {
225 10         18 $day = $class->{_month_day};
226             }
227             else {
228 0         0 croak "DateStamp reports: Bad/missing \"format\" argument.\n";
229             }
230 14         46 return($day);
231             }
232              
233             # --------------------------------------------------------------------------
234             # R E T U R N T I M E S T A M P (method)
235             # ==========================================================================
236             # USAGE : $date->return_time(format=>'12', length=>'short')
237             # PURPOSE : Returns timestamp of types:
238             # : 5:30 p.m.
239             # : 5:30:20 p.m.
240             # : 17:30
241             # : 17:30:20
242             # : Fri Nov 25 04:15:00 2005
243             # RETURNS : Scalar.
244             # PARAMETERS : format=>'alpha'
245             # : format=>'numeric'
246             # : format=>'localtime'
247             # : length=>'short'
248             # : length=>'long'
249             # THROWS : Croaks on bad/missing arguments.
250             # COMMENTS : Converts from 12-hour to 24-hour format.
251             # SEE ALSO : n/a
252             # --------------------------------------------------------------------------
253             sub return_time {
254 6     6 1 22 my ($class, %args) = @_;
255 6         9 my $timestamp;
256             # Localtime format (Sun Nov 27 00:13:56 2005):
257 6 100       29 if ($args{format} eq "localtime") {
    100          
    50          
258 1         5 my $day = $class->return_day( format=>'alpha', length=>'short' );
259 1         5 my $month = $class->return_month( format=>'alpha', length=>'short' );
260 1         4 my $mday = $class->{_month_day};
261 1         11 my $time = $class->return_time( format=>'24', length=>'long' );
262 1         4 my $year = $class->{_year};
263 1         3 my @timestamp = ($day, $month, $mday, $time, $year);
264 1         6 $timestamp = join(" ", @timestamp);
265             }
266             # 12 hour format (12:56 a.m. or 12:56:32 a.m.):
267             elsif ($args{format} eq "12") {
268 2 100       9 if ($args{length} eq "short") {
    50          
269 1         2 $timestamp = ${$class->{_time_12}}[0] . ":" . $class->{_minutes} . " " . ${$class->{_time_12}}[1];
  1         5  
  1         4  
270             }
271             elsif ($args{length} eq "long") {
272 1         2 $timestamp = ${$class->{_time_12}}[0] . ":" . $class->{_minutes} . ":" . $class->{_seconds} . " " . ${$class->{_time_12}}[1];
  1         6  
  1         3  
273             }
274             else {
275 0         0 croak "DateStamp reports: Bad/missing \"length\" argument.\n";
276             }
277             }
278             # 24 hour format (00:56 or 00:56:32):
279             elsif ($args{format} eq "24") {
280 3 100       10 if ($args{length} eq "short") {
    50          
281 1         4 $timestamp = $class->{_hour} . ":" . $class->{_minutes};
282             }
283             elsif ($args{length} eq "long") {
284 2         8 $timestamp = $class->{_hour} . ":" . $class->{_minutes} . ":" . $class->{_seconds};
285             }
286             else {
287 0         0 croak "DateStamp reports: Bad/missing \"length\" argument.\n";
288             }
289             }
290             else {
291 0         0 croak "DateStamp reports: Bad/missing \"format\" argument.\n";
292             }
293 6         659 return($timestamp);
294             }
295              
296             # --------------------------------------------------------------------------
297             # R E T U R N D A T E (method)
298             # ==========================================================================
299             # USAGE : $date->return_date(format=>'mmddyyyy', glue=>'/')
300             # PURPOSE : Returns various common date formats for user:
301             # : 20050331 or 2005-03-31
302             # : 03312005 or 03-31-2005
303             # : March 31, 2005
304             # : Mar 31, 2005
305             # : Thursday, March 31, 2005
306             # : March 31
307             # : Mar 31
308             # RETURNS : Scalar.
309             # PARAMETERS : format=>'yyyymmdd'
310             # : format=>'mmddyyyy'
311             # : format=>'month-day-year'
312             # : format=>'mon-day-year'
313             # : format=>'weekday-month-day-year'
314             # : format=>'month-day'
315             # : format=>'mon-day'
316             # THROWS : Croaks on bad/missing arguments.
317             # COMMENTS : The formats provided are not meant to be exhaustive. All of
318             # : the provided formats were built using DateStamp methods.
319             # : Further formats are left as an exercise to the user.
320             # SEE ALSO : return_year
321             # : return_month
322             # : return_day
323             # : return_time
324             # --------------------------------------------------------------------------
325             sub return_date {
326 9     9 1 30 my ($class, %args) = @_;
327 9         12 my $date;
328              
329             # YYYYMMDD format (20050331 or 2005-03-31):
330 9 100       52 if ($args{format} eq "yyyymmdd") {
    100          
    100          
    100          
    100          
    100          
    50          
331 2         7 my $year = $class->return_year( length=>'long' );
332 2         9 my $month = $class->return_month( format=>'numeric' );
333 2         7 my $day = $class->return_day( format=>'numeric' );
334 2 100       7 if ($args{glue}) {
335 1         4 my @dates = ($year, $month, $day);
336 1         5 $date = join("$args{glue}", @dates);
337             }
338             else {
339 1         4 $date = $year . $month . $day;
340             }
341             }
342              
343             # MMDDYYYY format (03312005 or 03-31-2005):
344             elsif ($args{format} eq "mmddyyyy") {
345 2         6 my $year = $class->return_year( length=>'long' );
346 2         7 my $month = $class->return_month( format=>'numeric' );
347 2         5 my $day = $class->return_day( format=>'numeric' );
348 2 100       6 if ($args{glue}) {
349 1         4 my @dates = ($month, $day, $year);
350 1         5 $date = join("$args{glue}", @dates);
351             }
352             else {
353 1         4 $date = $month . $day . $year;
354             }
355             }
356            
357             # Month-day-year format (March 31, 2005):
358             elsif ($args{format} eq "month-day-year") {
359 1         4 my $month = $class->return_month( format=>'alpha', length=>'long' );
360 1         3 my $day = $class->return_day( format=>'numeric' );
361 1         4 my $year = $class->return_year( length=>'long' );
362 1         4 $date = $month . " " . $day . ", " . $year;
363             }
364            
365             # Mon-day-year format (Mar 31, 2005):
366             elsif ($args{format} eq "mon-day-year") {
367 1         5 my $month = $class->return_month( format=>'alpha', length=>'short' );
368 1         4 my $day = $class->return_day( format=>'numeric' );
369 1         4 my $year = $class->return_year( length=>'long' );
370 1         5 $date = $month . " " . $day . ", " . $year;
371             }
372            
373             # Weekday-month-day-year format (Thursday, March 31, 2005):
374             elsif ($args{format} eq "weekday-month-day-year") {
375 1         3 my $weekday = $class->return_day( format=>'alpha', length=>'long' );
376 1         4 my $month = $class->return_month( format=>'alpha', length=>'long' );
377 1         4 my $day = $class->return_day( format=>'numeric' );
378 1         4 my $year = $class->return_year( length=>'long' );
379 1         6 $date = $weekday . ", " . $month . " " . $day . ", " . $year;
380             }
381              
382             # Month-day format (March 31):
383             elsif ($args{format} eq "month-day") {
384 1         4 my $month = $class->return_month( format=>'alpha', length=>'long' );
385 1         5 my $day = $class->return_day( format=>'numeric' );
386 1         3 $date = $month . " " . $day;
387             }
388            
389             # Mon-day format (Mar 31):
390             elsif ($args{format} eq "mon-day") {
391 1         4 my $month = $class->return_month( format=>'alpha', length=>'short' );
392 1         4 my $day = $class->return_day( format=>'numeric' );
393 1         3 $date = $month . " " . $day;
394             }
395              
396             else {
397 0         0 croak "DateStamp reports: Bad/missing \"format\" argument.\n";
398             }
399 9         58 return($date);
400             }
401              
402             1; # End of module.
403              
404             __END__