aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit/src/org/eclipse/jgit/util
diff options
context:
space:
mode:
authorMatthias Sohn <matthias.sohn@sap.com>2012-09-19 09:02:04 +0200
committerMatthias Sohn <matthias.sohn@sap.com>2012-09-19 09:02:31 +0200
commit555ad1f93c4310c99647851b84152660376d66f0 (patch)
tree6a3cae181f9f1f458aec84e66d6975ded1508b5c /org.eclipse.jgit/src/org/eclipse/jgit/util
parent1f19d0a834a3add31e40911578f9f547c2466e2b (diff)
parent54c4eb69acf700fdf80304e9d0827d3ea13cbc6d (diff)
downloadjgit-555ad1f93c4310c99647851b84152660376d66f0.tar.gz
jgit-555ad1f93c4310c99647851b84152660376d66f0.zip
Merge branch 'stable-2.1'
* stable-2.1: Prepare for 2.1 maintenance changes JGit v2.1.0.201209190230-r Introduce "never" as parseable date Introduce ParseExceptions for GitDateParser Support config param "gc.pruneexpire" Change-Id: If149d7f968a3425d9425f6ba9ce135a8341776a7 Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
Diffstat (limited to 'org.eclipse.jgit/src/org/eclipse/jgit/util')
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/util/GitDateParser.java49
1 files changed, 35 insertions, 14 deletions
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/util/GitDateParser.java b/org.eclipse.jgit/src/org/eclipse/jgit/util/GitDateParser.java
index 04ef553b05..f1743d4f16 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/util/GitDateParser.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/util/GitDateParser.java
@@ -42,6 +42,7 @@
*/
package org.eclipse.jgit.util;
+import java.text.MessageFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
@@ -50,6 +51,8 @@ import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;
+import org.eclipse.jgit.internal.JGitText;
+
/**
* Parses strings with time and date specifications into {@link Date}.
*
@@ -59,6 +62,12 @@ import java.util.Map;
* understands.
*/
public class GitDateParser {
+ /**
+ * The Date representing never. Though this is a concrete value, most
+ * callers are adviced to avoid depending on the actual value.
+ */
+ public static final Date NEVER = new Date(Long.MAX_VALUE);
+
// Since SimpleDateFormat instances are expensive to instantiate they should
// be cached. Since they are also not threadsafe they are cached using
// ThreadLocal.
@@ -110,6 +119,7 @@ public class GitDateParser {
* relative formats (e.g. "yesterday") the caller can specify the reference
* date. These types of strings can be parsed:
* <ul>
+ * <li>"never"</li>
* <li>"now"</li>
* <li>"yesterday"</li>
* <li>"(x) years|months|weeks|days|hours|minutes|seconds ago"<br>
@@ -135,32 +145,43 @@ public class GitDateParser {
* parser often but wants a consistent starting point for calls.<br>
* If set to <code>null</code> then the current time will be used
* instead.
- * @return the parsed {@link Date} or <code>null</code> if this string was
- * not parseable.
+ * @return the parsed {@link Date}
+ * @throws ParseException
+ * if the given dateStr was not recognized
*/
- public static Date parse(String dateStr, Calendar now) {
+ public static Date parse(String dateStr, Calendar now)
+ throws ParseException {
dateStr = dateStr.trim();
Date ret;
+
+ if ("never".equalsIgnoreCase(dateStr))
+ return NEVER;
ret = parse_relative(dateStr, now);
if (ret != null)
return ret;
for (ParseableSimpleDateFormat f : ParseableSimpleDateFormat.values()) {
- ret = parse_simple(dateStr, f);
- if (ret != null)
- return ret;
+ try {
+ return parse_simple(dateStr, f);
+ } catch (ParseException e) {
+ // simply proceed with the next parser
+ }
}
- return null;
+ ParseableSimpleDateFormat[] values = ParseableSimpleDateFormat.values();
+ StringBuilder allFormats = new StringBuilder("\"")
+ .append(values[0].formatStr);
+ for (int i = 1; i < values.length; i++)
+ allFormats.append("\", \"").append(values[i].formatStr);
+ allFormats.append("\"");
+ throw new ParseException(MessageFormat.format(
+ JGitText.get().cannotParseDate, dateStr, allFormats.toString()), 0);
}
// tries to parse a string with the formats supported by SimpleDateFormat
- private static Date parse_simple(String dateStr, ParseableSimpleDateFormat f) {
+ private static Date parse_simple(String dateStr, ParseableSimpleDateFormat f)
+ throws ParseException {
SimpleDateFormat dateFormat = getDateFormat(f);
- try {
- dateFormat.setLenient(false);
- return dateFormat.parse(dateStr);
- } catch (ParseException e) {
- return null;
- }
+ dateFormat.setLenient(false);
+ return dateFormat.parse(dateStr);
}
// tries to parse a string with a relative time specification