From fe8d5ba1d3922cd368691d47bb5f605b9e2bed58 Mon Sep 17 00:00:00 2001
From: Kevin Cernekee <cernekee@gmail.com>
Date: Wed, 23 Mar 2011 22:00:31 -0700
Subject: [PATCH] gitweb: minor fixups to javascript localtime feature

Fix whitespace, indentation, HH:MM:SS zero padding, IE6 compatibility.

Signed-off-by: Kevin Cernekee <cernekee@gmail.com>
---
 gitweb/gitweb.perl       |   11 ++++---
 gitweb/static/js/date.js |   63 +++++++++++++++++++++++++--------------------
 2 files changed, 41 insertions(+), 33 deletions(-)

diff --git a/gitweb/gitweb.perl b/gitweb/gitweb.perl
index dfceaf6..1d5e970 100755
--- a/gitweb/gitweb.perl
+++ b/gitweb/gitweb.perl
@@ -262,9 +262,9 @@ our %highlight_ext = (
 );
 
 
-# Enable / Disable the ability for gitweb to use a small amount of 
+# Enable / Disable the ability for gitweb to use a small amount of
 # javascript, along with a javascript cookie, to display commit
-# times in the preffered timezone.  Time zones can be pre-set 
+# times in the preferred timezone.  Time zones can be pre-set
 # by specifying it in $jslocaltime with one of the following:
 #
 #	utc
@@ -2956,6 +2956,7 @@ sub parse_date {
 	                     $mday, $months[$mon], $hour ,$min;
 	$date{'iso-8601'}  = sprintf "%04d-%02d-%02dT%02d:%02d:%02dZ",
 	                     1900+$year, 1+$mon, $mday, $hour ,$min, $sec;
+	$date{'getTime'}   = $epoch * 1000;
 
 	$tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
 	my $local = $epoch + ((int $1 + ($2/60)) * 3600);
@@ -4013,7 +4014,7 @@ sub git_print_authorship {
 	my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
 	print "<$tag class=\"author_date\">" .
 	      format_search_author($author, "author", esc_html($author)) .
-	      " [<span class=\"dtcommit\" title=\"$ad{'iso-8601'}\">$ad{'rfc2822'}</span>";
+	      " [<span class=\"dtcommit\" title=\"$ad{'getTime'}\">$ad{'rfc2822'}</span>";
 	print_local_time(%ad) if ($opts{-localtime});
 	print "]" . git_get_avatar($co->{'author_email'}, -pad_before => 1)
 		  . "</$tag>\n";
@@ -4040,7 +4041,7 @@ sub git_print_authorship_rows {
 		      git_get_avatar($co->{"${who}_email"}, -size => 'double') .
 		      "</td></tr>\n" .
 		      "<tr>" .
-		      "<td></td><td> <span class=\"dtcommit\" title=\"$wd{'iso-8601'}\">$wd{'rfc2822'}</span>";
+		      "<td></td><td> <span class=\"dtcommit\" title=\"$wd{'getTime'}\">$wd{'rfc2822'}</span>";
 		print_local_time(%wd);
 		print "</td>" .
 		      "</tr>\n";
@@ -5454,7 +5455,7 @@ sub git_summary {
 	      "<tr id=\"metadata_owner\"><td>owner</td><td>" . esc_html($owner) . "</td></tr>\n";
 	if (defined $cd{'rfc2822'}) {
 		print "<tr id=\"metadata_lchange\"><td>last change</td>\n";
-		print "<td><span class=\"dtcommit\" title=\"$cd{'iso-8601'}\">$cd{'rfc2822'}</span></td></tr>\n";
+		print "<td><span class=\"dtcommit\" title=\"$cd{'getTime'}\">$cd{'rfc2822'}</span></td></tr>\n";
 	}
 
 	# use per project git URL list in $projectroot/$project/cloneurl
diff --git a/gitweb/static/js/date.js b/gitweb/static/js/date.js
index a6d6f81..1728bda 100644
--- a/gitweb/static/js/date.js
+++ b/gitweb/static/js/date.js
@@ -12,15 +12,15 @@ function onloadTZSetup(){
 }
 
 function addChangeTZ() {
-		var txtClassesFound = "";
-		var classesFound = findElementsByClassName( "dtcommit" );
-		txtClassesFound += "Length: "+ classesFound.length +"<br>\n";
-		for ( x = 0; x < classesFound.length; x++){
-			curElement = classesFound[x];
-			txtClassesFound += "<br>\n"+ x +" - "+ curElement.nodeName  +" - "+ curElement.title +" - "+ curElement.innerHTML +"<br>\n";
-			var strExtra = " <span onclick=\"clickDate(event.target);\" title=\"+\">+</span>"
-			curElement.innerHTML = curElement.innerHTML + strExtra;
-		}
+	var txtClassesFound = "";
+	var classesFound = findElementsByClassName( "dtcommit" );
+	txtClassesFound += "Length: "+ classesFound.length +"<br>\n";
+	for ( x = 0; x < classesFound.length; x++){
+		curElement = classesFound[x];
+		txtClassesFound += "<br>\n"+ x +" - "+ curElement.nodeName  +" - "+ curElement.title +" - "+ curElement.innerHTML +"<br>\n";
+		var strExtra = " <span onclick=\"clickDate(event.target);\" title=\"+\">+</span>"
+		curElement.innerHTML = curElement.innerHTML + strExtra;
+	}
 }
 
 function checkTZCookie(){
@@ -35,18 +35,26 @@ function checkTZCookie(){
 }
 
 function formatTZ( tzOffset ) {
-		var posNeg = "+";
-		if( tzOffset < 0 ){
-			posNeg = "-";
-		}
-		tzOffset = Math.sqrt( Math.pow( tzOffset, 2 ) );
-		if( tzOffset < 100 ){
-			tzOffset = tzOffset * 100;
-		}
-		for( y = tzOffset.toString().length + 1; y <= 4; y++ ){
-			tzOffset = "0"+ tzOffset;
-		}
-		return posNeg + tzOffset;
+	var posNeg = "+";
+	if( tzOffset < 0 ){
+		posNeg = "-";
+	}
+	tzOffset = Math.sqrt( Math.pow( tzOffset, 2 ) );
+	if( tzOffset < 100 ){
+		tzOffset = tzOffset * 100;
+	}
+	for( y = tzOffset.toString().length + 1; y <= 4; y++ ){
+		tzOffset = "0"+ tzOffset;
+	}
+	return posNeg + tzOffset;
+}
+
+function padTime( val ) {
+	if( val < 10 ){
+		return "0" + val.toString();
+	} else {
+		return val.toString();
+	}
 }
 
 function dateOutput( objDate ) {
@@ -62,7 +70,7 @@ function dateOutputTZ( objDate, tzOffset ) {
 		tzOffset = 0;
 	}else if( tzOffset == "local" ){
 		var tempDate = new Date();
-		tzOffset = tempDate.getTimezoneOffset() * -1 / 60 * 100; 
+		tzOffset = tempDate.getTimezoneOffset() * -1 / 60 * 100;
 		tzOffset = formatTZ( tzOffset );
 	}
 
@@ -76,9 +84,9 @@ function dateOutputTZ( objDate, tzOffset ) {
 	strDate += toDateTime.getUTCDate() +" ";
 	strDate += monthsOfYr[ toDateTime.getUTCMonth() ] +" ";
 	strDate += toDateTime.getUTCFullYear() +" ";
-	strDate += toDateTime.getUTCHours() +":";
-	strDate += toDateTime.getUTCMinutes() +":";
-	strDate += toDateTime.getUTCSeconds() +" ";
+	strDate += padTime(toDateTime.getUTCHours()) +":";
+	strDate += padTime(toDateTime.getUTCMinutes()) +":";
+	strDate += padTime(toDateTime.getUTCSeconds()) +" ";
 
 	strDate += tzOffset;
 
@@ -98,7 +106,7 @@ function tzChangeSNS( tzOffset, set ){
 	var classesFound = findElementsByClassName( "dtcommit" );
 	for ( x = 0; x < classesFound.length; x++){
 		curElement = classesFound[x];
-		var origDateTime = new Date( curElement.title );
+		var origDateTime = new Date( parseInt(curElement.title) );
 		curElement.innerHTML = dateOutputTZ(origDateTime, tzOffset);
 	}
 	var tzExpDate = new Date();
@@ -114,7 +122,7 @@ function clickDate( clkEvent ) {
 		clkEvent.title="-";
 
 		var preSetTZ = getCookie( getwebCookieTZOffset );
-		
+
 		var arrSelected = new Array();
 		var offsetArr = 14;
 		arrSelected[0] = " ";
@@ -157,4 +165,3 @@ function clickDate( clkEvent ) {
 		clkEvent.parentNode.innerHTML = "+";
 	}
 }
-
-- 
1.7.4.1

