Skip to content

Commit

Permalink
Item11308: Add support for dates before 1970
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.foswiki.org/trunk@14619 0b4bb1d4-4e5a-0410-9cc4-b2b747904278
  • Loading branch information
GeorgeClark authored and GeorgeClark committed Apr 17, 2012
1 parent b642d6f commit 3227f48
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
19 changes: 14 additions & 5 deletions UnitTestContrib/test/unit/TimeTests.pm
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ our @ISA = qw( FoswikiTestCase );
use strict;
use Foswiki::Time;
require POSIX;
use Time::Local;
use Time::Local qw( timelocal timegm timelocal_nocheck timegm_nocheck);
use Config; #used to detect if this is strawberry perl

sub new {
Expand Down Expand Up @@ -71,11 +71,11 @@ sub showTime {

sub checkTime {
my ( $this, $s, $m, $h, $D, $M, $Y, $str, $dl ) = @_;
$Y -= 1900;
#$Y -= 1900;
$M--;

$Foswiki::cfg{DisplayTimeValues} = 'gmtime';
my $gmt = timegm( $s, $m, $h, $D, $M, $Y );
my $gmt = timegm_nocheck( $s, $m, $h, $D, $M, $Y );
my $tt = Foswiki::Time::parseTime( $str, $dl );
my $a = showTime($tt);
my $b = showTime($gmt);
Expand All @@ -85,8 +85,8 @@ sub checkTime {
$Foswiki::cfg{DisplayTimeValues} = 'servertime';
$gmt =
$str =~ /(?:Z|[-+]\d\d(?::\d\d)?)/
? timegm( $s, $m, $h, $D, $M, $Y )
: timelocal( $s, $m, $h, $D, $M, $Y );
? timegm_nocheck( $s, $m, $h, $D, $M, $Y )
: timelocal_nocheck( $s, $m, $h, $D, $M, $Y );
$tt = Foswiki::Time::parseTime( $str, $dl );
$a = showTime($tt);
$b = showTime($gmt);
Expand Down Expand Up @@ -116,6 +116,9 @@ sub test_parseTimeRCS {
$this->checkTime( 2, 1, 18, 2, 12, 2001, "2001-12-02 - 18:01:02" );
$this->checkTime( 2, 1, 18, 2, 12, 2001, "2001-12-02-18:01:02" );
$this->checkTime( 2, 1, 18, 2, 12, 2001, "2001-12-02.18:01:02" );
$this->checkTime( 2, 1, 18, 2, 12, 1902, "1902-12-02.18:01:02" );
$this->checkTime( 2, 1, 18, 2, 12, 1890, "1890-12-02.18:01:02" );
$this->checkTime( 7, 59, 6, 2, 7, 1730, "1730-07-02.06:59:07" );
}

sub test_parseTimeISO8601 {
Expand All @@ -130,6 +133,12 @@ sub test_parseTimeISO8601 {
$this->checkTime( 7, 59, 5, 2, 7, 1995, "1995-07-02T06:59:07+01:00" );
$this->checkTime( 7, 59, 5, 2, 7, 1995, "1995-07-02T06:59:07+01" );
$this->checkTime( 7, 59, 6, 2, 7, 1995, "1995-07-02T06:59:07Z" );
$this->checkTime( 7, 59, 6, 2, 7, 1902, "1902-07-02T06:59:07Z" );
$this->checkTime( 7, 59, 6, 2, 7, 1890, "1890-07-02T06:59:07Z" );
$this->checkTime( 7, 59, 6, 2, 7, 1730, "1730-07-02T06:59:07Z" );
$this->checkTime( 7, 59, 6, 2, 7, 10, "2010-07-02T06:59:07Z" );
$this->checkTime( 7, 59, 6, 2, 7, 99, "1999-07-02T06:59:07Z" );
$this->checkTime( 7, 59, 6, 2, 7, 29, "2029-07-02T06:59:07Z" );

if ( $^O eq 'MSWin32' ) {
$ENV{TZ} = 'GMT-1';
Expand Down
14 changes: 7 additions & 7 deletions core/lib/Foswiki/Time.pm
Original file line number Diff line number Diff line change
Expand Up @@ -118,20 +118,20 @@ sub parseTime {
$date =~ s/^\s*//; #remove leading spaces without de-tainting.
$date =~ s/\s*$//;

require Time::Local;
use Time::Local qw( timelocal timegm timelocal_nocheck timegm_nocheck);

# NOTE: This routine *will break* if input is not one of below formats!
my $timelocal =
$defaultLocal
? \&Time::Local::timelocal
: \&Time::Local::timegm;
? \&Time::Local::timelocal_nocheck
: \&Time::Local::timegm_nocheck;

# try "31 Dec 2001 - 23:59" (Foswiki date)
# or "31 Dec 2001"
#TODO: allow /.: too
if ( $date =~ /(\d+)[-\s]+([a-z]{3})[-\s]+(\d+)(?:[-\s]+(\d+):(\d+))?/i ) {
my $year = $3;
$year -= 1900 if ( $year > 1900 );
#$year -= 1900 if ( $year > 1900 );

my $mon = $MON2NUM{ lc($2) };
return undef unless defined $mon;
Expand All @@ -153,7 +153,7 @@ sub parseTime {
my ( $Y, $M, $D, $h, $m, $s, $tz ) =
( $1, $2 || 1, $3 || 1, $4 || 0, $5 || 0, $6 || 0, $7 || '' );
$M--;
$Y -= 1900 if ( $Y > 1900 );
#$Y -= 1900 if ( $Y > 1900 );
if ($tz) {
my $tzadj = 0;
if ( $tz eq 'Z' ) {
Expand All @@ -163,7 +163,7 @@ sub parseTime {
$tzadj = ( $1 || '' ) . ( ( ( $2 * 60 ) + ( $3 || 0 ) ) * 60 );
$tzadj -= 0;
}
return Time::Local::timegm( $s, $m, $h, $D, $M, $Y ) - $tzadj;
return Time::Local::timegm_nocheck( $s, $m, $h, $D, $M, $Y ) - $tzadj;
}
return &$timelocal( $s, $m, $h, $D, $M, $Y );
}
Expand Down Expand Up @@ -200,7 +200,7 @@ sub parseTime {
#$month_p = $MON2NUM{ lc($month_p) } if (defined($MON2NUM{ lc($month_p) }));

#TODO: unhappily, this means 09 == 1909 not 2009
$year -= 1900 if ( $year > 1900 );
#$year -= 1900 if ( $year > 1900 );

#range checks
return undef if ( defined($M) && ( $M < 1 || $M > 12 ) );
Expand Down

0 comments on commit 3227f48

Please sign in to comment.