Commit 380bf45b authored by R.W.Majeed's avatar R.W.Majeed

timezone default added to date parsing

parent 1b7d7b48
...@@ -67,33 +67,33 @@ public class DateTimeAccuracy implements Comparable<DateTimeAccuracy> { ...@@ -67,33 +67,33 @@ public class DateTimeAccuracy implements Comparable<DateTimeAccuracy> {
this.accuracy = ChronoUnit.SECONDS; this.accuracy = ChronoUnit.SECONDS;
} }
@Deprecated @Deprecated
public DateTimeAccuracy(int year) { public DateTimeAccuracy(ZoneId zone, int year) {
instant = LocalDateTime.of(year, 1, 1, 0, 0).toInstant(ZoneOffset.UTC); instant = LocalDateTime.of(year, 1, 1, 0, 0).atZone(zone).toInstant();
accuracy = ChronoUnit.YEARS; accuracy = ChronoUnit.YEARS;
} }
@Deprecated @Deprecated
public DateTimeAccuracy(int year, int month) { public DateTimeAccuracy(ZoneId zone, int year, int month) {
instant = LocalDateTime.of(year, month, 1, 0, 0).toInstant(ZoneOffset.UTC); instant = LocalDateTime.of(year, month, 1, 0, 0).atZone(zone).toInstant();
accuracy = ChronoUnit.MONTHS; accuracy = ChronoUnit.MONTHS;
} }
@Deprecated @Deprecated
public DateTimeAccuracy(int year, int month, int day) { public DateTimeAccuracy(ZoneId zone, int year, int month, int day) {
instant = LocalDateTime.of(year, month, day, 0, 0).toInstant(ZoneOffset.UTC); instant = LocalDateTime.of(year, month, day, 0, 0).atZone(zone).toInstant();
accuracy = ChronoUnit.DAYS; accuracy = ChronoUnit.DAYS;
} }
@Deprecated @Deprecated
public DateTimeAccuracy(int year, int month, int day, int hours) { public DateTimeAccuracy(ZoneId zone, int year, int month, int day, int hours) {
instant = LocalDateTime.of(year, month, day, hours, 0).toInstant(ZoneOffset.UTC); instant = LocalDateTime.of(year, month, day, hours, 0).atZone(zone).toInstant();
accuracy = ChronoUnit.HOURS; accuracy = ChronoUnit.HOURS;
} }
@Deprecated @Deprecated
public DateTimeAccuracy(int year, int month, int day, int hours, int mins) { public DateTimeAccuracy(ZoneId zone, int year, int month, int day, int hours, int mins) {
instant = LocalDateTime.of(year, month, day, hours, mins).toInstant(ZoneOffset.UTC); instant = LocalDateTime.of(year, month, day, hours, mins).atZone(zone).toInstant();
accuracy = ChronoUnit.MINUTES; accuracy = ChronoUnit.MINUTES;
} }
@Deprecated @Deprecated
public DateTimeAccuracy(int year, int month, int day, int hours, int mins, int secs) { public DateTimeAccuracy(ZoneId zone, int year, int month, int day, int hours, int mins, int secs) {
instant = LocalDateTime.of(year, month, day, hours, mins, secs).toInstant(ZoneOffset.UTC); instant = LocalDateTime.of(year, month, day, hours, mins, secs).atZone(zone).toInstant();
accuracy = ChronoUnit.SECONDS; accuracy = ChronoUnit.SECONDS;
} }
...@@ -334,9 +334,10 @@ public class DateTimeAccuracy implements Comparable<DateTimeAccuracy> { ...@@ -334,9 +334,10 @@ public class DateTimeAccuracy implements Comparable<DateTimeAccuracy> {
* *
* @param formatter formatter * @param formatter formatter
* @param text input text * @param text input text
* @param zoneId time zone to use, if the parser doesn't supply a time zone or offset
* @return date time with accuracy * @return date time with accuracy
*/ */
public static DateTimeAccuracy parse(DateTimeFormatter formatter, CharSequence text){ public static DateTimeAccuracy parse(DateTimeFormatter formatter, CharSequence text, ZoneId zoneId){
ParsePosition pos = new ParsePosition(0); ParsePosition pos = new ParsePosition(0);
TemporalAccessor a = formatter.parseUnresolved(text, pos); TemporalAccessor a = formatter.parseUnresolved(text, pos);
if( pos.getErrorIndex() != -1 ){ if( pos.getErrorIndex() != -1 ){
...@@ -344,44 +345,53 @@ public class DateTimeAccuracy implements Comparable<DateTimeAccuracy> { ...@@ -344,44 +345,53 @@ public class DateTimeAccuracy implements Comparable<DateTimeAccuracy> {
}else if( pos.getIndex() != text.length() ){ }else if( pos.getIndex() != text.length() ){
throw new DateTimeParseException("Unparsed text found at index "+pos.getIndex(), text, pos.getIndex()); throw new DateTimeParseException("Unparsed text found at index "+pos.getIndex(), text, pos.getIndex());
} }
try{
int offset = a.get(ChronoField.OFFSET_SECONDS);
// explicit offset specified, use that information
zoneId = ZoneOffset.ofTotalSeconds(offset);
}catch( DateTimeException e ){
// no offset available
// use default specified in zoneId param
}
int year = a.get(ChronoField.YEAR); int year = a.get(ChronoField.YEAR);
// month // month
int month; int month;
try{ try{
month = a.get(ChronoField.MONTH_OF_YEAR); month = a.get(ChronoField.MONTH_OF_YEAR);
}catch( DateTimeException e ){ }catch( DateTimeException e ){
return new DateTimeAccuracy(year); return new DateTimeAccuracy(zoneId, year);
} }
int day; int day;
try{ try{
day = a.get(ChronoField.DAY_OF_MONTH); day = a.get(ChronoField.DAY_OF_MONTH);
}catch( DateTimeException e ){ }catch( DateTimeException e ){
return new DateTimeAccuracy(year,month); return new DateTimeAccuracy(zoneId, year,month);
} }
int hour; int hour;
try{ try{
hour = a.get(ChronoField.HOUR_OF_DAY); hour = a.get(ChronoField.HOUR_OF_DAY);
}catch( DateTimeException e ){ }catch( DateTimeException e ){
return new DateTimeAccuracy(year,month,day); return new DateTimeAccuracy(zoneId, year,month,day);
} }
int minute; int minute;
try{ try{
minute = a.get(ChronoField.MINUTE_OF_HOUR); minute = a.get(ChronoField.MINUTE_OF_HOUR);
}catch( DateTimeException e ){ }catch( DateTimeException e ){
return new DateTimeAccuracy(year,month,day, hour); return new DateTimeAccuracy(zoneId, year,month,day, hour);
} }
int seconds; int seconds;
try{ try{
seconds = a.get(ChronoField.SECOND_OF_MINUTE); seconds = a.get(ChronoField.SECOND_OF_MINUTE);
}catch( DateTimeException e ){ }catch( DateTimeException e ){
return new DateTimeAccuracy(year,month,day, hour, minute); return new DateTimeAccuracy(zoneId, year,month,day, hour, minute);
} }
return new DateTimeAccuracy(year,month,day, hour, minute, seconds); return new DateTimeAccuracy(zoneId, year,month,day, hour, minute, seconds);
// milliseconds not supported for now // milliseconds not supported for now
} }
@Override @Override
......
...@@ -23,7 +23,7 @@ public class TestDateTimeAccuracy { ...@@ -23,7 +23,7 @@ public class TestDateTimeAccuracy {
public void testParseYYYYDD(){ public void testParseYYYYDD(){
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("M.u"); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("M.u");
String text = "02.2003"; String text = "02.2003";
DateTimeAccuracy a = DateTimeAccuracy.parse(formatter, text); DateTimeAccuracy a = DateTimeAccuracy.parse(formatter, text, ZoneOffset.UTC);
Assert.assertEquals(ChronoUnit.MONTHS,a.getAccuracy()); Assert.assertEquals(ChronoUnit.MONTHS,a.getAccuracy());
TemporalAccessor ac = a.toInstantMin().atOffset(ZoneOffset.UTC); TemporalAccessor ac = a.toInstantMin().atOffset(ZoneOffset.UTC);
Assert.assertEquals(2, ac.get(ChronoField.MONTH_OF_YEAR)); Assert.assertEquals(2, ac.get(ChronoField.MONTH_OF_YEAR));
...@@ -35,10 +35,10 @@ public class TestDateTimeAccuracy { ...@@ -35,10 +35,10 @@ public class TestDateTimeAccuracy {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d.M.u[ H[:m[:s]]]"); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d.M.u[ H[:m[:s]]]");
DateTimeAccuracy a; DateTimeAccuracy a;
ZoneId tz = ZoneId.of("Asia/Shanghai"); // China standard time ZoneId tz = ZoneId.of("Asia/Shanghai"); // China standard time
a = DateTimeAccuracy.parse(formatter, "01.02.2003"); a = DateTimeAccuracy.parse(formatter, "01.02.2003", ZoneOffset.UTC);
Assert.assertEquals("2003-02-01", a.toPartialIso8601(null)); Assert.assertEquals("2003-02-01", a.toPartialIso8601(null));
Assert.assertEquals("2003-02-01", a.toPartialIso8601(tz)); Assert.assertEquals("2003-02-01", a.toPartialIso8601(tz));
a = DateTimeAccuracy.parse(formatter, "01.02.2003 13"); a = DateTimeAccuracy.parse(formatter, "01.02.2003 13", ZoneOffset.UTC);
Assert.assertEquals(ChronoUnit.HOURS, a.getAccuracy()); Assert.assertEquals(ChronoUnit.HOURS, a.getAccuracy());
Assert.assertEquals("2003-02-01T13", a.toPartialIso8601(null)); Assert.assertEquals("2003-02-01T13", a.toPartialIso8601(null));
Assert.assertEquals("2003-02-01T21+0800", a.toPartialIso8601(tz)); Assert.assertEquals("2003-02-01T21+0800", a.toPartialIso8601(tz));
...@@ -103,13 +103,13 @@ public class TestDateTimeAccuracy { ...@@ -103,13 +103,13 @@ public class TestDateTimeAccuracy {
formatter.withResolverStyle(ResolverStyle.STRICT); formatter.withResolverStyle(ResolverStyle.STRICT);
DateTimeAccuracy a; DateTimeAccuracy a;
a = DateTimeAccuracy.parse(formatter, "01.02.2003"); a = DateTimeAccuracy.parse(formatter, "01.02.2003", ZoneOffset.UTC);
Assert.assertEquals(ChronoUnit.DAYS,a.getAccuracy()); Assert.assertEquals(ChronoUnit.DAYS,a.getAccuracy());
a = DateTimeAccuracy.parse(formatter, "01.02.2003 13"); a = DateTimeAccuracy.parse(formatter, "01.02.2003 13", ZoneOffset.UTC);
Assert.assertEquals(ChronoUnit.HOURS,a.getAccuracy()); Assert.assertEquals(ChronoUnit.HOURS,a.getAccuracy());
a = DateTimeAccuracy.parse(formatter, "01.02.2003 13:14"); a = DateTimeAccuracy.parse(formatter, "01.02.2003 13:14", ZoneOffset.UTC);
Assert.assertEquals(ChronoUnit.MINUTES,a.getAccuracy()); Assert.assertEquals(ChronoUnit.MINUTES,a.getAccuracy());
} }
...@@ -144,16 +144,16 @@ public class TestDateTimeAccuracy { ...@@ -144,16 +144,16 @@ public class TestDateTimeAccuracy {
} }
// verify same behavior for DateTimeAccurecy // verify same behavior for DateTimeAccurecy
// should not fail below // should not fail below
DateTimeAccuracy a = DateTimeAccuracy.parse(formatter, "2003"); DateTimeAccuracy a = DateTimeAccuracy.parse(formatter, "2003" ,ZoneOffset.UTC);
Assert.assertEquals(ChronoUnit.YEARS, a.getAccuracy()); Assert.assertEquals(ChronoUnit.YEARS, a.getAccuracy());
// next two calls should throw exceptions // next two calls should throw exceptions
try{ try{
DateTimeAccuracy.parse(formatter, "+"); DateTimeAccuracy.parse(formatter, "+", ZoneOffset.UTC);
Assert.fail("Exception unexpected input"); Assert.fail("Exception unexpected input");
}catch( DateTimeParseException e ){ }catch( DateTimeParseException e ){
} }
try{ try{
DateTimeAccuracy.parse(formatter, "2003+"); DateTimeAccuracy.parse(formatter, "2003+", ZoneOffset.UTC);
Assert.fail("Exception expected for unparsed text at end of input"); Assert.fail("Exception expected for unparsed text at end of input");
}catch( DateTimeParseException e ){ }catch( DateTimeParseException e ){
} }
......
...@@ -8,6 +8,7 @@ import java.io.StringWriter; ...@@ -8,6 +8,7 @@ import java.io.StringWriter;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.net.URL; import java.net.URL;
import java.time.Instant; import java.time.Instant;
import java.time.ZoneOffset;
import javax.xml.XMLConstants; import javax.xml.XMLConstants;
import javax.xml.bind.JAXB; import javax.xml.bind.JAXB;
...@@ -61,7 +62,7 @@ public class ObservationImplJAXBTest { ...@@ -61,7 +62,7 @@ public class ObservationImplJAXBTest {
o.conceptId = "C"+index; o.conceptId = "C"+index;
o.patientId = "P"+index; o.patientId = "P"+index;
o.encounterId = "E"+index; o.encounterId = "E"+index;
o.startTime = new DateTimeAccuracy(2015,1,1,index); o.startTime = new DateTimeAccuracy(ZoneOffset.UTC, 2015,1,1,index);
switch( index ){ switch( index ){
case 0: case 0:
// string value // string value
......
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