summaryrefslogtreecommitdiff
path: root/cpan/Time-Piece/Seconds.pm
diff options
context:
space:
mode:
authorChris 'BinGOs' Williams <chris@bingosnet.co.uk>2010-06-26 15:04:03 +0100
committerChris 'BinGOs' Williams <chris@bingosnet.co.uk>2010-06-26 15:04:03 +0100
commit90d55c29481047f2784ec1daadd553c5710e090a (patch)
tree5296d5b2bfd4ffa1c5b8651d9c5882065275b2ff /cpan/Time-Piece/Seconds.pm
parent1db4d19556a36b5a8e8604c1e7656999ebc7732b (diff)
downloadperl-90d55c29481047f2784ec1daadd553c5710e090a.tar.gz
Update Time-Piece to CPAN version 1.20
[DELTA] 1.20 - Fix for alloca broke Solaris - Fixed documentation buggette about strptime - Added ->pretty() method for Time::Seconds objects - Add %s support to strptime 1.19 - Fix for alloca broke FreeBSD 1.18 - Fix for alloca on IRIX 1.17 - Force all to use internal strptime then everyone gets %z even OSX users. - Finally figured out the timezone test failures on Win32 and fixed them. 1.16 - Implement %z for the internal implementation of strptime(). Unfortunately this doesn't get picked up everywhere, so there are no tests for it (yet - patches welcome). - Fix for major bug in add_months() using negative months which were multiples of 12. Also affected add_years() with negative years. - Fix for object creation bug in get_epochs which called new from object but that wasn't supported in the new() code. - Added docs about the weakness of using epoch seconds internally and suggested alternatives. - Removed useless "use UNIVERSAL qw(isa)" line. - Fix for installing over core perl version.
Diffstat (limited to 'cpan/Time-Piece/Seconds.pm')
-rw-r--r--cpan/Time-Piece/Seconds.pm70
1 files changed, 48 insertions, 22 deletions
diff --git a/cpan/Time-Piece/Seconds.pm b/cpan/Time-Piece/Seconds.pm
index 4aac9881cb..1ecefa16ab 100644
--- a/cpan/Time-Piece/Seconds.pm
+++ b/cpan/Time-Piece/Seconds.pm
@@ -1,25 +1,22 @@
-# $Id: Seconds.pm 44 2002-09-08 20:51:38Z matt $
-
package Time::Seconds;
use strict;
use vars qw/@EXPORT @EXPORT_OK @ISA/;
-# use UNIVERSAL qw(isa); # Commented out for Perl 5.12.0 by JRV to avoid a deprecation warning.
@ISA = 'Exporter';
@EXPORT = qw(
- ONE_MINUTE
- ONE_HOUR
- ONE_DAY
- ONE_WEEK
- ONE_MONTH
- ONE_REAL_MONTH
- ONE_YEAR
- ONE_REAL_YEAR
- ONE_FINANCIAL_MONTH
- LEAP_YEAR
- NON_LEAP_YEAR
- );
+ ONE_MINUTE
+ ONE_HOUR
+ ONE_DAY
+ ONE_WEEK
+ ONE_MONTH
+ ONE_REAL_MONTH
+ ONE_YEAR
+ ONE_REAL_YEAR
+ ONE_FINANCIAL_MONTH
+ LEAP_YEAR
+ NON_LEAP_YEAR
+);
@EXPORT_OK = qw(cs_sec cs_mon);
@@ -41,10 +38,10 @@ use constant cs_mon => 1;
use overload
'fallback' => 'undef',
- '0+' => \&seconds,
- '""' => \&seconds,
- '<=>' => \&compare,
- '+' => \&add,
+ '0+' => \&seconds,
+ '""' => \&seconds,
+ '<=>' => \&compare,
+ '+' => \&add,
'-' => \&subtract,
'-=' => \&subtract_from,
'+=' => \&add_to,
@@ -150,6 +147,32 @@ sub years {
$s->days / 365.24225;
}
+sub pretty {
+ my $s = shift;
+ my $str = "";
+ if ($s < 0) {
+ $s = -$s;
+ $str = "minus ";
+ }
+ if ($s >= ONE_MINUTE) {
+ if ($s >= ONE_HOUR) {
+ if ($s >= ONE_DAY) {
+ my $days = sprintf("%d", $s->days); # does a "floor"
+ $str = $days . " days, ";
+ $s -= ($days * ONE_DAY);
+ }
+ my $hours = sprintf("%d", $s->hours);
+ $str .= $hours . " hours, ";
+ $s -= ($hours * ONE_HOUR);
+ }
+ my $mins = sprintf("%d", $s->minutes);
+ $str .= $mins . " minutes, ";
+ $s -= ($mins * ONE_MINUTE);
+ }
+ $str .= $s->seconds . " seconds";
+ return $str;
+}
+
1;
__END__
@@ -183,9 +206,9 @@ Time::Seconds also exports the following constants:
ONE_WEEK
ONE_HOUR
ONE_MINUTE
- ONE_MONTH
- ONE_YEAR
- ONE_FINANCIAL_MONTH
+ ONE_MONTH
+ ONE_YEAR
+ ONE_FINANCIAL_MONTH
LEAP_YEAR
NON_LEAP_YEAR
@@ -205,6 +228,9 @@ The following methods are available:
$val->months;
$val->financial_months; # 30 days
$val->years;
+ $val->pretty; # gives English representation of the delta
+
+The usual arithmetic (+,-,+=,-=) is also available on the objects.
The methods make the assumption that there are 24 hours in a day, 7 days in
a week, 365.24225 days in a year and 12 months in a year.