File Coverage

blib/lib/Date/Time2fmtstr.pm
Criterion Covered Total %
statement 9 116 7.7
branch 0 48 0.0
condition 0 26 0.0
subroutine 3 48 6.2
pod 1 1 100.0
total 13 239 5.4


line stmt bran cond sub pod time code
1             =head1 NAME
2              
3             Date::Time2fmtstr - Functions to format Perl time integers to strings based on a "Picture" format string.
4              
5             =head1 AUTHOR
6              
7             Jim Turner
8              
9             (c) 2015, Jim Turner under the same license that Perl 5 itself is. All rights reserved.
10              
11             =head1 SYNOPSIS
12              
13             use Date::Time2fmtstr;
14              
15             print time2str(time, 'mm-dd-yyyy hh:mi PM');
16              
17             =head1 DESCRIPTION
18              
19             Date::Time2fmtstr provides a single function B that accepts a standard Perl (Unix)
20             "time" value (a large integer equivalent to the number of seconds since 1980) and converts it
21             to a string value based on a I consisting of special substrings which represent
22             the various parts of a date and time value. It returns a string that is essentially the
23             same as the original I with each of these special substrings replaced with
24             the corresponding date/time value.
25              
26             =head1 METHODS
27              
28             =over 4
29              
30             =item <$string> = B(I
31              
32             Returns a string corresponding to the specified I with each special substring
33             replaced with the corresponding date/time data field. For example:
34              
35             $s = B(1452324044, 'mm-dd-yyyy hh:mi PM (Day) (Month)');
36              
37             would set $s to '01-09-2016 01:20 AM (Sat) (January)'.
38              
39             =item B
40              
41             There are numerous choices of special format substrings which can be used in an infinite
42             number of combinations to produce the desired results. They are listed below:
43              
44             =over 4
45              
46             B - The Full name of the month in all lower case, ie. "january".
47              
48             B - The Full name of the month capitalized, ie. "January".
49              
50             B - The Full name of the month all capitalized, ie. "JANUARY".
51              
52             B - Day of the week in all lower case, ie. "sunday".
53              
54             B - Day of the week capitalized, ie. "Sunday".
55              
56             B - Day of the week all capitalized, ie. "SUNDAY".
57              
58             B - Three letter abbreviation of the day of the week in all lower case, ie. "sun".
59              
60             B - Three letter abbreviation of the day of the week capitalized, ie. "Sun".
61              
62             B - Three letter abbreviation of the day of the week all capitalized, ie. "SUN".
63              
64             B - Num. of days since beginning of year.
65              
66             B
- Day of month (2 digits, left padded with a zero if needed), ie. "01".
67              
68             B - Day of month (1 or 2 digits, as needed), ie. "1".
69              
70             B, B - Numeric day of the week zero-based (Sunday=0, Monday=1, ... Saturday=6).
71              
72             B - Numeric day of the week one-based (Sunday=1, Monday=2, ... Saturday=7).
73              
74             B - Numeric date in 8 digits, ie. "20150107" for January 7, 2015.
75              
76             B, B - Year in 4 digits.
77              
78             B, B - Year in last 2 digits.
79              
80             B - Military time (hours and minutes: 24 hours, no colon).
81              
82             B - Hour in common format, ie. 01-12.
83              
84             B

- Hour in common format, 1 or 2 digits, as needed, ie. 1-12.

85              
86             B - Minute, ie. 00-59.
87              
88             B - Number of month (2 digits, left padded with a zero if needed), ie. "01" for January.
89              
90             B - Three letter abbreviation of the month, in lower case, ie. "jan" for January.
91              
92             B - Hour in 24-hour format, 2 digits, left padded with a zero if needed, ie. 00-23.
93              
94             B

- Hour in 24-hour format, 1 or 2 digits, as needed, ie. 0-23.

95              
96             B - Three letter abbreviation of the month, capitalized, ie. "Jan" for January.
97              
98             B - Three letter abbreviation of the month all capitalized, ie. "JAN".
99              
100             B - Number of month (1 or 2 digits, as needed), ie. "1" for January.
101              
102             B - Seconds since start of day.
103              
104             B - Seconds since start of last minute (2 digits), ie. 00-59.
105              
106             B, B - display "am" if between Midnight and Noon, "pm" otherwise (both specifiers are identical).
107              
108             B, B - display "AM" if between Midnight and Noon, "PM" otherwise (both specifiers are identical).
109              
110             B, B

- display "a" if between Midnight and Noon, "p" otherwise (both specifiers are identical).

111              
112             B, B

- display "A" if between Midnight and Noon, "P" otherwise (both specifiers are identical).

113              
114             B - Roman numeral for the month (i-xii) in lower case.
115              
116             B - Roman numeral for the month (I-XII) in upper case.
117              
118             B - Number of week of the year (00-51).
119              
120             B - Number of the quarter of the year - (1-4).
121              
122             =back
123              
124             =back
125              
126             =head1 KEYWORDS
127              
128             L, L, formatting, picture_clause, strings
129              
130             =cut
131              
132             package Date::Time2fmtstr;
133              
134 1     1   22028 use strict;
  1         3  
  1         31  
135             #use warnings;
136 1     1   5 use vars qw(@ISA @EXPORT $VERSION);
  1         1  
  1         322  
137             $VERSION = '1.02';
138              
139             require Exporter;
140              
141             @ISA = qw(Exporter);
142             @EXPORT = qw(time2str);
143              
144             my @inputs = ();
145              
146             sub time2str
147             {
148 0   0 0 1   my $s = $_[0] || time;
149 0   0       my $f = $_[1] || 'yyyymmdd';
150              
151 0           my @fmts = split(/\b/, $f);
152 0           my @today = localtime(time);
153 0           @inputs = localtime($s);
154 0           my $resORerr = '';
155 0           my $rtnTime = '';
156 0           my $fn;
157              
158 0           OUTER1: for (my $i=0;$i<=$#fmts;$i++)
159             {
160 0 0         if ($fmts[$i] =~ /\W/o)
161             {
162 0           $resORerr .= $fmts[$i];
163 0           next;
164             }
165 0           MIDDLE1: while ($fmts[$i] =~ /\w/o)
166             {
167 0           foreach my $f (qw(month Month MONTH dayofweek Dayofweek DAYOFWEEK day Day DAY
168             ddd dd d1 d0 yyyymmdd yyyy yy hh24 hh HH H1 h1 mi mm mon
169             Mon MON m1 sssss ss am pm AM PM a p A P rm RM rr d ww q))
170             {
171 0 0         if ($fmts[$i] =~ s/^$f//)
172             {
173 0           $fn = '_toc_'.$f;
174 1     1   6 no strict 'refs';
  1         5  
  1         1595  
175 0           $resORerr .= &$fn();
176 0           next MIDDLE1;
177             }
178             }
179 0 0         if ($fmts[$i] =~ s/^(\w)(\w+)$/$2/)
180             {
181 0           $resORerr .= $1;
182 0           next MIDDLE1;
183             }
184 0           $resORerr .= $fmts[$i];
185 0           next OUTER1;
186             }
187             }
188              
189 0           return $resORerr;
190             }
191              
192             sub _toc_month
193             {
194 0     0     my @mthlist = (qw(january february march april may june july august september
195             october november december));
196              
197 0 0 0       return "Invalid Month ($inputs[4])! " unless ($inputs[4] >= 0 && $inputs[4] < 12);
198 0           return $mthlist[$inputs[4]];
199             }
200              
201             sub _toc_Month
202             {
203 0     0     my $mymonth = &_toc_month();
204 0           return "\u\L$mymonth\E"
205             }
206              
207             sub _toc_MONTH
208             {
209 0     0     my $mymonth = &_toc_month();
210 0           return "\U$mymonth\E";
211             }
212              
213             sub _toc_mon
214             {
215 0     0     my @mthlist = (qw(jan feb mar apr may jun jul aug sep oct nov dec));
216              
217 0 0 0       return "Invalid Month ($inputs[4])! " unless ($inputs[4] >= 0 && $inputs[4] < 12);
218 0           return $mthlist[$inputs[4]];
219             }
220              
221             sub _toc_Mon
222             {
223 0     0     my $mymonth = &_toc_mon();
224 0           return "\u\L$mymonth\E";
225             }
226              
227             sub _toc_MON
228             {
229 0     0     my $mymonth = &_toc_mon();
230 0           return "\U$mymonth\E";
231             }
232              
233             sub _toc_rm #ROMAN NUMBER MONTH - LOWER CASE
234             {
235 0     0     my @mthlist = (qw(i ii iii iv v vi vii viii ix x xi xii));
236              
237 0 0 0       return "Invalid Month ($inputs[4])! " unless ($inputs[4] >= 0 && $inputs[4] < 12);
238 0           return $mthlist[$inputs[4]];
239             }
240              
241             sub _toc_RM #ROMAN NUMBER MONTH - UPPER CASE
242             {
243 0     0     my $mymonth = &_toc_rm();
244 0           return "\U$mymonth\E";
245             }
246              
247             sub _toc_mm #MONTH (01-12)
248             {
249 0     0     my $mymth = $inputs[4] + 1;
250 0 0 0       return "Invalid Month ($mymth)! " unless ($mymth >= 1 && $mymth <= 12);
251 0 0         return '0'.$mymth if ($mymth < 10);
252 0           return $mymth;
253             }
254              
255             sub _toc_m1 #MONTH (1-12)
256             {
257 0     0     my $mymth = $inputs[4] + 1;
258 0 0 0       return "Invalid Month ($mymth)! " unless ($mymth >= 1 && $mymth <= 12);
259 0           return $mymth;
260             }
261              
262             sub _toc_yyyymmdd
263             {
264 0     0     return &_toc_yyyy() . &_toc_mm() . &_toc_dd();
265             }
266              
267             sub _toc_yyyy #4-DIGIT YEAR
268             {
269 0     0     return $inputs[5] + 1900;
270             }
271              
272             sub _toc_yy
273             {
274 0     0     my $myyr = $inputs[5];
275 0 0         return "Invalid Year ($myyr)! " unless ($myyr =~ /^[0-9]+$/o);
276 0           $myyr -= 100 while ($myyr >= 100);
277 0 0         return '0'.$myyr if ($myyr < 10);
278 0           return $myyr;
279             }
280              
281             sub _toc_rr
282             {
283 0     0     return &_toc_yy();
284             }
285              
286             sub _toc_rrrr
287             {
288 0     0     return &_toc_yyyy();
289             }
290              
291             sub _toc_ddd #DAY OF YEAR (1-365)
292             {
293 0     0     return $inputs[7] + 1;
294             }
295              
296             sub _toc_dd #DAY OF MONTH (01-31)
297             {
298 0 0   0     return '0'.$inputs[3] if ($inputs[3] < 10);
299 0           return $inputs[3];
300             }
301              
302             sub _toc_d1 #DAY OF MONTH (1-31)
303             {
304 0     0     return $inputs[3];
305             }
306              
307             sub _toc_hh24 #24-HOUR MILITARY TIME (0000-2359):
308             {
309 0     0     return sprintf('%4.4d', ($inputs[2] * 100) + $inputs[1]);
310             }
311              
312             sub _toc_HH #HOUR (00-23)
313             {
314 0 0   0     return '0'.$inputs[2] if ($inputs[2] < 10);
315 0           return $inputs[2];
316             }
317              
318             sub _toc_H1 #HOUR (0-23)
319             {
320 0     0     return $inputs[2];
321             }
322              
323             sub _toc_hh #HOUR (01-12)
324             {
325 0     0     my $hr = $inputs[2];
326 0 0         return 12 unless ($hr);
327 0 0         $hr -= 12 if ($hr > 12);
328 0 0         return '0'.$hr if ($hr < 10);
329 0           return $hr;
330             }
331              
332             sub _toc_h1 #HOUR (1-12)
333             {
334 0     0     my $hr = $inputs[2];
335 0 0         return 12 unless ($hr);
336 0 0         $hr -= 12 if ($hr > 12);
337 0           return $hr;
338             }
339              
340             sub _toc_a
341             {
342 0 0   0     return ($inputs[2] < 12) ? 'a' : 'p';
343             }
344              
345             sub _toc_p
346             {
347 0     0     return &_toc_a();
348             }
349              
350             sub _toc_A
351             {
352 0 0   0     return ($inputs[2] < 12) ? 'A' : 'P';
353             }
354              
355             sub _toc_P
356             {
357 0     0     return &_toc_A();
358             }
359              
360             sub _toc_am
361             {
362 0     0     return &_toc_a() . 'm';
363             }
364              
365             sub _toc_pm
366             {
367 0     0     return &_toc_a() . 'm';
368             }
369              
370             sub _toc_AM
371             {
372 0     0     return &_toc_A() . 'M';
373             }
374              
375             sub _toc_PM
376             {
377 0     0     return &_toc_A() . 'M';
378             }
379              
380             sub _toc_mi #MINUTES (00-59)
381             {
382 0 0   0     return '0'.$inputs[1] if ($inputs[1] < 10);
383 0           return $inputs[1];
384             }
385              
386             sub _toc_sssss #SECONDS OF THE DAY (0-86399)
387             {
388 0     0     return sprintf('%5.5d', (($inputs[2]*3600)+($inputs[1]*60)+$inputs[0]));
389             }
390              
391             sub _toc_ss #SECONDS
392             {
393 0 0   0     return '0'.$inputs[0] if ($inputs[0] < 10);
394 0           return $inputs[0];
395             }
396              
397             sub _toc_d #DAY OF WEEK (SUN=1..SAT=7
398             {
399 0     0     return $inputs[6] + 1;
400             }
401              
402             sub _toc_d0 #DAY OF WEEK (SUN=0..SAT=6
403             {
404 0     0     return $inputs[6];
405             }
406              
407             sub _toc_day
408             {
409 0     0     my @daylist = (qw(sun mon tue wed thu fri sat));
410              
411 0 0 0       return "Invalid Day ($inputs[6])! " unless ($inputs[6] >= 0 && $inputs[6] < 7);
412 0           return $daylist[$inputs[6]];
413             }
414              
415             sub _toc_Day
416             {
417 0     0     my $myday = &_toc_day();
418 0           return "\u\L$myday\E";
419             }
420              
421             sub _toc_DAY
422             {
423 0     0     my $myday = &_toc_day();
424 0           return "\U$myday\E";
425             }
426              
427             sub _toc_dayofweek
428             {
429 0     0     my @daylist = (qw(sunday monday tuesday wednesday thursday friday saturday));
430              
431 0 0 0       return "Invalid Day ($inputs[6])! " unless ($inputs[6] >= 0 && $inputs[6] < 7);
432 0           return $daylist[$inputs[6]];
433             }
434              
435             sub _toc_Dayofweek
436             {
437 0     0     my $myday = &_toc_dayofweek();
438 0           return "\u\L$myday\E";
439             }
440              
441             sub _toc_DAYOFWEEK
442             {
443 0     0     my $myday = &_toc_dayofweek();
444 0           return "\U$myday\E";
445             }
446              
447             sub _toc_ww #WEEK OF YEAR (0-51)
448             {
449 0     0     return &_toc_ddd % 7;
450             }
451              
452             sub _toc_q #QUARTER (1-4):
453             {
454 0     0     return int(&_toc_mm / 4) + 1;
455             }
456              
457             1
458              
459             __END__