git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[JGIT PATCH 1/2] Add time and timezone to SystemReader properties

From
Robin Rosenberg <robin.rosenberg@dewire.com>
Date
Aug 16, 2009, 21:36 UTC
Message-ID
<1250458612-21095-1-git-send-email-robin.rosenberg@dewire.com>

Having an uncontrollable variable time and timezone is inconvenient for unit testing.

Signed-off-by: Robin Rosenberg <robin.rosenberg@dewire.com>
---
 .../tst/org/spearce/jgit/lib/MockSystemReader.java |   11 ++++++++++
 .../src/org/spearce/jgit/lib/PersonIdent.java      |    6 +++-
 .../src/org/spearce/jgit/util/SystemReader.java    |   22 ++++++++++++++++++++
 3 files changed, 37 insertions(+), 2 deletions(-)
diff --git a/org.spearce.jgit.test/tst/org/spearce/jgit/lib/MockSystemReader.java b/org.spearce.jgit.test/tst/org/spearce/jgit/lib/MockSystemReader.java
index 7a65f99..e51df7e 100644
--- a/org.spearce.jgit.test/tst/org/spearce/jgit/lib/MockSystemReader.java
+++ b/org.spearce.jgit.test/tst/org/spearce/jgit/lib/MockSystemReader.java
@@ -39,6 +39,7 @@
 
 import java.util.HashMap;
 import java.util.Map;
+import java.util.TimeZone;
 
 import org.spearce.jgit.util.SystemReader;
 
@@ -75,4 +76,14 @@ public FileBasedConfig openUserConfig() {
 	public String getHostname() {
 		return "fake.host.example.com";
 	}
+
+	@Override
+	public long getCurrentTime() {
+		return 1250379778668L; // Sat Aug 15 20:12:58 GMT-03:30 2009 
+	}
+
+	@Override
+	public int getTimezone(long when) {
+		return TimeZone.getTimeZone("GMT-3:30").getOffset(when);
+	}
 }
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 393e177..a0418ab 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/lib/PersonIdent.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/lib/PersonIdent.java
@@ -44,6 +44,8 @@
 import java.util.Locale;
 import java.util.TimeZone;
 
+import org.spearce.jgit.util.SystemReader;
+
 /**
  * A combination of a person identity and time in Git.
  * 
@@ -70,8 +72,8 @@ public PersonIdent(final Repository repo) {
 		final RepositoryConfig config = repo.getConfig();
 		name = config.getCommitterName();
 		emailAddress = config.getCommitterEmail();
-		when = System.currentTimeMillis();
-		tzOffset = TimeZone.getDefault().getOffset(when) / (60 * 1000);
+		when = SystemReader.getInstance().getCurrentTime();
+		tzOffset = SystemReader.getInstance().getTimezone(when);
 	}
 
 	/**
diff --git a/org.spearce.jgit/src/org/spearce/jgit/util/SystemReader.java b/org.spearce.jgit/src/org/spearce/jgit/util/SystemReader.java
index 51a0d29..083d120 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/util/SystemReader.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/util/SystemReader.java
@@ -40,6 +40,7 @@
 import java.io.File;
 import java.net.InetAddress;
 import java.net.UnknownHostException;
+import java.util.TimeZone;
 
 import org.spearce.jgit.lib.FileBasedConfig;
 
@@ -81,6 +82,16 @@ public String getHostname() {
 			}
 			return hostname;
 		}
+
+		@Override
+		public long getCurrentTime() {
+			return System.currentTimeMillis();
+		}
+
+		@Override
+		public int getTimezone(long when) {
+			return TimeZone.getDefault().getOffset(when) / (60 * 1000);
+		}
 	};
 
 	/** @return the live instance to read system properties. */
@@ -120,4 +131,15 @@ public static void setInstance(SystemReader newReader) {
 	 * @return the git configuration found in the user home
 	 */
 	public abstract FileBasedConfig openUserConfig();
+
+	/**
+	 * @return the current system time
+	 */
+	public abstract long getCurrentTime();
+
+	/**
+	 * @param when TODO
+	 * @return the local time zone
+	 */
+	public abstract int getTimezone(long when);
 }
-- 
1.6.4.115.gc0eb0
Next: Robin Rosenberg
Message 1 of 2 in “Add time and timezone to SystemReader properties”
  1. 1/2 Add time and timezone to SystemReader propertiesRobin Rosenberg, Aug 16, 2009
  2. 2/2 Update the reflog for HEAD when the referenced branch is modifiedRobin Rosenberg, Aug 16, 2009

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.