Git development
 help / color / mirror / Atom feed
From: Robin Rosenberg <robin.rosenberg@dewire.com>
To: spearce@spearce.org
Cc: git@vger.kernel.org, Robin Rosenberg <robin.rosenberg@dewire.com>
Subject: [JGIT PATCH 1/2] Add time and timezone to SystemReader properties
Date: Sun, 16 Aug 2009 23:36:51 +0200	[thread overview]
Message-ID: <1250458612-21095-1-git-send-email-robin.rosenberg@dewire.com> (raw)

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

             reply	other threads:[~2009-08-16 21:37 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-08-16 21:36 Robin Rosenberg [this message]
2009-08-16 21:36 ` [JGIT PATCH 2/2] Update the reflog for HEAD when the referenced branch is modified Robin Rosenberg

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1250458612-21095-1-git-send-email-robin.rosenberg@dewire.com \
    --to=robin.rosenberg@dewire.com \
    --cc=git@vger.kernel.org \
    --cc=spearce@spearce.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox