4 our $VERSION = '1.3202';
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;
148 sub _counted_objects {
149 my ($n, $counted) = @_;
150 my $number = sprintf("%d", $n); # does a "floor"
151 $counted .= 's' if 1 != $number;
152 return ($number, $counted);
162 if ($s >= ONE_MINUTE) {
163 if ($s >= ONE_HOUR) {
165 my ($days, $sd) = _counted_objects($s->days, "day");
166 $str .= "$days $sd, ";
167 $s -= ($days * ONE_DAY);
169 my ($hours, $sh) = _counted_objects($s->hours, "hour");
170 $str .= "$hours $sh, ";
171 $s -= ($hours * ONE_HOUR);
173 my ($mins, $sm) = _counted_objects($s->minutes, "minute");
174 $str .= "$mins $sm, ";
175 $s -= ($mins * ONE_MINUTE);
177 $str .= join " ", _counted_objects($s->seconds, "second");
188 Time::Seconds - a simple API to convert seconds to other date values
201 print "Difference is: ", $s->days, "\n";
205 This module is part of the Time::Piece distribution. It allows the user
206 to find out the number of minutes, hours, days, weeks or years in a given
207 number of seconds. It is returned by Time::Piece when you delta two
210 Time::Seconds also exports the following constants:
222 Since perl does not (yet?) support constant objects, these constants are in
223 seconds only, so you cannot, for example, do this: C<print ONE_WEEK-E<gt>minutes;>
227 The following methods are available:
229 my $val = Time::Seconds->new(SECONDS)
236 $val->financial_months; # 30 days
238 $val->pretty; # gives English representation of the delta
240 The usual arithmetic (+,-,+=,-=) is also available on the objects.
242 The methods make the assumption that there are 24 hours in a day, 7 days in
243 a week, 365.24225 days in a year and 12 months in a year.
244 (from The Calendar FAQ at http://www.tondering.dk/claus/calendar.html)
248 Matt Sergeant, matt@sergeant.org
250 Tobias Brox, tobiasb@tobiasb.funcom.com
252 Balázs Szabó (dLux), dlux@kapu.hu
254 =head1 COPYRIGHT AND LICENSE
256 Copyright 2001, Larry Wall.
258 This module is free software, you may distribute it under the same terms
263 Currently the methods aren't as efficient as they could be, for reasons of
264 clarity. This is probably a bad idea.