In perl.git, the branch blead has been updated <http://perl5.git.perl.org/perl.git/commitdiff/90d55c29481047f2784ec1daadd553c5710e090a?hp=1db4d19556a36b5a8e8604c1e7656999ebc7732b>
- Log ----------------------------------------------------------------- commit 90d55c29481047f2784ec1daadd553c5710e090a Author: Chris 'BinGOs' Williams <[email protected]> Date: Sat Jun 26 15:04:03 2010 +0100 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. ----------------------------------------------------------------------- Summary of changes: Porting/Maintainers.pl | 2 +- cpan/Time-Piece/Changes | 32 +++++++++- cpan/Time-Piece/Makefile.PL | 1 + cpan/Time-Piece/Piece.pm | 39 ++++++++--- cpan/Time-Piece/Piece.xs | 146 ++++++++++++++++++++++++++++++++++++------ cpan/Time-Piece/Seconds.pm | 70 ++++++++++++++------- cpan/Time-Piece/t/02core.t | 1 + cpan/Time-Piece/t/07arith.t | 23 +++++++- 8 files changed, 257 insertions(+), 57 deletions(-) diff --git a/Porting/Maintainers.pl b/Porting/Maintainers.pl index 9f38b97..e9657c0 100755 --- a/Porting/Maintainers.pl +++ b/Porting/Maintainers.pl @@ -1481,7 +1481,7 @@ use File::Glob qw(:case); 'Time::Piece' => { 'MAINTAINER' => 'msergeant', - 'DISTRIBUTION' => 'MSERGEANT/Time-Piece-1.15.tar.gz', + 'DISTRIBUTION' => 'MSERGEANT/Time-Piece-1.20.tar.gz', 'FILES' => q[cpan/Time-Piece], 'UPSTREAM' => undef, }, diff --git a/cpan/Time-Piece/Changes b/cpan/Time-Piece/Changes index 5eeb54b..5342dcb 100644 --- a/cpan/Time-Piece/Changes +++ b/cpan/Time-Piece/Changes @@ -1,6 +1,36 @@ - Time::Piece Changes +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. + 1.15 - Skip a test on Win32 that there's just no way of passing - Document the above failure diff --git a/cpan/Time-Piece/Makefile.PL b/cpan/Time-Piece/Makefile.PL index a69cf55..9b2a964 100644 --- a/cpan/Time-Piece/Makefile.PL +++ b/cpan/Time-Piece/Makefile.PL @@ -7,4 +7,5 @@ WriteMakefile( 'VERSION_FROM' => 'Piece.pm', # finds $VERSION 'AUTHOR' => 'Matt Sergeant', 'ABSTRACT_FROM' => 'Piece.pm', + 'INSTALLDIRS' => ( $] >= 5.009005 ? 'perl' : 'site' ), ); diff --git a/cpan/Time-Piece/Piece.pm b/cpan/Time-Piece/Piece.pm index a7a602f..2afbdba 100644 --- a/cpan/Time-Piece/Piece.pm +++ b/cpan/Time-Piece/Piece.pm @@ -1,5 +1,3 @@ -# $Id: Piece.pm 82 2009-06-27 13:20:23Z matt $ - package Time::Piece; use strict; @@ -9,7 +7,6 @@ require DynaLoader; use Time::Seconds; use Carp; use Time::Local; -#use UNIVERSAL qw(isa); # Commented out for Perl 5.12.0 by JRV to avoid a deprecation warning our @ISA = qw(Exporter DynaLoader); @@ -22,7 +19,7 @@ our %EXPORT_TAGS = ( ':override' => 'internal', ); -our $VERSION = '1.15_01'; +our $VERSION = '1.20'; bootstrap Time::Piece $VERSION; @@ -78,7 +75,7 @@ sub new { $self = $class->localtime(); } - return bless $self, $class; + return bless $self, ref($class) || $class; } sub parse { @@ -102,7 +99,7 @@ sub _mktime { : $class; if (ref($time)) { $time->[c_epoch] = undef; - return wantarray ? @$time : bless [...@$time, $islocal], $class; + return wantarray ? @$time : bless [...@$time[0..9], $islocal], $class; } _tzset(); my @time = $islocal ? @@ -297,7 +294,9 @@ sub tzoffset { # Compute floating offset in hours. # - my $delta = 24 * (&$j(CORE::localtime $epoch) - &$j(CORE::gmtime $epoch)); + # Note use of crt methods so the tz is properly set... + # See: http://perlmonks.org/?node_id=820347 + my $delta = 24 * ($j->(_crt_localtime($epoch)) - $j->(_crt_gmtime($epoch))); # Return value in seconds rounded to nearest minute. return Time::Seconds->new( int($delta * 60 + ($delta >= 0 ? 0.5 : -0.5)) * 60 ); @@ -601,7 +600,12 @@ sub add_months { if ($final_month > 11 || $final_month < 0) { # these two ops required because we have no POSIX::floor and don't # want to load POSIX.pm - $num_years = int($final_month / 12); + if ($final_month < 0 && $final_month % 12 == 0) { + $num_years = int($final_month / 12) + 1; + } + else { + $num_years = int($final_month / 12); + } $num_years-- if ($final_month < 0); $final_month = $final_month % 12; @@ -609,7 +613,7 @@ sub add_months { my @vals = _mini_mktime($time->sec, $time->min, $time->hour, $time->mday, $final_month, $time->year - 1900 + $num_years); -# warn(sprintf("got vals: %d-%d-%d %d:%d:%d\n", reverse(@vals))); + # warn(sprintf("got %d vals: %d-%d-%d %d:%d:%d [%d]\n", scalar(@vals), reverse(@vals), $time->[c_islocal])); return scalar $time->_mktime(\...@vals, $time->[c_islocal]); } @@ -780,10 +784,10 @@ Date comparisons are also possible, using the full suite of "<", ">", =head2 Date Parsing -Time::Piece links to your C library's strptime() function, allowing +Time::Piece has a built-in strptime() function (from FreeBSD), allowing you incredibly flexible date parsing routines. For example: - my $t = Time::Piece->strptime("Sun 3rd Nov, 1943", + my $t = Time::Piece->strptime("Sunday 3rd Nov, 1943", "%A %drd %b, %Y"); print $t->strftime("%a, %d %b %Y"); @@ -797,6 +801,8 @@ Outputs: For more information see "man strptime", which should be on all unix systems. +Alternatively look here: http://www.unix.com/man-page/FreeBSD/3/strftime/ + =head2 YYYY-MM-DDThh:mm:ss The ISO 8601 standard defines the date format to be YYYY-MM-DD, and @@ -838,6 +844,17 @@ also call _tzset() in the main thread to register the environment change). Furthermore, remember that this caveat also applies to fork(), which is emulated by threads on Win32. +=head2 Use of epoch seconds + +This module internally uses the epoch seconds system that is provided via +the perl C<time()> function and supported by C<gmtime()> and C<localtime()>. + +If your perl does not support times larger than C<2^31> seconds then this +module is likely to fail at processing dates beyond the year 2038. There are +moves afoot to fix that in perl. Alternatively use 64 bit perl. Or if none +of those are options, use the L<DateTime> module which has support for years +well into the future and past. + =head1 AUTHOR Matt Sergeant, [email protected] diff --git a/cpan/Time-Piece/Piece.xs b/cpan/Time-Piece/Piece.xs index 772ed9c..0bbefd3 100644 --- a/cpan/Time-Piece/Piece.xs +++ b/cpan/Time-Piece/Piece.xs @@ -303,16 +303,9 @@ my_mini_mktime(struct tm *ptm) ptm->tm_wday = (jday + WEEKDAY_BIAS) % 7; } -#ifndef HAS_STRPTIME - /* Assume everyone has strptime except Win32 and QNX4 */ -# define HAS_STRPTIME 1 # if defined(WIN32) || (defined(__QNX__) && defined(__WATCOMC__)) -# undef HAS_STRPTIME +# define strncasecmp(x,y,n) strnicmp(x,y,n) # endif -#endif - -#ifndef HAS_STRPTIME -#define strncasecmp(x,y,n) strnicmp(x,y,n) #if defined(WIN32) #if defined(__BORLANDC__) @@ -320,6 +313,11 @@ void * __cdecl _EXPFUNC alloca(_SIZE_T __size); #else #define alloca _alloca #endif +#else +#if defined(_SGIAPI) || defined( __sgi ) || ( defined (__SVR4) && defined (__sun) ) +/* required for IRIX and Solaris */ +#include <alloca.h> +#endif #endif /* strptime copied from freebsd with the following copyright: */ @@ -469,13 +467,17 @@ _strptime(pTHX_ const char *buf, const char *fmt, struct tm *tm) len; int Ealternative, Oalternative; + /* There seems to be a slightly improved version at + * http://www.opensource.apple.com/source/Libc/Libc-583/stdtime/strptime-fbsd.c + * which we may end up borrowing more from + */ ptr = fmt; while (*ptr != 0) { if (*buf == 0) break; c = *ptr++; - + if (c != '%') { if (isspace((unsigned char)c)) while (*buf != 0 && isspace((unsigned char)*buf)) @@ -565,6 +567,14 @@ label: return 0; break; + case 'n': /* whitespace */ + case 't': + if (!isspace((unsigned char)*buf)) + return 0; + while (isspace((unsigned char)*buf)) + buf++; + break; + case 'T': buf = _strptime(aTHX_ buf, "%H:%M:%S", tm); if (buf == 0) @@ -641,7 +651,7 @@ label: * XXX The %l specifier may gobble one too many * digits if used incorrectly. */ - if (!isdigit((unsigned char)*buf)) + if (!isdigit((unsigned char)*buf)) return 0; len = 2; @@ -668,7 +678,7 @@ label: * XXX This is bogus if parsed before hour-related * specifiers. */ - len = strlen(Locale->am); + len = strlen(Locale->am); if (strncasecmp(buf, Locale->am, len) == 0) { if (tm->tm_hour > 12) return 0; @@ -722,7 +732,7 @@ label: * point to calculate a real value, so just check the * range for now. */ - if (!isdigit((unsigned char)*buf)) + if (!isdigit((unsigned char)*buf)) return 0; len = 2; @@ -838,6 +848,38 @@ label: ptr++; break; + case 's': + { + char *cp; + int sverrno; + long n; + time_t t; + struct tm mytm; + + sverrno = errno; + errno = 0; + n = strtol(buf, &cp, 10); + if (errno == ERANGE || (long)(t = n) != n) { + errno = sverrno; + return 0; + } + errno = sverrno; + buf = cp; + memset(&mytm, 0, sizeof(mytm)); + my_init_tm(&mytm); /* XXX workaround - see my_init_tm() above */ + mytm = *gmtime(&t); + tm->tm_sec = mytm.tm_sec; + tm->tm_min = mytm.tm_min; + tm->tm_hour = mytm.tm_hour; + tm->tm_mday = mytm.tm_mday; + tm->tm_mon = mytm.tm_mon; + tm->tm_year = mytm.tm_year; + tm->tm_wday = mytm.tm_wday; + tm->tm_yday = mytm.tm_yday; + tm->tm_isdst = mytm.tm_isdst; + } + break; + case 'Y': case 'y': if (*buf == 0 || isspace((unsigned char)*buf)) @@ -887,6 +929,34 @@ label: } } break; + + case 'z': + { + int sign = 1; + + if (*buf != '+') { + if (*buf == '-') + sign = -1; + else + return 0; + } + + buf++; + i = 0; + for (len = 4; len > 0; len--) { + if (isdigit((int)*buf)) { + i *= 10; + i += *buf - '0'; + buf++; + } else + return 0; + } + + tm->tm_hour -= sign * (i / 100); + tm->tm_min -= sign * (i % 100); + got_GMT = 1; + } + break; } } return (char *)buf; @@ -894,7 +964,7 @@ label: char * -strptime(pTHX_ const char *buf, const char *fmt, struct tm *tm) +our_strptime(pTHX_ const char *buf, const char *fmt, struct tm *tm) { char *ret; @@ -912,8 +982,6 @@ pthread_mutex_lock(&gotgmt_mutex); return ret; } -#endif /* !HAS_STRPTIME */ - MODULE = Time::Piece PACKAGE = Time::Piece PROTOTYPES: ENABLE @@ -1011,13 +1079,9 @@ _strptime ( string, format ) PPCODE: t = 0; mytm = *gmtime(&t); -#ifdef HAS_STRPTIME - remainder = (char *)strptime(string, format, &mytm); -#else - remainder = (char *)strptime(aTHX_ string, format, &mytm); -#endif + remainder = (char *)our_strptime(aTHX_ string, format, &mytm); if (remainder == NULL) { - croak("Error parsing time"); + croak("Error parsing time"); } if (*remainder != '\0') { warn("garbage at end of string in strptime: %s", remainder); @@ -1076,3 +1140,43 @@ _mini_mktime(int sec, int min, int hour, int mday, int mon, int year) PUSHs(sv_2mortal(newSViv(0))); /* islocal */ PUSHs(sv_2mortal(newSViv(0))); + +void +_crt_localtime(time_t sec) + PREINIT: + struct tm mytm; + PPCODE: + mytm = *localtime(&sec); + /* Need to get: $s,$n,$h,$d,$m,$y */ + + EXTEND(SP, 9); + PUSHs(sv_2mortal(newSViv(mytm.tm_sec))); + PUSHs(sv_2mortal(newSViv(mytm.tm_min))); + PUSHs(sv_2mortal(newSViv(mytm.tm_hour))); + PUSHs(sv_2mortal(newSViv(mytm.tm_mday))); + PUSHs(sv_2mortal(newSViv(mytm.tm_mon))); + PUSHs(sv_2mortal(newSViv(mytm.tm_year))); + PUSHs(sv_2mortal(newSViv(mytm.tm_year))); + PUSHs(sv_2mortal(newSViv(mytm.tm_wday))); + PUSHs(sv_2mortal(newSViv(mytm.tm_yday))); + PUSHs(sv_2mortal(newSViv(mytm.tm_isdst))); + +void +_crt_gmtime(time_t sec) + PREINIT: + struct tm mytm; + PPCODE: + mytm = *gmtime(&sec); + /* Need to get: $s,$n,$h,$d,$m,$y */ + + EXTEND(SP, 9); + PUSHs(sv_2mortal(newSViv(mytm.tm_sec))); + PUSHs(sv_2mortal(newSViv(mytm.tm_min))); + PUSHs(sv_2mortal(newSViv(mytm.tm_hour))); + PUSHs(sv_2mortal(newSViv(mytm.tm_mday))); + PUSHs(sv_2mortal(newSViv(mytm.tm_mon))); + PUSHs(sv_2mortal(newSViv(mytm.tm_year))); + PUSHs(sv_2mortal(newSViv(mytm.tm_wday))); + PUSHs(sv_2mortal(newSViv(mytm.tm_yday))); + PUSHs(sv_2mortal(newSViv(mytm.tm_isdst))); + diff --git a/cpan/Time-Piece/Seconds.pm b/cpan/Time-Piece/Seconds.pm index 4aac988..1ecefa1 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. diff --git a/cpan/Time-Piece/t/02core.t b/cpan/Time-Piece/t/02core.t index 5610bcb..8a0673e 100644 --- a/cpan/Time-Piece/t/02core.t +++ b/cpan/Time-Piece/t/02core.t @@ -219,3 +219,4 @@ cmp_ok( '==', 951827696 ); + diff --git a/cpan/Time-Piece/t/07arith.t b/cpan/Time-Piece/t/07arith.t index c4836a9..7f16bd1 100644 --- a/cpan/Time-Piece/t/07arith.t +++ b/cpan/Time-Piece/t/07arith.t @@ -1,4 +1,4 @@ -use Test::More tests => 13; +use Test::More tests => 25; BEGIN { use_ok('Time::Piece'); } @@ -24,3 +24,24 @@ my $t4 = $t->add_years(1); is($t4->year, 2001); is($t4->mon, 3); is($t4->mday, 1); + +$t = Time::Piece->strptime("01 01 2010","%d %m %Y"); +my $t6 = $t->add_months(-12); +is($t6->year, 2009); +is($t6->mon, 1); +is($t6->mday, 1); + +my $t7 = $t->add_months(-1); +is($t7->year, 2009); +is($t7->mon, 12); +is($t7->mday, 1); + +my $t8 = $t->add_months(-240); +is($t8->year, 1990); +is($t8->mon, 1); +is($t8->mday, 1); + +my $t9 = $t->add_months(-13); +is($t9->year, 2008); +is($t9->mon, 12); +is($t9->mday, 1); -- Perl5 Master Repository
