Commit de135289 authored by R.W.Majeed's avatar R.W.Majeed

rewrote parsing to allow time zone offset at any time accuracy

parent 758fb772
......@@ -7,8 +7,11 @@ import java.time.format.DateTimeParseException;
import java.time.format.ResolverStyle;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAccessor;
import java.time.temporal.UnsupportedTemporalTypeException;
import org.junit.Assert;
import static org.junit.Assert.*;
import org.junit.Test;
public class TestDateTimeAccuracy {
......@@ -34,10 +37,62 @@ public class TestDateTimeAccuracy {
a = DateTimeAccuracy.parse(formatter, "01.02.2003 13");
Assert.assertEquals(ChronoUnit.HOURS, a.getAccuracy());
Assert.assertEquals("2003-02-01T13", a.toPartialIso8601(null));
Assert.assertEquals("2003-02-01T13+08:00", a.toPartialIso8601(tz));
Assert.assertEquals("2003-02-01T21+0800", a.toPartialIso8601(tz));
}
@Test
public void verifyDateTimeFormatter(){
TemporalAccessor a;
DateTimeFormatter f = DateTimeFormatter.ISO_DATE_TIME;
// zone offset missing, field should be not available
a = f.parse("2001-02-03T04:05:06");
Assert.assertFalse(a.isSupported(ChronoField.OFFSET_SECONDS));
try{
a.get(ChronoField.OFFSET_SECONDS);
Assert.fail("Expected exception not thrown");
}catch( UnsupportedTemporalTypeException e ){
// expected outcome
}
// zero zone offset, field should be available
a = f.parse("2001-02-03T04:05:06Z");
Assert.assertEquals(0, a.get(ChronoField.OFFSET_SECONDS));
a = f.parse("2001-02-03T04:05:06+00:00");
Assert.assertEquals(0, a.get(ChronoField.OFFSET_SECONDS));
a = f.parse("2001-02-03T04:05"); // seconds can be omitted
// test the partial timestamp formatter
f = DateTimeFormatter.ofPattern("u[-M[-d['T'H[:m[:s[.S]]][X]]]]");
}
@Test
public void verifyParsingIncompleteIsoTimestamp() throws ParseException{
DateTimeAccuracy a;
a = DateTimeAccuracy.parsePartialIso8601("2001");
assertEquals(ChronoUnit.YEARS, a.getAccuracy());
a = DateTimeAccuracy.parsePartialIso8601("2001-02");
assertEquals(ChronoUnit.MONTHS, a.getAccuracy());
a = DateTimeAccuracy.parsePartialIso8601("2001-02-03");
assertEquals(ChronoUnit.DAYS, a.getAccuracy());
a = DateTimeAccuracy.parsePartialIso8601("2001-02-03T04");
assertEquals(ChronoUnit.HOURS, a.getAccuracy());
a = DateTimeAccuracy.parsePartialIso8601("2001-02-03T04:05");
assertEquals(ChronoUnit.MINUTES, a.getAccuracy());
a = DateTimeAccuracy.parsePartialIso8601("2001-02-03T04:05:06");
assertEquals(ChronoUnit.SECONDS, a.getAccuracy());
// verify zone offset
// for second accuracy
a = DateTimeAccuracy.parsePartialIso8601("2001-02-03T04:05:06+0800");
assertEquals(ChronoUnit.SECONDS, a.getAccuracy());
// zone offset calculation
assertEquals(DateTimeAccuracy.parsePartialIso8601("2001-02-02T20:05:06Z"), a);
a = DateTimeAccuracy.parsePartialIso8601("2001-02-03T04+0800");
assertEquals(ChronoUnit.HOURS, a.getAccuracy());
a = DateTimeAccuracy.parsePartialIso8601("2001-02-03T04Z");
assertEquals(ChronoUnit.HOURS, a.getAccuracy());
a = DateTimeAccuracy.parsePartialIso8601("2001-02-03T04:05+0800");
assertEquals(ChronoUnit.MINUTES, a.getAccuracy());
}
@Test
public void testFormatExceedsText(){
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d.M.u[ H[:m[:s]]]");
......@@ -63,7 +118,7 @@ public class TestDateTimeAccuracy {
}
// TODO test more aspects of zone offset parsing
DateTimeAccuracy.parsePartialIso8601("2003-02-01T04:05:06Z");
DateTimeAccuracy a = DateTimeAccuracy.parsePartialIso8601("2003-02-01T04:05:06+01:00");
DateTimeAccuracy a = DateTimeAccuracy.parsePartialIso8601("2003-02-01T04:05:06+0100");
// make sure the date is adjusted to UTC
Assert.assertEquals(3, a.get(ChronoField.HOUR_OF_DAY));
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment