Subject: [PATCH (BUGFIX) v2] gitweb: Fix handling of fractional timezones in parse_date

From: Jakub Narebski <[email-masked]>
Date: Fri, 25 Mar 2011 17:50:54 +0100

On Fri, 25 Mar 2011, Kevin Cernekee wrote:
> 2011/3/25 Jakub Narebski <[email-masked]>:
> > @@ -2921,8 +2921,10 @@ sub parse_date {
> >        $date{'iso-8601'}  = sprintf "%04d-%02d-%02dT%02d:%02d:%02dZ",
> >                             1900+$year, 1+$mon, $mday, $hour ,$min, $sec;
> >
> > -       $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
> > -       my $local = $epoch + ((int $1 + ($2/60)) * 3600);
> > +       my ($tz_sign, $tz_hour, $tz_min) =
> > +               ($tz =~ m/^([+\-])([0-9][0-9])([0-9][0-9])$/);
> 
> It's just a matter of personal preference, but I would find this
> regexp slightly easier to read:
> 
> +               ($tz =~ m/^([+\-])([0-9]{2})([0-9]{2})$/);

I went for minimal changes, same as with the change below.

> > +       $tz_sign = ($tz_sign eq '-' ? -1 : +1);
> > +       my $local = $epoch + $tz_sign*($tz_hour + ($tz_min/60.0))*3600;
> 
> If you wanted to avoid floats, you could do something like:
> 
> +       my $local = $epoch + $tz_sign * ($tz_hour * 3600 + $tz_min * 60);

Note that because valid $tz_min can be only 00, 30 or 45, see 
e.g. http://en.wikipedia.org/wiki/List_of_time_zones_by_UTC_offset 
therefore version using floats would not introduce any rounding
errors: 0, 0.5 and 0.75 can be represented exactly as 2-base float.

Anyway below is patch with above changes:

-- >8 --
Subject: [PATCH] gitweb: Fix handling of fractional timezones in parse_date

Fractional timezones, like -0330 (NST used in Canada) or +0430
(Afghanistan, Iran DST), were not handled properly in parse_date; this
means values such as 'minute_local' and 'iso-tz' were not generated
correctly.

This was caused by two mistakes:

* sign of timezone was applied only to hour part of offset, and not
  as it should be also to minutes part (this affected only negative
  fractional timezones).

* 'int $h + $m/60' is 'int($h + $m/60)' and not 'int($h) + $m/60',
  so fractional part was discarded altogether ($h is hours, $m is
  minutes, which is always less than 60).

Note that positive fractional timezones +0430, +0530 and +1030 can be
found as authortime in git.git repository itself.

For example http://repo.or.cz/w/git.git/commit/88d50e7 had authortime
of "Fri, 8 Jan 2010 18:48:07 +0000 (23:48 +0530)", which is not marked
with 'atnight', when "git show 88d50e7" gives correct author date of
"Sat Jan 9 00:18:07 2010 +0530".

Signed-off-by: Jakub Narebski <[email-masked]>
---
 gitweb/gitweb.perl |    6 ++++--
 1 files changed, 4 insertions(+), 2 deletions(-)

diff --git a/gitweb/gitweb.perl b/gitweb/gitweb.perl
index 0178633..7b9f90b 100755
--- a/gitweb/gitweb.perl
+++ b/gitweb/gitweb.perl
@@ -2921,8 +2921,10 @@ sub parse_date {
 	$date{'iso-8601'}  = sprintf "%04d-%02d-%02dT%02d:%02d:%02dZ",
 	                     1900+$year, 1+$mon, $mday, $hour ,$min, $sec;
 
-	$tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
-	my $local = $epoch + ((int $1 + ($2/60)) * 3600);
+	my ($tz_sign, $tz_hour, $tz_min) =
+		($tz =~ m/^([+\-])([0-9]{2})([0-9]{2})$/);
+	$tz_sign = ($tz_sign eq '-' ? -1 : +1);
+	my $local = $epoch + $tz_sign*($tz_hour*3600 + $tz_min*60);
 	($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
 	$date{'hour_local'} = $hour;
 	$date{'minute_local'} = $min;
-- 
1.7.3