File Coverage

blib/lib/Hub/Misc/Time.pm
Criterion Covered Total %
statement 6 45 13.3
branch 0 34 0.0
condition 0 5 0.0
subroutine 2 4 50.0
pod 2 2 100.0
total 10 90 11.1


line stmt bran cond sub pod time code
1             package Hub::Misc::Time;
2 1     1   6 use strict;
  1         2  
  1         42  
3 1     1   6 use Hub qw/:lib/;
  1         2  
  1         5  
4             our $VERSION = '4.00043';
5             our @EXPORT = qw//;
6             our @EXPORT_OK = qw/
7             datetime
8             dhms
9             /;
10              
11             our @MONTH_NAMES = (
12             "January",
13             "February",
14             "March",
15             "April",
16             "May",
17             "June",
18             "July",
19             "August",
20             "September",
21             "October",
22             "November",
23             "December"
24             );
25              
26             our @MONTH_ABBRS = (
27             "Jan",
28             "Feb",
29             "Mar",
30             "Apr",
31             "May",
32             "Jun",
33             "Jul",
34             "Aug",
35             "Sep",
36             "Oct",
37             "Nov",
38             "Dec"
39             );
40              
41             our @DAY_NAMES = (
42             "Sunday",
43             "Monday",
44             "Tueday",
45             "Wednesday",
46             "Thursday",
47             "Friday",
48             "Saturday"
49             );
50              
51             our @DAY_ABBRS = (
52             "Sun",
53             "Mon",
54             "Tue",
55             "Wed",
56             "Thr",
57             "Fri",
58             "Sat"
59             );
60              
61             # ------------------------------------------------------------------------------
62             # datetime - Friendly date-time formats of seconds-since-the-epoch timestamps.
63             # datetime [$timestamp], [options]
64             #
65             # Default is the current time formatted as: MM/DD/YYYY hh:mm:ss.
66             # The decimal portion of HiRest time is truncated.
67             # Uses `localtime` to localize.
68             # ------------------------------------------------------------------------------
69             #|test(true) datetime( );
70             #|test(regex,02\/..\/2003 ..:..:24) datetime( 1045837284 );
71             #|test(regex,02\/..\/2003 ..:..) datetime( 1045837284, -nosec );
72             #|test(regex,02\/.. ..:..:24) datetime( 1045837284, -noyear );
73             #|test(regex,02\/..\/2003 ..:..:24am) datetime( 1045837284, -ampm );
74             #|test(regex,2\/..\/2003 .:..:24) datetime( 1045837284, -nozeros );
75             #|test(regex,02\/..\/2003) datetime( 1045837284, -notime );
76             #|test(regex,..:..:24) datetime( 1045837284, -nodate );
77             #|test(regex,February .., 2003 ..:..:24) datetime( 1045837284, -letter );
78             #
79             # Combining options
80             #
81             #|test(regex) datetime( 1045837284, -ampm, -nosec );
82             #~ 02\/..\/2003 ..:..am
83             #
84             #|test(regex) datetime( 1045837284, -nosec, -nozeros, -noyear );
85             #~ 2\/.. .:..
86             # ------------------------------------------------------------------------------
87              
88             sub datetime {
89 0     0 1   my ($opts, $timestamp) = Hub::opts(\@_);
90 0   0       $timestamp ||= time;
91             # Get fields for requested time
92 0           my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
93             localtime($timestamp);
94             # Aquire logical values
95 0           my $ampm = "";
96 0 0         if( $$opts{'ampm'} ) {
97 0 0         $ampm = $hour > 12 ? "pm" : "am";
98 0 0         $hour = $hour > 12 ? $hour - 12 : $hour;
99             }#if
100 0 0         my $digit_format = $$opts{'nozeros'} ? '%d' : '%02d';
101 0           my $props = {
102             day => sprintf( $digit_format, $mday ),
103             month => sprintf( $digit_format, $mon+1 ),
104             year => $year + 1900,
105             hour => sprintf( $digit_format, $hour ),
106             minute => sprintf( '%02d', $min ),
107             second => sprintf( '%02d', $sec ),
108             ampm => $ampm,
109             };
110 0           my $names = {
111             MONTH => $MONTH_NAMES[$mon],
112             MM => $MONTH_ABBRS[$mon],
113             DAY => $DAY_NAMES[$wday],
114             DD => $DAY_ABBRS[$wday],
115             };
116             # Default formats
117 0           my $date = "[#month]/[#day]/[#year]";
118 0           my $time = "[#hour]:[#minute]:[#second][#ampm]";
119             # Apply formatting options
120 0 0         $$opts{'nosec'} and $time = "[#hour]:[#minute][#ampm]";
121 0 0         $$opts{'noyear'} and $date = "[#month]/[#day]";
122 0 0         $$opts{'letter'} and $date = "[#MONTH] [#day], [#year]";
123 0 0         $$opts{'mysql'} and $date = "[#year]-[#month]-[#day]";
124 0           my $format = "$date $time";
125 0 0         $$opts{'apache'} and $format =
126             "[#DD] [#MM] [#day] [#hour]:[#minute]:[#second] [#year]";
127 0 0         $$opts{'notime'} and $format = $date;
128 0 0         $$opts{'nodate'} and $format = $time;
129             # Populate template format with data and time
130 0           return Hub::populate(\$format, $props, $names);
131             }#dateTime
132              
133             # ------------------------------------------------------------------------------
134             # Hub::dhms( $seconds, $options, $format )
135             #
136             # Format the provided number of seconds in days, hours, minutes, and seconds.
137             #
138             # Examples: Returns:
139             # ------------------------------------------------------- --------------------
140             # Hub::dhms( 10 ) 00d:00h:00m:10s
141             # Hub::dhms( 60 ) 00d:00h:01m:00s
142             # Hub::dhms( 3600 ) 00d:01h:00m:00s
143             # Hub::dhms( 86400 ) 01d:00h:00m:00s
144             # Hub::dhms( 11 ) 00d:00h:00m:11s
145             # Hub::dhms( 71 ) 00d:00h:01m:11s
146             # Hub::dhms( 3671 ) 00d:01h:01m:11s
147             # Hub::dhms( 90071 ) 01d:01h:01m:11s
148             # Hub::dhms( 90071, "--nozeros" ) 1d:1h:1m:11s
149             # Hub::dhms( 90071, "--nozeros" ) 1d:1h:1m:11s
150             # Hub::dhms( 90071, "--nozeros", "[#d]days [#h]:[#m]:[#s]" ) 1days 1:1:11
151             # Hub::dhms( 90071, "[#d]days [#h]:[#m]:[#s]" ) 01days 01:01:11
152             #
153             # ------------------------------------------------------------------------------
154             sub dhms {
155 0     0 1   my ($seconds,$options,$format) = @_;
156 0 0         return unless $seconds;
157 0 0         unless( $format ) {
158 0 0         $format = $options unless $options =~ /--/;
159             }
160 0           my $digit_format = "%02d";
161 0 0         $options =~ /nozeros/ and $digit_format = "%d";
162 0 0         $options =~ /MS/ and $format = "[#m]m:[#s]s";
163 0 0         $options =~ /HMS/ and $format = "[#h]h:[#m]m:[#s]s";
164 0   0       $format ||= "[#d]d:[#h]h:[#m]m:[#s]s";
165 0           my $dhms = {
166             d => 0,
167             h => 0,
168             m => 0,
169             s => 0,
170             };
171 0           $$dhms{'d'} = sprintf( $digit_format, int($seconds/86400) );
172 0           $seconds -= ($$dhms{'d'} * 86400);
173 0           $$dhms{'h'} = sprintf( $digit_format, int($seconds/3600) );
174 0           $seconds -= ($$dhms{'h'} * 3600);
175 0           $$dhms{'m'} = sprintf( $digit_format, int($seconds/60) );
176 0           $seconds -= ($$dhms{'m'} * 60);
177 0           $$dhms{'s'} = sprintf( "%02d", $seconds ); # sec don't acknowledge 'nozeros'
178 0           return Hub::populate( \$format, $dhms );
179             }#dhms
180              
181             # ------------------------------------------------------- --------------------
182             1;