From 6c6e7d393119dc31a8a6c407236af10290abc77e Mon Sep 17 00:00:00 2001
From: James Moger <james.moger@gitblit.com>
Date: Tue, 21 Feb 2012 17:32:04 -0500
Subject: [PATCH] Allow specifying a timezone in gitblit.properties/web.xml (issue 54)

---
 src/com/gitblit/wicket/panels/BasePanel.java |    2 
 src/com/gitblit/GitBlit.java                 |   37 +++++++++++++++++-
 src/com/gitblit/wicket/WicketUtils.java      |   22 ++++++-----
 docs/04_releases.mkd                         |    9 +++-
 src/com/gitblit/wicket/pages/BasePage.java   |    2 
 distrib/gitblit.properties                   |    9 ++++
 6 files changed, 64 insertions(+), 17 deletions(-)

diff --git a/distrib/gitblit.properties b/distrib/gitblit.properties
index 27b0d1f..2ac6598 100644
--- a/distrib/gitblit.properties
+++ b/distrib/gitblit.properties
@@ -232,6 +232,15 @@
 # SINCE 0.5.0
 web.repositoriesMessage = gitblit
 
+# Manually set the default timezone to be used by Gitblit for display in the 
+# web ui.  This value is independent of the JVM timezone.  Specifying a blank
+# value will default to the JVM timezone.
+# e.g. America/New_York, US/Pacific, UTC, Europe/Berlin
+#
+# SINCE 0.9.0
+# RESTART REQUIRED
+web.timezone =
+
 # Use the client timezone when formatting dates.
 # This uses AJAX to determine the browser's timezone and may require more
 # server overhead because a Wicket session is created.  All Gitblit pages
diff --git a/docs/04_releases.mkd b/docs/04_releases.mkd
index f675113..21b96d2 100644
--- a/docs/04_releases.mkd
+++ b/docs/04_releases.mkd
@@ -10,12 +10,14 @@
 
 #### changes
 
-- block pushes to a repository with a working copy (i.e. non-bare repository) (issue-49)
-- web.datetimestampLongFormat from *EEEE, MMMM d, yyyy h:mm a z* to *EEEE, MMMM d, yyyy HH:mm Z* (issue 50)
-- expanded commit age coloring from 2 days to 30 days (issue 57)
+- Block pushes to a repository with a working copy (i.e. non-bare repository) (issue-49)
+- Changed default web.datetimestampLongFormat from *EEEE, MMMM d, yyyy h:mm a z* to *EEEE, MMMM d, yyyy HH:mm Z* (issue 50)
+- Expanded commit age coloring from 2 days to 30 days (issue 57)
 
 #### additions
 
+- Allow specifying timezone to use for Gitblit which is independent of both the JVM and the system timezone (issue 54)  
+    **New:** *web.timezone =*  
 - Added a built-in AJP connector for integrating Gitblit GO into an Apache mod_proxy setup (issue 59)  
     **New:** *server.ajpPort = 0*  
     **New:** *server.ajpBindInterface = localhost*
@@ -28,6 +30,7 @@
 
 #### fixes 
 
+- Fixed timezone bug on the activity apge (issue 54)
 - Prevent add/edit team with no selected repositories (issue 56) 
 - Disallow browser autocomplete on add/edit user/team/repository pages
 - Fixed username case-sensitivity issues (issue 43)
diff --git a/src/com/gitblit/GitBlit.java b/src/com/gitblit/GitBlit.java
index 7cb813f..9c1cd40 100644
--- a/src/com/gitblit/GitBlit.java
+++ b/src/com/gitblit/GitBlit.java
@@ -23,16 +23,19 @@
 import java.io.InputStreamReader;
 import java.lang.reflect.Field;
 import java.text.MessageFormat;
+import java.text.SimpleDateFormat;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
+import java.util.Date;
 import java.util.HashMap;
 import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Set;
+import java.util.TimeZone;
 import java.util.TreeSet;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.Executors;
@@ -103,7 +106,7 @@
 public class GitBlit implements ServletContextListener {
 
 	private static GitBlit gitblit;
-
+	
 	private final Logger logger = LoggerFactory.getLogger(GitBlit.class);
 
 	private final ScheduledExecutorService scheduledExecutor = Executors.newScheduledThreadPool(5);
@@ -132,6 +135,8 @@
 	private ServerStatus serverStatus;
 
 	private MailExecutor mailExecutor;
+	
+	private TimeZone timezone;
 
 	public GitBlit() {
 		if (gitblit == null) {
@@ -159,6 +164,23 @@
 	 */
 	public static boolean isGO() {
 		return self().settings instanceof FileSettings;
+	}
+	
+	/**
+	 * Returns the preferred timezone for the Gitblit instance.
+	 * 
+	 * @return a timezone
+	 */
+	public static TimeZone getTimezone() {
+		if (self().timezone == null) {
+			String tzid = getString("web.timezone", null);
+			if (StringUtils.isEmpty(tzid)) {
+				self().timezone = TimeZone.getDefault();
+				return self().timezone;
+			}
+			self().timezone = TimeZone.getTimeZone(tzid);
+		}
+		return self().timezone;
 	}
 
 	/**
@@ -1767,6 +1789,10 @@
 		repositoriesFolder = getRepositoriesFolder();
 		logger.info("Git repositories folder " + repositoriesFolder.getAbsolutePath());
 		repositoryResolver = new FileResolver<Void>(repositoriesFolder, true);
+		
+		logTimezone("JVM", TimeZone.getDefault());
+		logTimezone(Constants.NAME, getTimezone());
+
 		serverStatus = new ServerStatus(isGO());
 		String realm = settings.getString(Keys.realm.userService, "users.properties");
 		IUserService loginService = null;
@@ -1786,7 +1812,14 @@
 		}
 		if (startFederation) {
 			configureFederation();
-		}
+		}		
+	}
+	
+	private void logTimezone(String type, TimeZone zone) {
+		SimpleDateFormat df = new SimpleDateFormat("z Z");
+		df.setTimeZone(zone);
+		String offset = df.format(new Date());
+		logger.info(type + " timezone is " + zone.getID() + " (" + offset + ")");
 	}
 
 	/**
diff --git a/src/com/gitblit/wicket/WicketUtils.java b/src/com/gitblit/wicket/WicketUtils.java
index 8c1cf3c..7be5328 100644
--- a/src/com/gitblit/wicket/WicketUtils.java
+++ b/src/com/gitblit/wicket/WicketUtils.java
@@ -408,9 +408,10 @@
 	public static Label createDateLabel(String wicketId, Date date, TimeZone timeZone) {
 		String format = GitBlit.getString(Keys.web.datestampShortFormat, "MM/dd/yy");
 		DateFormat df = new SimpleDateFormat(format);
-		if (timeZone != null) {
-			df.setTimeZone(timeZone);
+		if (timeZone == null) {
+			timeZone = GitBlit.getTimezone();
 		}
+		df.setTimeZone(timeZone);
 		String dateString;
 		if (date.getTime() == 0) {
 			dateString = "--";
@@ -438,9 +439,10 @@
 	public static Label createTimeLabel(String wicketId, Date date, TimeZone timeZone) {
 		String format = GitBlit.getString(Keys.web.timeFormat, "HH:mm");
 		DateFormat df = new SimpleDateFormat(format);
-		if (timeZone != null) {
-			df.setTimeZone(timeZone);
+		if (timeZone == null) {
+			timeZone = GitBlit.getTimezone();
 		}
+		df.setTimeZone(timeZone);
 		String timeString;
 		if (date.getTime() == 0) {
 			timeString = "--";
@@ -449,7 +451,6 @@
 		}
 		String title = TimeUtils.timeAgo(date);
 		Label label = new Label(wicketId, timeString);
-		WicketUtils.setCssClass(label, TimeUtils.timeAgoCss(date));
 		if (!StringUtils.isEmpty(title)) {
 			WicketUtils.setHtmlTooltip(label, title);
 		}
@@ -459,9 +460,10 @@
 	public static Label createDatestampLabel(String wicketId, Date date, TimeZone timeZone) {
 		String format = GitBlit.getString(Keys.web.datestampLongFormat, "EEEE, MMMM d, yyyy");
 		DateFormat df = new SimpleDateFormat(format);
-		if (timeZone != null) {
-			df.setTimeZone(timeZone);
+		if (timeZone == null) {
+			timeZone = GitBlit.getTimezone();
 		}
+		df.setTimeZone(timeZone);
 		String dateString;
 		if (date.getTime() == 0) {
 			dateString = "--";
@@ -483,7 +485,6 @@
 			title = tmp;
 		}
 		Label label = new Label(wicketId, dateString);
-		WicketUtils.setCssClass(label, TimeUtils.timeAgoCss(date));
 		if (!StringUtils.isEmpty(title)) {
 			WicketUtils.setHtmlTooltip(label, title);
 		}
@@ -494,9 +495,10 @@
 		String format = GitBlit.getString(Keys.web.datetimestampLongFormat,
 				"EEEE, MMMM d, yyyy HH:mm Z");
 		DateFormat df = new SimpleDateFormat(format);
-		if (timeZone != null) {
-			df.setTimeZone(timeZone);
+		if (timeZone == null) {
+			timeZone = GitBlit.getTimezone();
 		}
+		df.setTimeZone(timeZone);
 		String dateString;
 		if (date.getTime() == 0) {
 			dateString = "--";
diff --git a/src/com/gitblit/wicket/pages/BasePage.java b/src/com/gitblit/wicket/pages/BasePage.java
index 515e9ce..3852818 100644
--- a/src/com/gitblit/wicket/pages/BasePage.java
+++ b/src/com/gitblit/wicket/pages/BasePage.java
@@ -181,7 +181,7 @@
 
 	protected TimeZone getTimeZone() {
 		return GitBlit.getBoolean(Keys.web.useClientTimezone, false) ? GitBlitWebSession.get()
-				.getTimezone() : TimeZone.getDefault();
+				.getTimezone() : GitBlit.getTimezone();
 	}
 
 	protected String getServerName() {
diff --git a/src/com/gitblit/wicket/panels/BasePanel.java b/src/com/gitblit/wicket/panels/BasePanel.java
index 73e1399..3606dd0 100644
--- a/src/com/gitblit/wicket/panels/BasePanel.java
+++ b/src/com/gitblit/wicket/panels/BasePanel.java
@@ -38,7 +38,7 @@
 
 	protected TimeZone getTimeZone() {
 		return GitBlit.getBoolean(Keys.web.useClientTimezone, false) ? GitBlitWebSession.get()
-				.getTimezone() : TimeZone.getDefault();
+				.getTimezone() : GitBlit.getTimezone();
 	}
 
 	protected void setPersonSearchTooltip(Component component, String value, Constants.SearchType searchType) {

--
Gitblit v1.9.1