import org.eclipse.jgit.junit.RepositoryTestCase;
import org.eclipse.jgit.util.GitDateParser;
+import org.eclipse.jgit.util.SystemReader;
import org.junit.Before;
import org.junit.Test;
@Test
public void testGConeCommit() throws Exception {
- Date expire = GitDateParser.parse("now", null);
+ Date expire = GitDateParser.parse("now", null, SystemReader
+ .getInstance().getLocale());
Properties res = git.gc().setExpire(expire).call();
assertTrue(res.size() == 7);
}
writeTrashFile("b.txt", "a couple of words for gc to pack more 2");
writeTrashFile("c.txt", "a couple of words for gc to pack more 3");
git.commit().setAll(true).setMessage("commit3").call();
- Properties res = git.gc().setExpire(GitDateParser.parse("now", null))
- .call();
+ Properties res = git
+ .gc()
+ .setExpire(
+ GitDateParser.parse("now", null, SystemReader
+ .getInstance().getLocale())).call();
assertTrue(res.size() == 7);
}
}
Calendar ref = new GregorianCalendar(SystemReader.getInstance()
.getTimeZone(), SystemReader.getInstance().getLocale());
try {
- GitDateParser.parse(dateStr, ref);
+ GitDateParser.parse(dateStr, ref, SystemReader.getInstance()
+ .getLocale());
fail("The expected ParseException while parsing '" + dateStr
+ "' did not occur.");
} catch (ParseException e) {
@Theory
public void badlyFormattedWithoutRef() {
try {
- GitDateParser.parse(dateStr, null);
+ GitDateParser.parse(dateStr, null, SystemReader.getInstance()
+ .getLocale());
fail("The expected ParseException while parsing '" + dateStr
+ "' did not occur.");
} catch (ParseException e) {
GregorianCalendar cal = new GregorianCalendar(SystemReader
.getInstance().getTimeZone(), SystemReader.getInstance()
.getLocale());
- Date parse = GitDateParser.parse("yesterday", cal);
+ Date parse = GitDateParser.parse("yesterday", cal, SystemReader
+ .getInstance().getLocale());
cal.add(Calendar.DATE, -1);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
GregorianCalendar cal = new GregorianCalendar(SystemReader
.getInstance().getTimeZone(), SystemReader.getInstance()
.getLocale());
- Date parse = GitDateParser.parse("never", cal);
+ Date parse = GitDateParser.parse("never", cal, SystemReader
+ .getInstance().getLocale());
Assert.assertEquals(GitDateParser.NEVER, parse);
parse = GitDateParser.parse("never", null);
Assert.assertEquals(GitDateParser.NEVER, parse);
.getLocale());
cal.setTime(refDate);
- Date parse = GitDateParser.parse("now", cal);
+ Date parse = GitDateParser.parse("now", cal, SystemReader.getInstance()
+ .getLocale());
Assert.assertEquals(refDate, parse);
long t1 = SystemReader.getInstance().getCurrentTime();
parse = GitDateParser.parse("now", null);
.getLocale());
cal.setTime(refDate);
- Date parse = GitDateParser.parse("2 weeks ago", cal);
+ Date parse = GitDateParser.parse("2 weeks ago", cal, SystemReader
+ .getInstance().getLocale());
Assert.assertEquals(df.parse("2007-02-07 15:35:00 +0100"), parse);
}
.getLocale());
cal.setTime(refDate);
- Date parse = GitDateParser.parse("2 weeks ago", cal);
+ Date parse = GitDateParser.parse("2 weeks ago", cal, SystemReader.getInstance()
+ .getLocale());
Assert.assertEquals(df.parse("2007-02-07 15:35:00 +0100"), parse);
parse = GitDateParser.parse("3 days 2 weeks ago", cal);
Assert.assertEquals(df.parse("2007-02-04 15:35:00 +0100"), parse);
String dateStr = "2007-02-21 15:35:00 +0100";
Date exp = SystemReader.getInstance()
.getSimpleDateFormat("yyyy-MM-dd HH:mm:ss Z").parse(dateStr);
- Date parse = GitDateParser.parse(dateStr, null);
+ Date parse = GitDateParser.parse(dateStr, null, SystemReader
+ .getInstance().getLocale());
Assert.assertEquals(exp, parse);
}
Date exp = SystemReader.getInstance()
.getSimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z")
.parse(dateStr);
- Date parse = GitDateParser.parse(dateStr, null);
+ Date parse = GitDateParser.parse(dateStr, null, SystemReader
+ .getInstance().getLocale());
Assert.assertEquals(exp, parse);
}
String dateStr = "2007-02-21";
Date exp = SystemReader.getInstance().getSimpleDateFormat("yyyy-MM-dd")
.parse(dateStr);
- Date parse = GitDateParser.parse(dateStr, null);
+ Date parse = GitDateParser.parse(dateStr, null, SystemReader
+ .getInstance().getLocale());
Assert.assertEquals(exp, parse);
}
String dateStr = "2007.02.21";
Date exp = SystemReader.getInstance().getSimpleDateFormat("yyyy.MM.dd")
.parse(dateStr);
- Date parse = GitDateParser.parse(dateStr, null);
+ Date parse = GitDateParser.parse(dateStr, null, SystemReader
+ .getInstance().getLocale());
Assert.assertEquals(exp, parse);
}
String dateStr = "02/21/2007";
Date exp = SystemReader.getInstance().getSimpleDateFormat("MM/dd/yyyy")
.parse(dateStr);
- Date parse = GitDateParser.parse(dateStr, null);
+ Date parse = GitDateParser.parse(dateStr, null, SystemReader
+ .getInstance().getLocale());
Assert.assertEquals(exp, parse);
}
String dateStr = "21.02.2007";
Date exp = SystemReader.getInstance().getSimpleDateFormat("dd.MM.yyyy")
.parse(dateStr);
- Date parse = GitDateParser.parse(dateStr, null);
+ Date parse = GitDateParser.parse(dateStr, null, SystemReader
+ .getInstance().getLocale());
Assert.assertEquals(exp, parse);
}
Date exp = SystemReader.getInstance()
.getSimpleDateFormat("EEE MMM dd HH:mm:ss yyyy Z")
.parse(dateStr);
- Date parse = GitDateParser.parse(dateStr, null);
+ Date parse = GitDateParser.parse(dateStr, null, SystemReader
+ .getInstance().getLocale());
Assert.assertEquals(exp, parse);
}
String dateStr = "Wed Feb 21 15:35:00 2007";
Date exp = SystemReader.getInstance()
.getSimpleDateFormat("EEE MMM dd HH:mm:ss yyyy").parse(dateStr);
- Date parse = GitDateParser.parse(dateStr, null);
+ Date parse = GitDateParser.parse(dateStr, null, SystemReader
+ .getInstance().getLocale());
Assert.assertEquals(exp, parse);
}
}
import org.eclipse.jgit.treewalk.filter.TreeFilter;
import org.eclipse.jgit.util.FileUtils;
import org.eclipse.jgit.util.GitDateParser;
+import org.eclipse.jgit.util.SystemReader;
/**
* A garbage collector for git {@link FileRepository}. Instances of this class
ConfigConstants.CONFIG_KEY_PRUNEEXPIRE);
if (pruneExpireStr == null)
pruneExpireStr = PRUNE_EXPIRE_DEFAULT;
- expire = GitDateParser.parse(pruneExpireStr, null);
+ expire = GitDateParser.parse(pruneExpireStr, null, SystemReader
+ .getInstance().getLocale());
expireAgeMillis = -1;
}
if (expire != null)
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
+import java.util.Locale;
import java.util.Map;
import org.eclipse.jgit.internal.JGitText;
// Since SimpleDateFormat instances are expensive to instantiate they should
// be cached. Since they are also not threadsafe they are cached using
// ThreadLocal.
- private static ThreadLocal<Map<ParseableSimpleDateFormat, SimpleDateFormat>> formatCache = new ThreadLocal<Map<ParseableSimpleDateFormat, SimpleDateFormat>>() {
- protected Map<ParseableSimpleDateFormat, SimpleDateFormat> initialValue() {
- return new HashMap<ParseableSimpleDateFormat, SimpleDateFormat>();
+ private static ThreadLocal<Map<Locale, Map<ParseableSimpleDateFormat, SimpleDateFormat>>> formatCache =
+ new ThreadLocal<Map<Locale, Map<ParseableSimpleDateFormat, SimpleDateFormat>>>() {
+
+ protected Map<Locale, Map<ParseableSimpleDateFormat, SimpleDateFormat>> initialValue() {
+ return new HashMap<Locale, Map<ParseableSimpleDateFormat, SimpleDateFormat>>();
}
};
- // Gets an instance of a SimpleDateFormat. If there is not already an
- // appropriate instance in the (ThreadLocal) cache the create one and put in
- // into the cache
- private static SimpleDateFormat getDateFormat(ParseableSimpleDateFormat f) {
- Map<ParseableSimpleDateFormat, SimpleDateFormat> map = formatCache
+ // Gets an instance of a SimpleDateFormat for the specified locale. If there
+ // is not already an appropriate instance in the (ThreadLocal) cache then
+ // create one and put it into the cache.
+ private static SimpleDateFormat getDateFormat(ParseableSimpleDateFormat f,
+ Locale locale) {
+ Map<Locale, Map<ParseableSimpleDateFormat, SimpleDateFormat>> cache = formatCache
.get();
+ Map<ParseableSimpleDateFormat, SimpleDateFormat> map = cache
+ .get(locale);
+ if (map == null) {
+ map = new HashMap<ParseableSimpleDateFormat, SimpleDateFormat>();
+ cache.put(locale, map);
+ return getNewSimpleDateFormat(f, locale, map);
+ }
SimpleDateFormat dateFormat = map.get(f);
if (dateFormat != null)
return dateFormat;
+ SimpleDateFormat df = getNewSimpleDateFormat(f, locale, map);
+ return df;
+ }
+
+ private static SimpleDateFormat getNewSimpleDateFormat(
+ ParseableSimpleDateFormat f, Locale locale,
+ Map<ParseableSimpleDateFormat, SimpleDateFormat> map) {
SimpleDateFormat df = SystemReader.getInstance().getSimpleDateFormat(
- f.formatStr);
+ f.formatStr, locale);
map.put(f, df);
return df;
}
}
/**
- * Parses a string into a {@link Date}. Since this parser also supports
- * relative formats (e.g. "yesterday") the caller can specify the reference
- * date. These types of strings can be parsed:
+ * Parses a string into a {@link Date} using the default locale. Since this
+ * parser also supports 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>
*/
public static Date parse(String dateStr, Calendar now)
throws ParseException {
+ return parse(dateStr, now, Locale.getDefault());
+ }
+
+ /**
+ * Parses a string into a {@link Date} using the given locale. Since this
+ * parser also supports 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>
+ * Multiple specs can be combined like in "2 weeks 3 days ago". Instead of
+ * ' ' one can use '.' to seperate the words</li>
+ * <li>"yyyy-MM-dd HH:mm:ss Z" (ISO)</li>
+ * <li>"EEE, dd MMM yyyy HH:mm:ss Z" (RFC)</li>
+ * <li>"yyyy-MM-dd"</li>
+ * <li>"yyyy.MM.dd"</li>
+ * <li>"MM/dd/yyyy",</li>
+ * <li>"dd.MM.yyyy"</li>
+ * <li>"EEE MMM dd HH:mm:ss yyyy Z" (DEFAULT)</li>
+ * <li>"EEE MMM dd HH:mm:ss yyyy" (LOCAL)</li>
+ * </ul>
+ *
+ * @param dateStr
+ * the string to be parsed
+ * @param now
+ * the base date which is used for the calculation of relative
+ * formats. E.g. if baseDate is "25.8.2012" then parsing of the
+ * string "1 week ago" would result in a date corresponding to
+ * "18.8.2012". This is used when a JGit command calls this
+ * 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.
+ * @param locale
+ * locale to be used to parse the date string
+ * @return the parsed {@link Date}
+ * @throws ParseException
+ * if the given dateStr was not recognized
+ * @since 3.2
+ */
+ public static Date parse(String dateStr, Calendar now, Locale locale)
+ throws ParseException {
dateStr = dateStr.trim();
Date ret;
return ret;
for (ParseableSimpleDateFormat f : ParseableSimpleDateFormat.values()) {
try {
- return parse_simple(dateStr, f);
+ return parse_simple(dateStr, f, locale);
} catch (ParseException e) {
// simply proceed with the next parser
}
}
// 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, Locale locale)
throws ParseException {
- SimpleDateFormat dateFormat = getDateFormat(f);
+ SimpleDateFormat dateFormat = getDateFormat(f, locale);
dateFormat.setLenient(false);
return dateFormat.parse(dateStr);
}
return new SimpleDateFormat(pattern);
}
+ /**
+ * Returns a simple date format instance as specified by the given pattern.
+ *
+ * @param pattern
+ * the pattern as defined in
+ * {@link SimpleDateFormat#SimpleDateFormat(String)}
+ * @param locale
+ * locale to be used for the {@code SimpleDateFormat}
+ * @return the simple date format
+ * @since 3.2
+ */
+ public SimpleDateFormat getSimpleDateFormat(String pattern, Locale locale) {
+ return new SimpleDateFormat(pattern, locale);
+ }
+
/**
* Returns a date/time format instance for the given styles.
*