Skip to content

Commit

Permalink
Revert to using java.text.SimpleDateFormat for retrieving timestamps
Browse files Browse the repository at this point in the history
Millisecond parsing in Joda has different results than with
Date/SimpleDateFormat.  If the number of ms digits in the format string
exceeds the number of ms digits in the date string, then zero padding
happens on the wrong side (e.g. 93 becomes 930 instead of 093).  If
the number of ms digits in the date string exceeds the number of ms
digits in the format string, then parsing fails.  See
JodaOrg/joda-time#62.

Fixes #12117.
  • Loading branch information
melissalinkert committed Dec 26, 2014
1 parent 0147338 commit 61db1c1
Showing 1 changed file with 6 additions and 14 deletions.
20 changes: 6 additions & 14 deletions components/formats-common/src/loci/common/DateTools.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@

import java.text.FieldPosition;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
Expand Down Expand Up @@ -259,20 +261,10 @@ public static String formatDate(String date, String[] formats,
* (in Unix format: milliseconds since January 1, 1970).
*/
public static long getTime(String date, String format) {
final DateTimeFormatter parser =
DateTimeFormat.forPattern(format).withZone(DateTimeZone.UTC);
Instant timestamp = null;
try {
timestamp = Instant.parse(date, parser);
}
catch (IllegalArgumentException e) {
LOGGER.debug("Invalid timestamp '{}'", date);
}
catch (UnsupportedOperationException e) {
LOGGER.debug("Error parsing timestamp '{}'", date, e);
}
if (timestamp == null) return -1;
return timestamp.getMillis();
SimpleDateFormat f = new SimpleDateFormat(format);
Date d = f.parse(date, new ParsePosition(0));
if (d == null) return -1;
return d.getTime();
}

/**
Expand Down

0 comments on commit 61db1c1

Please sign in to comment.