Skip to content

Commit a40e06e

Browse files
bmwiedemanngitster
authored andcommitted
perl: call timegm and timelocal with 4-digit year
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>
1 parent ffa9524 commit a40e06e

File tree

4 files changed

+8
-4
lines changed

4 files changed

+8
-4
lines changed

contrib/examples/git-svnimport.perl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ ($)
238238
my($d) = @_;
239239
$d =~ m#(\d\d\d\d)-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)#
240240
or die "Unparseable date: $d\n";
241-
my $y=$1; $y-=1900 if $y>1900;
241+
my $y=$1; $y+=1900 if $y<1000;
242242
return timegm($6||0,$5,$4,$3,$2-1,$y);
243243
}
244244

git-cvsimport.perl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,9 @@ ($)
601601
my ($d) = @_;
602602
m#(\d{2,4})/(\d\d)/(\d\d)\s(\d\d):(\d\d)(?::(\d\d))?#
603603
or die "Unparseable date: $d\n";
604-
my $y=$1; $y-=1900 if $y>1900;
604+
my $y=$1;
605+
$y+=100 if $y<70;
606+
$y+=1900 if $y<1000;
605607
return timegm($6||0,$5,$4,$3,$2-1,$y);
606608
}
607609

perl/Git.pm

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,9 @@ If TIME is not supplied, the current local time is used.
534534
sub get_tz_offset {
535535
# some systems don't handle or mishandle %z, so be creative.
536536
my $t = shift || time;
537-
my $gm = timegm(localtime($t));
537+
my @t = localtime($t);
538+
$t[5] += 1900;
539+
my $gm = timegm(@t);
538540
my $sign = qw( + + - )[ $gm <=> $t ];
539541
return sprintf("%s%02d%02d", $sign, (gmtime(abs($t - $gm)))[2,1]);
540542
}

perl/Git/SVN.pm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1405,7 +1405,7 @@ sub parse_svn_date {
14051405
$ENV{TZ} = 'UTC';
14061406

14071407
my $epoch_in_UTC =
1408-
Time::Local::timelocal($S, $M, $H, $d, $m - 1, $Y - 1900);
1408+
Time::Local::timelocal($S, $M, $H, $d, $m - 1, $Y);
14091409

14101410
# Determine our local timezone (including DST) at the
14111411
# time of $epoch_in_UTC. $Git::SVN::Log::TZ stored the

0 commit comments

Comments
 (0)