[EGIT PATCH 1/2] Use a UTC relative time zone for PersonIdent

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



The timezone used in Git objects is not the user's preferred
timezone, but rather a numeric form stating the offset relative to
UTC in hours and minuutes. Any special rules like daylight savings
is already accounted for and cannot be deduced from the time
zone part of the git timestamp.

As an example, a committer in Sweden will have +0100 as
his/her timezone in winter and +0200 in summer.

We hereby abandon the attempts to guess a real timezone
from the UTC offset. When creating a person ident a real
timezone can be used, but it's identifty will be lost when externalizing
and only the UTC offset will be left.

Signed-off-by: Robin Rosenberg <robin.rosenberg@xxxxxxxxxx>
---
 .../src/org/spearce/jgit/lib/PersonIdent.java      |   54 ++++++++++++-------
 1 files changed, 34 insertions(+), 20 deletions(-)

diff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/PersonIdent.java b/org.spearce.jgit/src/org/spearce/jgit/lib/PersonIdent.java
index 85435b5..4ed2f71 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/lib/PersonIdent.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/lib/PersonIdent.java
@@ -39,7 +39,9 @@
 
 package org.spearce.jgit.lib;
 
+import java.text.SimpleDateFormat;
 import java.util.Date;
+import java.util.Locale;
 import java.util.TimeZone;
 
 /**
@@ -228,24 +230,24 @@ public String getEmailAddress() {
 	}
 
 	/**
-	 * @return timestamp
+	 * @return timestamp in the person's local time
 	 */
 	public Date getWhen() {
 		return new Date(when);
 	}
 
 	/**
-	 * @return this person's preferred time zone; null if time zone is unknown.
+	 * @return this person's declared time zone; null if time zone is unknown.
 	 */
 	public TimeZone getTimeZone() {
-		final String[] ids = TimeZone.getAvailableIDs(tzOffset * 60 * 1000);
-		if (ids.length == 0)
-			return null;
-		return TimeZone.getTimeZone(ids[0]);
+		StringBuffer tzId = new StringBuffer(8);
+		tzId.append("GMT");
+		appendTimezone(tzId);
+		return TimeZone.getTimeZone(tzId.toString());
 	}
 
 	/**
-	 * @return this person's preferred time zone as minutes east of UTC. If the
+	 * @return this person's declared time zone as minutes east of UTC. If the
 	 *         timezone is to the west of UTC it is negative.
 	 */
 	public int getTimeZoneOffset() {
@@ -273,6 +275,17 @@ public boolean equals(final Object o) {
 	 */
 	public String toExternalString() {
 		final StringBuffer r = new StringBuffer();
+		r.append(getName());
+		r.append(" <");
+		r.append(getEmailAddress());
+		r.append("> ");
+		r.append(when / 1000);
+		r.append(' ');
+		appendTimezone(r);
+		return r.toString();
+	}
+
+	private void appendTimezone(final StringBuffer r) {
 		int offset = tzOffset;
 		final char sign;
 		final int offsetHours;
@@ -288,12 +301,6 @@ public String toExternalString() {
 		offsetHours = offset / 60;
 		offsetMins = offset % 60;
 
-		r.append(getName());
-		r.append(" <");
-		r.append(getEmailAddress());
-		r.append("> ");
-		r.append(when / 1000);
-		r.append(' ');
 		r.append(sign);
 		if (offsetHours < 10) {
 			r.append('0');
@@ -303,25 +310,32 @@ public String toExternalString() {
 			r.append('0');
 		}
 		r.append(offsetMins);
-		return r.toString();
 	}
 
 	public String toString() {
 		final StringBuffer r = new StringBuffer();
-		int minutes;
-
-		minutes = tzOffset < 0 ? -tzOffset : tzOffset;
-		minutes = (minutes / 100) * 60 + (minutes % 100);
-		minutes = tzOffset < 0 ? -minutes : minutes;
 
 		r.append("PersonIdent[");
 		r.append(getName());
 		r.append(", ");
 		r.append(getEmailAddress());
 		r.append(", ");
-		r.append(new Date(when + minutes * 60));
+		r.append(formatTime(new SimpleDateFormat("EEE MMM d HH:mm:ss yyyy Z", Locale.US)));
 		r.append("]");
 
 		return r.toString();
 	}
+
+	/**
+	 * Format the time represented by this object using the give
+	 * SimpleDateFormat.
+	 * 
+	 * @param simpleDateFormat
+	 * @return a formatted time stamp.
+	 */
+	@SuppressWarnings("boxing")
+	public String formatTime(SimpleDateFormat simpleDateFormat) {
+		simpleDateFormat.setTimeZone(getTimeZone());
+		return simpleDateFormat.format(when);
+	}
 }
-- 
1.6.3.2.199.g7340d

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]