3 use vars qw/@EXPORT @EXPORT_OK @ISA/;
21 @EXPORT_OK = qw(cs_sec cs_mon);
23 use constant ONE_MINUTE => 60;
24 use constant ONE_HOUR => 3_600;
25 use constant ONE_DAY => 86_400;
26 use constant ONE_WEEK => 604_800;
27 use constant ONE_MONTH => 2_629_744; # ONE_YEAR / 12
28 use constant ONE_REAL_MONTH => '1M';
29 use constant ONE_YEAR => 31_556_930; # 365.24225 days
30 use constant ONE_REAL_YEAR => '1Y';
31 use constant ONE_FINANCIAL_MONTH => 2_592_000; # 30 days
32 use constant LEAP_YEAR => 31_622_400; # 366 * ONE_DAY
33 use constant NON_LEAP_YEAR => 31_536_000; # 365 * ONE_DAY
35 # hacks to make Time::Piece compile once again
36 use constant cs_sec => 0;
37 use constant cs_mon => 1;
40 'fallback' => 'undef',
46 '-=' => \&subtract_from,
53 $val = 0 unless defined $val;
58 my ($lhs, $rhs, $reverse) = @_;
61 if (UNIVERSAL::isa($rhs, 'Time::Seconds')) {
65 die "Can't use non Seconds object in operator overload";
76 my ($lhs, $rhs) = _get_ovlvals(@_);
81 my ($lhs, $rhs) = _get_ovlvals(@_);
82 return Time::Seconds->new($lhs + $rhs);
88 $rhs = $rhs->seconds if UNIVERSAL::isa($rhs, 'Time::Seconds');
94 my ($lhs, $rhs) = _get_ovlvals(@_);
95 return Time::Seconds->new($lhs - $rhs);
101 $rhs = $rhs->seconds if UNIVERSAL::isa($rhs, 'Time::Seconds');
107 Time::Seconds->new(${$_[0]});
137 $s->days / 30.4368541;
140 sub financial_months {
147 $s->days / 365.24225;
157 if ($s >= ONE_MINUTE) {
158 if ($s >= ONE_HOUR) {
160 my $days = sprintf("%d", $s->days); # does a "floor"
161 $str = $days . " days, ";
162 $s -= ($days * ONE_DAY);
164 my $hours = sprintf("%d", $s->hours);
165 $str .= $hours . " hours, ";
166 $s -= ($hours * ONE_HOUR);
168 my $mins = sprintf("%d", $s->minutes);
169 $str .= $mins . " minutes, ";
170 $s -= ($mins * ONE_MINUTE);
172 $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
249 Please see Time::Piece for the license.
253 Currently the methods aren't as efficient as they could be, for reasons of
254 clarity. This is probably a bad idea.