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