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