mirror of https://github.com/apache/jclouds.git
Fixed bug with date handling with non-millisec/microsec precision value (e.g. .35123Z)
This commit is contained in:
parent
2367c14eed
commit
e6c0e5fe37
|
@ -27,13 +27,15 @@ import java.util.regex.Pattern;
|
||||||
*/
|
*/
|
||||||
public class DateUtils {
|
public class DateUtils {
|
||||||
|
|
||||||
public static final Pattern NANOS_TO_MILLIS_PATTERN = Pattern.compile(".*[0-9][0-9][0-9][0-9][0-9][0-9]");
|
public static final Pattern MILLIS_PATTERN = Pattern.compile("(.*\\.[0-9][0-9][0-9])[0-9]*Z?");
|
||||||
|
|
||||||
public static final Pattern TZ_PATTERN = Pattern.compile("(.*)[+-][0-9][0-9]:?[0-9][0-9]Z?");
|
public static final Pattern TZ_PATTERN = Pattern.compile("(.*)[+-][0-9][0-9]:?[0-9][0-9]Z?");
|
||||||
|
|
||||||
public static String trimNanosToMillis(String toParse) {
|
public static String trimToMillis(String toParse) {
|
||||||
if (NANOS_TO_MILLIS_PATTERN.matcher(toParse).matches())
|
Matcher matcher = MILLIS_PATTERN.matcher(toParse);
|
||||||
toParse = toParse.substring(0, toParse.length() - 3) + 'Z';
|
if (matcher.find()) {
|
||||||
|
toParse = matcher.group(1) + 'Z';
|
||||||
|
}
|
||||||
return toParse;
|
return toParse;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -17,7 +17,7 @@
|
||||||
* under the License.
|
* under the License.
|
||||||
*/
|
*/
|
||||||
package org.jclouds.date.internal;
|
package org.jclouds.date.internal;
|
||||||
import static org.jclouds.date.internal.DateUtils.trimNanosToMillis;
|
import static org.jclouds.date.internal.DateUtils.trimToMillis;
|
||||||
import static org.jclouds.date.internal.DateUtils.trimTZ;
|
import static org.jclouds.date.internal.DateUtils.trimTZ;
|
||||||
|
|
||||||
import java.text.ParseException;
|
import java.text.ParseException;
|
||||||
|
@ -124,7 +124,7 @@ public class SimpleDateFormatDateService implements DateService {
|
||||||
|
|
||||||
public final Date iso8601DateParse(String toParse) {
|
public final Date iso8601DateParse(String toParse) {
|
||||||
toParse = trimTZ(toParse);
|
toParse = trimTZ(toParse);
|
||||||
toParse = trimNanosToMillis(toParse);
|
toParse = trimToMillis(toParse);
|
||||||
synchronized (iso8601SimpleDateFormat) {
|
synchronized (iso8601SimpleDateFormat) {
|
||||||
try {
|
try {
|
||||||
return iso8601SimpleDateFormat.parse(toParse);
|
return iso8601SimpleDateFormat.parse(toParse);
|
||||||
|
|
|
@ -0,0 +1,55 @@
|
||||||
|
/**
|
||||||
|
* Licensed to jclouds, Inc. (jclouds) under one or more
|
||||||
|
* contributor license agreements. See the NOTICE file
|
||||||
|
* distributed with this work for additional information
|
||||||
|
* regarding copyright ownership. jclouds licenses this file
|
||||||
|
* to you under the Apache License, Version 2.0 (the
|
||||||
|
* "License"); you may not use this file except in compliance
|
||||||
|
* with the License. You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing,
|
||||||
|
* software distributed under the License is distributed on an
|
||||||
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
|
* KIND, either express or implied. See the License for the
|
||||||
|
* specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
*/
|
||||||
|
package org.jclouds.date.internal;
|
||||||
|
|
||||||
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
|
import static org.testng.AssertJUnit.assertEquals;
|
||||||
|
|
||||||
|
@Test(groups = "unit", testName = "DateUtilsTest")
|
||||||
|
public class DateUtilsTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testTrimsToMillisWithTimezone() {
|
||||||
|
assertEquals("NO_MILLISZ",DateUtils.trimToMillis("NO_MILLISZ"));
|
||||||
|
assertEquals("NO_MILLIS.1Z",DateUtils.trimToMillis("NO_MILLIS.1Z"));
|
||||||
|
assertEquals("NO_MILLIS.12Z",DateUtils.trimToMillis("NO_MILLIS.12Z"));
|
||||||
|
assertEquals("NO_MILLIS.123Z",DateUtils.trimToMillis("NO_MILLIS.123Z"));
|
||||||
|
assertEquals("NO_MILLIS.123Z",DateUtils.trimToMillis("NO_MILLIS.1234Z"));
|
||||||
|
assertEquals("NO_MILLIS.123Z",DateUtils.trimToMillis("NO_MILLIS.12345Z"));
|
||||||
|
assertEquals("NO_MILLIS.123Z",DateUtils.trimToMillis("NO_MILLIS.123456Z"));
|
||||||
|
assertEquals("NO_MILLIS.123Z",DateUtils.trimToMillis("NO_MILLIS.1234567Z"));
|
||||||
|
assertEquals("NO_MILLIS.123Z",DateUtils.trimToMillis("NO_MILLIS.12345689Z"));
|
||||||
|
assertEquals("NO_MILLIS.123Z",DateUtils.trimToMillis("NO_MILLIS.12345690123345678Z"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testTrimsToMillisNoTimezone() {
|
||||||
|
assertEquals("NO_MILLIS",DateUtils.trimToMillis("NO_MILLISZ"));
|
||||||
|
assertEquals("NO_MILLIS.1",DateUtils.trimToMillis("NO_MILLIS.1"));
|
||||||
|
assertEquals("NO_MILLIS.12",DateUtils.trimToMillis("NO_MILLIS.12"));
|
||||||
|
assertEquals("NO_MILLIS.123",DateUtils.trimToMillis("NO_MILLIS.123"));
|
||||||
|
assertEquals("NO_MILLIS.123",DateUtils.trimToMillis("NO_MILLIS.1234"));
|
||||||
|
assertEquals("NO_MILLIS.123",DateUtils.trimToMillis("NO_MILLIS.12345"));
|
||||||
|
assertEquals("NO_MILLIS.123",DateUtils.trimToMillis("NO_MILLIS.123456"));
|
||||||
|
assertEquals("NO_MILLIS.123",DateUtils.trimToMillis("NO_MILLIS.1234567"));
|
||||||
|
assertEquals("NO_MILLIS.123",DateUtils.trimToMillis("NO_MILLIS.12345689"));
|
||||||
|
assertEquals("NO_MILLIS.123",DateUtils.trimToMillis("NO_MILLIS.12345690123345678"));
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,41 @@
|
||||||
|
/**
|
||||||
|
* Licensed to jclouds, Inc. (jclouds) under one or more
|
||||||
|
* contributor license agreements. See the NOTICE file
|
||||||
|
* distributed with this work for additional information
|
||||||
|
* regarding copyright ownership. jclouds licenses this file
|
||||||
|
* to you under the Apache License, Version 2.0 (the
|
||||||
|
* "License"); you may not use this file except in compliance
|
||||||
|
* with the License. You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing,
|
||||||
|
* software distributed under the License is distributed on an
|
||||||
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
|
* KIND, either express or implied. See the License for the
|
||||||
|
* specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
*/
|
||||||
|
package org.jclouds.date.internal;
|
||||||
|
|
||||||
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
import static org.testng.AssertJUnit.assertEquals;
|
||||||
|
|
||||||
|
@Test(groups = "unit", testName = "SimpleDateFormatDateServiceTest")
|
||||||
|
public class SimpleDateFormatDateServiceTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCorrectHandlingOfMillis() {
|
||||||
|
Date date = new SimpleDateFormatDateService().iso8601DateParse("2011-11-07T11:19:13.38225Z");
|
||||||
|
assertEquals("Mon Nov 07 11:19:13 GMT 2011",date.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCorrectHandlingOfMillisWithNoTimezone() {
|
||||||
|
Date date = new SimpleDateFormatDateService().iso8601DateParse("2009-02-03T05:26:32.612278");
|
||||||
|
assertEquals("Tue Feb 03 05:26:32 GMT 2009",date.toString());
|
||||||
|
}
|
||||||
|
}
|
|
@ -18,7 +18,7 @@
|
||||||
*/
|
*/
|
||||||
package org.jclouds.date.joda;
|
package org.jclouds.date.joda;
|
||||||
|
|
||||||
import static org.jclouds.date.internal.DateUtils.trimNanosToMillis;
|
import static org.jclouds.date.internal.DateUtils.trimToMillis;
|
||||||
import static org.jclouds.date.internal.DateUtils.trimTZ;
|
import static org.jclouds.date.internal.DateUtils.trimTZ;
|
||||||
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
@ -98,7 +98,7 @@ public class JodaDateService implements DateService {
|
||||||
|
|
||||||
public final Date iso8601DateParse(String toParse) {
|
public final Date iso8601DateParse(String toParse) {
|
||||||
toParse = trimTZ(toParse);
|
toParse = trimTZ(toParse);
|
||||||
toParse = trimNanosToMillis(toParse);
|
toParse = trimToMillis(toParse);
|
||||||
return iso8601DateFormatter.parseDateTime(toParse).toDate();
|
return iso8601DateFormatter.parseDateTime(toParse).toDate();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue