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