Support time zone Z in RFC 822 dates
This commit is contained in:
parent
8cac90db66
commit
73cd60ea1e
2 changed files with 62 additions and 6 deletions
|
@ -19,7 +19,9 @@ package com.rometools.rome.io.impl;
|
|||
import java.text.DateFormat;
|
||||
import java.text.ParsePosition;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Arrays;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.TimeZone;
|
||||
|
||||
|
@ -146,15 +148,33 @@ public class DateParser {
|
|||
*
|
||||
*/
|
||||
public static Date parseRFC822(String sDate, final Locale locale) {
|
||||
final int utIndex = sDate.indexOf(" UT");
|
||||
if (utIndex > -1) {
|
||||
final String pre = sDate.substring(0, utIndex);
|
||||
final String post = sDate.substring(utIndex + 3);
|
||||
sDate = pre + " GMT" + post;
|
||||
}
|
||||
sDate = convertUnsupportedTimeZones(sDate);
|
||||
return parseUsingMask(RFC822_MASKS, sDate, locale);
|
||||
}
|
||||
|
||||
private static String convertUnsupportedTimeZones(String sDate) {
|
||||
final List<String> unsupportedZeroOffsetTimeZones = Arrays.asList("UT", "Z");
|
||||
|
||||
for (String timeZone : unsupportedZeroOffsetTimeZones) {
|
||||
if (sDate.endsWith(timeZone)) {
|
||||
return replaceLastOccurrence(sDate, timeZone, "UTC");
|
||||
}
|
||||
}
|
||||
return sDate;
|
||||
}
|
||||
|
||||
private static String replaceLastOccurrence(String original, String target, String replacement) {
|
||||
final int lastIndexOfTarget = original.lastIndexOf(target);
|
||||
|
||||
if (lastIndexOfTarget == -1) {
|
||||
return original;
|
||||
} else {
|
||||
return new StringBuilder(original)
|
||||
.replace(lastIndexOfTarget, lastIndexOfTarget + target.length(), replacement)
|
||||
.toString();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a Date out of a String with a date in W3C date-time format.
|
||||
* <p/>
|
||||
|
|
|
@ -16,6 +16,9 @@
|
|||
*/
|
||||
package com.rometools.rome.unittest;
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.GregorianCalendar;
|
||||
|
@ -125,6 +128,39 @@ public class TestDateParser extends TestCase {
|
|||
// INVALID
|
||||
sDate = "X20:10 2000-10-10";
|
||||
assertNull(DateParser.parseDate(sDate, Locale.US));
|
||||
}
|
||||
|
||||
public void testRfc822WithZTimeZone() throws Exception {
|
||||
assertDateEquals(
|
||||
"1970-01-01 00:00:00",
|
||||
DateParser.parseRFC822("Thu, 01 Jan 1970 00:00:00 Z", Locale.US));
|
||||
}
|
||||
|
||||
public void testRfc822WithUtTimeZone() throws Exception {
|
||||
assertDateEquals(
|
||||
"1970-01-01 00:00:00",
|
||||
DateParser.parseRFC822("Thu, 01 Jan 1970 00:00:00 UT", Locale.US));
|
||||
}
|
||||
|
||||
public void testRfc822WithUtcTimeZone() throws Exception {
|
||||
assertDateEquals(
|
||||
"1970-01-01 00:00:00",
|
||||
DateParser.parseRFC822("Thu, 01 Jan 1970 00:00:00 UTC", Locale.US));
|
||||
}
|
||||
|
||||
static void assertDateEquals(String expectedString, Date actual) {
|
||||
if (actual == null) {
|
||||
fail("Actual date is null");
|
||||
}
|
||||
|
||||
final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
|
||||
|
||||
try {
|
||||
final Date expected = dateFormat.parse(expectedString);
|
||||
assertEquals(expected, actual);
|
||||
} catch (ParseException e) {
|
||||
fail("Failed to parse date: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue