perl: call timegm and timelocal with 4-digit year
authorBernhard M. Wiedemann <bwiedemann@suse.de>
Fri, 23 Feb 2018 17:20:45 +0000 (18:20 +0100)
committerJunio C Hamano <gitster@pobox.com>
Fri, 23 Feb 2018 22:47:06 +0000 (14:47 -0800)
Amazingly, timegm(gmtime(0)) is only 0 before 2020 because perl's
timegm deviates from GNU timegm(3) in how it handles years.

man Time::Local says

Whenever possible, use an absolute four digit year instead.

with a detailed explanation about ambiguity of 2-digit years above that.

Even though this ambiguity is error-prone with >50% of users getting it
wrong, it has been like this for 20+ years, so we just use 4-digit years
everywhere to be on the safe side.

We add some extra logic to cvsimport because it allows 2-digit year
input and interpreting an 18 as 1918 can be avoided easily and safely.

Signed-off-by: Bernhard M. Wiedemann <bwiedemann@suse.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
contrib/examples/git-svnimport.perl
git-cvsimport.perl
perl/Git.pm
perl/Git/SVN.pm
index c414f0d9c7ecfac7074d0e052c19f73cae4344d1..75a43e23b6138754e7dc780aa3a643be38e1ba14 100755 (executable)
@@ -238,7 +238,7 @@ ($)
        my($d) = @_;
        $d =~ m#(\d\d\d\d)-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)#
                or die "Unparseable date: $d\n";
-       my $y=$1; $y-=1900 if $y>1900;
+       my $y=$1; $y+=1900 if $y<1000;
        return timegm($6||0,$5,$4,$3,$2-1,$y);
 }
 
index 2d8df831722913093a46e9325796b85f67a97073..b31613cb8aa8decd3f808d5d29e047243fa1eac6 100755 (executable)
@@ -601,7 +601,9 @@ ($)
        my ($d) = @_;
        m#(\d{2,4})/(\d\d)/(\d\d)\s(\d\d):(\d\d)(?::(\d\d))?#
                or die "Unparseable date: $d\n";
-       my $y=$1; $y-=1900 if $y>1900;
+       my $y=$1;
+       $y+=100 if $y<70;
+       $y+=1900 if $y<1000;
        return timegm($6||0,$5,$4,$3,$2-1,$y);
 }
 
index ffa09ace924e0a7b079d039e905363435b08cf9b..df62518c710cd608487ccbcdd00067ba377accff 100644 (file)
@@ -534,7 +534,9 @@ sub version {
 sub get_tz_offset {
        # some systems don't handle or mishandle %z, so be creative.
        my $t = shift || time;
-       my $gm = timegm(localtime($t));
+       my @t = localtime($t);
+       $t[5] += 1900;
+       my $gm = timegm(@t);
        my $sign = qw( + + - )[ $gm <=> $t ];
        return sprintf("%s%02d%02d", $sign, (gmtime(abs($t - $gm)))[2,1]);
 }
index bc4eed3d75461444f8af0e27e2930ccb25663312..991a5885e9230b1f55bd6f3b7f7b53321bf9e562 100644 (file)
@@ -1405,7 +1405,7 @@ sub parse_svn_date {
                $ENV{TZ} = 'UTC';
 
                my $epoch_in_UTC =
-                   Time::Local::timelocal($S, $M, $H, $d, $m - 1, $Y - 1900);
+                   Time::Local::timelocal($S, $M, $H, $d, $m - 1, $Y);
 
                # Determine our local timezone (including DST) at the
                # time of $epoch_in_UTC.  $Git::SVN::Log::TZ stored the