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