diff options
author | Martin Fietz <Martin.Fietz@gmail.com> | 2017-04-10 23:25:55 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-04-10 23:25:55 +0200 |
commit | e42a76219df0ef0af5f9b92384eaf03f9ffbb22c (patch) | |
tree | 81fd2c1667617a6aa37fa70a8ed1069d36aa59e5 /core | |
parent | ef4bd9fd560b735e08078cc92bfd1413a0053d93 (diff) | |
parent | 545b1e364eec52a28ce823b0b8c598b163ebfd95 (diff) | |
download | AntennaPod-e42a76219df0ef0af5f9b92384eaf03f9ffbb22c.zip |
Merge pull request #2288 from dklimkin/datefix
Additional date format and a hack for CEST
Diffstat (limited to 'core')
-rw-r--r-- | core/src/androidTest/java/de/danoeh/antennapod/core/tests/util/DateUtilsTest.java | 15 | ||||
-rw-r--r-- | core/src/main/java/de/danoeh/antennapod/core/util/DateUtils.java | 40 |
2 files changed, 37 insertions, 18 deletions
diff --git a/core/src/androidTest/java/de/danoeh/antennapod/core/tests/util/DateUtilsTest.java b/core/src/androidTest/java/de/danoeh/antennapod/core/tests/util/DateUtilsTest.java index 2727b1447..bdd114f46 100644 --- a/core/src/androidTest/java/de/danoeh/antennapod/core/tests/util/DateUtilsTest.java +++ b/core/src/androidTest/java/de/danoeh/antennapod/core/tests/util/DateUtilsTest.java @@ -101,4 +101,19 @@ public class DateUtilsTest extends AndroidTestCase { assertEquals(expected, actual); } + public void testParseDateWithNoTimezonePadding() throws Exception { + GregorianCalendar exp = new GregorianCalendar(2017, 1, 22, 22, 28, 00); + exp.setTimeZone(TimeZone.getTimeZone("UTC")); + Date expected = new Date(exp.getTimeInMillis() + 2); + Date actual = DateUtils.parse("2017-02-22T14:28:00.002-08:00"); + assertEquals(expected, actual); + } + + public void testParseDateWithForCest() throws Exception { + GregorianCalendar exp = new GregorianCalendar(2017, 0, 28, 23, 00, 00); + exp.setTimeZone(TimeZone.getTimeZone("UTC")); + Date expected = new Date(exp.getTimeInMillis()); + Date actual = DateUtils.parse("Sun, 29 Jan 2017 00:00:00 CEST"); + assertEquals(expected, actual); + } } diff --git a/core/src/main/java/de/danoeh/antennapod/core/util/DateUtils.java b/core/src/main/java/de/danoeh/antennapod/core/util/DateUtils.java index 314062e52..c06921c95 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/util/DateUtils.java +++ b/core/src/main/java/de/danoeh/antennapod/core/util/DateUtils.java @@ -16,41 +16,44 @@ import java.util.TimeZone; * Parses several date formats. */ public class DateUtils { - - private static final String TAG = "DateUtils"; + + private static final String TAG = "DateUtils"; private static final TimeZone defaultTimezone = TimeZone.getTimeZone("GMT"); public static Date parse(final String input) { - if(input == null) { + if (input == null) { throw new IllegalArgumentException("Date must not be null"); } String date = input.trim().replace('/', '-').replaceAll("( ){2,}+", " "); + // CEST is widely used but not in the "ISO 8601 Time zone" list. Let's hack around. + date = date.replaceAll("CEST$", "+01:00"); + // if datetime is more precise than seconds, make sure the value is in ms - if(date.contains(".")) { + if (date.contains(".")) { int start = date.indexOf('.'); - int current = start+1; - while(current < date.length() && Character.isDigit(date.charAt(current))) { + int current = start + 1; + while (current < date.length() && Character.isDigit(date.charAt(current))) { current++; } // even more precise than microseconds: discard further decimal places - if(current - start > 4) { - if(current < date.length()-1) { + if (current - start > 4) { + if (current < date.length() - 1) { date = date.substring(0, start + 4) + date.substring(current); } else { date = date.substring(0, start + 4); } - // less than 4 decimal places: pad to have a consistent format for the parser - } else if(current - start < 4) { - if(current < date.length()-1) { - date = date.substring(0, current) + StringUtils.repeat("0", 4-(current-start)) + date.substring(current); + // less than 4 decimal places: pad to have a consistent format for the parser + } else if (current - start < 4) { + if (current < date.length() - 1) { + date = date.substring(0, current) + StringUtils.repeat("0", 4 - (current - start)) + date.substring(current); } else { - date = date.substring(0, current) + StringUtils.repeat("0", 4-(current-start)); + date = date.substring(0, current) + StringUtils.repeat("0", 4 - (current - start)); } } } - String[] patterns = { + final String[] patterns = { "dd MMM yy HH:mm:ss Z", "dd MMM yy HH:mm Z", "EEE, dd MMM yyyy HH:mm:ss Z", @@ -76,6 +79,7 @@ public class DateUtils { "yyyy-MM-dd'T'HH:mm:ss.SSS", "yyyy-MM-dd'T'HH:mm:ssZ", "yyyy-MM-dd'T'HH:mm:ss'Z'", + "yyyy-MM-dd'T'HH:mm:ss.SSSZ", "yyyy-MM-ddZ", "yyyy-MM-dd" }; @@ -85,7 +89,7 @@ public class DateUtils { parser.setTimeZone(defaultTimezone); ParsePosition pos = new ParsePosition(0); - for(String pattern : patterns) { + for (String pattern : patterns) { parser.applyPattern(pattern); pos.setIndex(0); try { @@ -93,7 +97,7 @@ public class DateUtils { if (result != null && pos.getIndex() == date.length()) { return result; } - } catch(Exception e) { + } catch (Exception e) { Log.e(TAG, Log.getStackTraceString(e)); } } @@ -143,7 +147,7 @@ public class DateUtils { } public static String formatAbbrev(final Context context, final Date date) { - if(date == null) { + if (date == null) { return ""; } GregorianCalendar cal = new GregorianCalendar(); @@ -152,7 +156,7 @@ public class DateUtils { cal.add(GregorianCalendar.DAY_OF_MONTH, 10); boolean withinLastYear = date.after(cal.getTime()); int format = android.text.format.DateUtils.FORMAT_ABBREV_ALL; - if(withinLastYear) { + if (withinLastYear) { format |= android.text.format.DateUtils.FORMAT_NO_YEAR; } return android.text.format.DateUtils.formatDateTime(context, date.getTime(), format); |