File Coverage

blib/lib/Time/Seconds.pm
Criterion Covered Total %
statement 81 81 100.0
branch 22 22 100.0
condition n/a
subroutine 22 22 100.0
pod 0 16 0.0
total 125 141 88.6


line stmt bran cond sub pod time code
1             package Time::Seconds;
2 15     15   94 use strict;
  15         29  
  15         932  
3              
4             our $VERSION = '1.41';
5              
6 15     15   139 use Exporter 5.57 'import';
  15         487  
  15         1942  
7              
8             our @EXPORT = qw(
9             ONE_MINUTE
10             ONE_HOUR
11             ONE_DAY
12             ONE_WEEK
13             ONE_MONTH
14             ONE_YEAR
15             ONE_FINANCIAL_MONTH
16             LEAP_YEAR
17             NON_LEAP_YEAR
18             );
19              
20             our @EXPORT_OK = qw(cs_sec cs_mon);
21              
22             use constant {
23 15         4782 ONE_MINUTE => 60,
24             ONE_HOUR => 3_600,
25             ONE_DAY => 86_400,
26             ONE_WEEK => 604_800,
27             ONE_MONTH => 2_629_744, # ONE_YEAR / 12
28             ONE_YEAR => 31_556_930, # 365.24225 days
29             ONE_FINANCIAL_MONTH => 2_592_000, # 30 days
30             LEAP_YEAR => 31_622_400, # 366 * ONE_DAY
31             NON_LEAP_YEAR => 31_536_000, # 365 * ONE_DAY
32             # hacks to make Time::Piece compile once again
33             cs_sec => 0,
34             cs_mon => 1,
35 15     15   103 };
  15         39  
36              
37             use overload
38 15         187 'fallback' => 'undef',
39             '0+' => \&seconds,
40             '""' => \&seconds,
41             '<=>' => \&compare,
42             '+' => \&add,
43             '-' => \&subtract,
44             '-=' => \&subtract_from,
45             '+=' => \&add_to,
46 15     15   11522 '=' => \©
  15         29142  
47              
48             sub new {
49 25     25 0 88 my $class = shift;
50 25         60 my ($val) = @_;
51 25 100       76 $val = 0 unless defined $val;
52 25         132 bless \$val, $class;
53             }
54              
55             sub _get_ovlvals {
56 23     23   69 my ($lhs, $rhs, $reverse) = @_;
57 23         60 $lhs = $lhs->seconds;
58              
59 23 100       154 if (UNIVERSAL::isa($rhs, 'Time::Seconds')) {
    100          
60 1         4 $rhs = $rhs->seconds;
61             }
62             elsif (ref($rhs)) {
63 1         11 die "Can't use non Seconds object in operator overload";
64             }
65              
66 22 100       46 if ($reverse) {
67 3         14 return $rhs, $lhs;
68             }
69              
70 19         48 return $lhs, $rhs;
71             }
72              
73             sub compare {
74 17     17 0 30 my ($lhs, $rhs) = _get_ovlvals(@_);
75 17         55 return $lhs <=> $rhs;
76             }
77              
78             sub add {
79 4     4 0 20 my ($lhs, $rhs) = _get_ovlvals(@_);
80 3         12 return Time::Seconds->new($lhs + $rhs);
81             }
82              
83             sub add_to {
84 3     3 0 8 my $lhs = shift;
85 3         7 my $rhs = shift;
86 3 100       18 $rhs = $rhs->seconds if UNIVERSAL::isa($rhs, 'Time::Seconds');
87 3         8 $$lhs += $rhs;
88 3         9 return $lhs;
89             }
90              
91             sub subtract {
92 2     2 0 6 my ($lhs, $rhs) = _get_ovlvals(@_);
93 2         8 return Time::Seconds->new($lhs - $rhs);
94             }
95              
96             sub subtract_from {
97 10     10 0 19 my $lhs = shift;
98 10         15 my $rhs = shift;
99 10 100       40 $rhs = $rhs->seconds if UNIVERSAL::isa($rhs, 'Time::Seconds');
100 10         20 $$lhs -= $rhs;
101 10         23 return $lhs;
102             }
103              
104             sub copy {
105 4     4 0 10 Time::Seconds->new(${$_[0]});
  4         18  
106             }
107              
108             sub seconds {
109 41     41 0 1733 my $s = shift;
110 41         174 return $$s;
111             }
112              
113             sub minutes {
114 25     25 0 60 my $s = shift;
115 25         214 return $$s / 60;
116             }
117              
118             sub hours {
119 16     16 0 30 my $s = shift;
120 16         44 $s->minutes / 60;
121             }
122              
123             sub days {
124 11     11 0 480 my $s = shift;
125 11         33 $s->hours / 24;
126             }
127              
128             sub weeks {
129 1     1 0 29 my $s = shift;
130 1         5 $s->days / 7;
131             }
132              
133             sub months {
134 4     4 0 9 my $s = shift;
135 4         43 $s->days / 30.4368541;
136             }
137              
138             sub financial_months {
139 1     1 0 7 my $s = shift;
140 1         4 $s->days / 30;
141             }
142              
143             sub years {
144 1     1 0 3 my $s = shift;
145 1         4 $s->days / 365.24225;
146             }
147              
148             sub _counted_objects {
149 14     14   31 my ($n, $counted) = @_;
150 14         39 my $number = sprintf("%d", $n); # does a "floor"
151 14 100       64 $counted .= 's' if 1 != $number;
152 14         47 return ($number, $counted);
153             }
154              
155             sub pretty {
156 5     5 0 25 my $s = shift;
157 5         11 my $str = "";
158 5 100       21 if ($s < 0) {
159 2         10 $s = -$s;
160 2         5 $str = "minus ";
161             }
162 5 100       13 if ($s >= ONE_MINUTE) {
163 4 100       9 if ($s >= ONE_HOUR) {
164 3 100       6 if ($s >= ONE_DAY) {
165 2         7 my ($days, $sd) = _counted_objects($s->days, "day");
166 2         6 $str .= "$days $sd, ";
167 2         21 $s -= ($days * ONE_DAY);
168             }
169 3         9 my ($hours, $sh) = _counted_objects($s->hours, "hour");
170 3         9 $str .= "$hours $sh, ";
171 3         7 $s -= ($hours * ONE_HOUR);
172             }
173 4         12 my ($mins, $sm) = _counted_objects($s->minutes, "minute");
174 4         9 $str .= "$mins $sm, ";
175 4         12 $s -= ($mins * ONE_MINUTE);
176             }
177 5         12 $str .= join " ", _counted_objects($s->seconds, "second");
178 5         33 return $str;
179             }
180              
181             1;
182             __END__