silently parse mildly incorrect dates

This commit is contained in:
Adrian Cole 2012-03-22 21:34:59 -07:00
parent 296462ecb5
commit 2cd11f6b18
4 changed files with 13 additions and 5 deletions

View File

@ -138,6 +138,8 @@ public class SimpleDateFormatDateService implements DateService {
toParse = trimToMillis(toParse);
toParse = trimTZ(toParse);
toParse += tz;
if (toParse.charAt(10) == ' ')
toParse = new StringBuilder(toParse).replace(10, 11, "T").toString();
synchronized (iso8601SimpleDateFormat) {
try {
return iso8601SimpleDateFormat.parse(toParse);

View File

@ -167,6 +167,11 @@ public class DateServiceTest extends PerformanceTest {
void testTz() throws ExecutionException, InterruptedException {
assertEquals(dateService.iso8601SecondsDateParse("2011-05-26T02:14:13-04:00").getTime(), 1306390453000l);
}
@Test
void testTzNoT() throws ExecutionException, InterruptedException {
assertEquals(dateService.iso8601DateParse("2011-05-25 16:12:21.656+0000").getTime(), 1306339941656l);
}
@Test
void testRfc822DateFormatResponseTime() throws ExecutionException, InterruptedException {

View File

@ -39,8 +39,7 @@ public class DateUtilsTest {
assertEquals("NO_MILLIS.123Z", DateUtils.trimToMillis("NO_MILLIS.12345690123345678Z"));
}
// TODO: this test is failing on my jvm which is in IST
@Test(enabled = false)
@Test
public void testTrimsToMillisNoTimezone() {
assertEquals("NO_MILLIS", DateUtils.trimToMillis("NO_MILLIS"));
assertEquals("NO_MILLIS.1", DateUtils.trimToMillis("NO_MILLIS.1"));

View File

@ -18,9 +18,9 @@
*/
package org.jclouds.date.joda;
import static org.jclouds.date.internal.DateUtils.*;
import static org.jclouds.date.internal.DateUtils.trimToMillis;
import static org.jclouds.date.internal.DateUtils.findTZ;
import static org.jclouds.date.internal.DateUtils.trimTZ;
import static org.jclouds.date.internal.DateUtils.trimToMillis;
import java.util.Date;
import java.util.Locale;
@ -40,7 +40,7 @@ import org.joda.time.format.DateTimeFormatter;
*/
@Singleton
public class JodaDateService implements DateService {
private static final DateTimeFormatter rfc822DateFormatter = DateTimeFormat.forPattern(
"EEE, dd MMM yyyy HH:mm:ss 'GMT'").withLocale(Locale.US).withZone(DateTimeZone.forID("GMT"));
@ -112,6 +112,8 @@ public class JodaDateService implements DateService {
toParse = trimToMillis(toParse);
toParse = trimTZ(toParse);
toParse += tz;
if (toParse.charAt(10) == ' ')
toParse = new StringBuilder(toParse).replace(10, 11, "T").toString();
return iso8601DateFormatter.parseDateTime(toParse).toDate();
}