6 use Exporter 5.57 'import';
20 our @EXPORT_OK = qw(cs_sec cs_mon);
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
38 'fallback' => 'undef',
44 '-=' => \&subtract_from,
51 $val = 0 unless defined $val;
56 my ($lhs, $rhs, $reverse) = @_;
59 if (UNIVERSAL::isa($rhs, 'Time::Seconds')) {
63 die "Can't use non Seconds object in operator overload";
74 my ($lhs, $rhs) = _get_ovlvals(@_);
79 my ($lhs, $rhs) = _get_ovlvals(@_);
80 return Time::Seconds->new($lhs + $rhs);
86 $rhs = $rhs->seconds if UNIVERSAL::isa($rhs, 'Time::Seconds');
92 my ($lhs, $rhs) = _get_ovlvals(@_);
93 return Time::Seconds->new($lhs - $rhs);
99 $rhs = $rhs->seconds if UNIVERSAL::isa($rhs, 'Time::Seconds');
105 Time::Seconds->new(${$_[0]});
135 $s->days / 30.4368541;
138 sub financial_months {
145 $s->days / 365.24225;
155 if ($s >= ONE_MINUTE) {
156 if ($s >= ONE_HOUR) {
158 my $days = sprintf("%d", $s->days); # does a "floor"
159 $str .= $days . " days, ";
160 $s -= ($days * ONE_DAY);
162 my $hours = sprintf("%d", $s->hours);
163 $str .= $hours . " hours, ";
164 $s -= ($hours * ONE_HOUR);
166 my $mins = sprintf("%d", $s->minutes);
167 $str .= $mins . " minutes, ";
168 $s -= ($mins * ONE_MINUTE);
170 $str .= $s->seconds . " seconds";
181 Time::Seconds - a simple API to convert seconds to other date values
194 print "Difference is: ", $s->days, "\n";
198 This module is part of the Time::Piece distribution. It allows the user
199 to find out the number of minutes, hours, days, weeks or years in a given
200 number of seconds. It is returned by Time::Piece when you delta two
203 Time::Seconds also exports the following constants:
215 Since perl does not (yet?) support constant objects, these constants are in
216 seconds only, so you cannot, for example, do this: C<print ONE_WEEK-E<gt>minutes;>
220 The following methods are available:
222 my $val = Time::Seconds->new(SECONDS)
229 $val->financial_months; # 30 days
231 $val->pretty; # gives English representation of the delta
233 The usual arithmetic (+,-,+=,-=) is also available on the objects.
235 The methods make the assumption that there are 24 hours in a day, 7 days in
236 a week, 365.24225 days in a year and 12 months in a year.
237 (from The Calendar FAQ at http://www.tondering.dk/claus/calendar.html)
241 Matt Sergeant, matt@sergeant.org
243 Tobias Brox, tobiasb@tobiasb.funcom.com
245 Balázs Szabó (dLux), dlux@kapu.hu
247 =head1 COPYRIGHT AND LICENSE
249 Copyright 2001, Larry Wall.
251 This module is free software, you may distribute it under the same terms
256 Currently the methods aren't as efficient as they could be, for reasons of
257 clarity. This is probably a bad idea.