New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting DateFormatException when using HAPI FHIR 1.6 or 2.0 or latest snapshot #444

Closed
CarthageKing opened this Issue Sep 9, 2016 · 3 comments

Comments

Projects
None yet
2 participants
@CarthageKing

CarthageKing commented Sep 9, 2016

Hi,

The following code will generate a DiagnosticReport XML string and then attempts to convert that back into the FHIR object.

        LocalDateTime ldt = LocalDateTime.of(1960, 9, 7, 0, 44, 25, 12387401);
        DiagnosticReport dr = new DiagnosticReport();
        // HAPI FHIR 1.4 and 1.5 generate a valid timestamp, but 1.6 and 2.0 generated an invalid timestamp
        dr.setIssued(Date.from(ldt.toInstant(ZoneOffset.UTC)));

        FhirContext fctx = FhirContext.forDstu3();
        IParser xmlParser = fctx.newXmlParser().setPrettyPrint(true);

        String xml = xmlParser.encodeResourceToString(dr);
        System.out.println(xml);

        IBaseResource ibr = xmlParser.parseResource(xml);

Using HAPI FHIR 1.6 or 2.0 or the latest snapshot available, I'm getting a DateFormatException when deserializing the XML string to the FHIR object.

<DiagnosticReport xmlns="http://hl7.org/fhir">
   <issued value="1960-09-06T19:44:25.-988-05:00"/>
</DiagnosticReport>
Exception in thread "main" ca.uhn.fhir.parser.DataFormatException: DataFormatException at [[row,col {unknown-source}]: [2,4]]: Invalid date/time format: "1960-09-06T19:44:25.-988-05:00": Timezone offset must be in the form "Z", "-HH:mm", or "+HH:mm"
    at ca.uhn.fhir.parser.XmlParser.doXmlLoop(XmlParser.java:268)
    at ca.uhn.fhir.parser.XmlParser.parseResource(XmlParser.java:1045)
    at ca.uhn.fhir.parser.XmlParser.doParseResource(XmlParser.java:187)
    at ca.uhn.fhir.parser.BaseParser.parseResource(BaseParser.java:641)
    at ca.uhn.fhir.parser.BaseParser.parseResource(BaseParser.java:689)
    at ca.uhn.fhir.parser.BaseParser.parseResource(BaseParser.java:699)
    at com.accenture.ips.fhir.ingest.Test.main(Test.java:29)
Caused by: ca.uhn.fhir.parser.DataFormatException: Invalid date/time format: "1960-09-06T19:44:25.-988-05:00": Timezone offset must be in the form "Z", "-HH:mm", or "+HH:mm"
    at org.hl7.fhir.dstu3.model.BaseDateTimeType.throwBadDateFormat(BaseDateTimeType.java:449)
    at org.hl7.fhir.dstu3.model.BaseDateTimeType.setTimeZone(BaseDateTimeType.java:378)
    at org.hl7.fhir.dstu3.model.BaseDateTimeType.parse(BaseDateTimeType.java:281)
    at org.hl7.fhir.dstu3.model.BaseDateTimeType.parse(BaseDateTimeType.java:17)
    at org.hl7.fhir.dstu3.model.PrimitiveType.fromStringValue(PrimitiveType.java:77)
    at org.hl7.fhir.dstu3.model.PrimitiveType.setValueAsString(PrimitiveType.java:134)
    at org.hl7.fhir.dstu3.model.BaseDateTimeType.setValueAsString(BaseDateTimeType.java:441)
    at ca.uhn.fhir.parser.ParserState$PrimitiveState.attributeValue(ParserState.java:2349)
    at ca.uhn.fhir.parser.ParserState.attributeValue(ParserState.java:117)
    at ca.uhn.fhir.parser.XmlParser.doXmlLoop(XmlParser.java:234)
    ... 6 more

The issue does not exist in HAPI FHIR 1.4 or 1.5.

I attached a Maven project to reproduce the issue.
hapi-date-parse-test.zip

@jamesagnew

This comment has been minimized.

Show comment
Hide comment
@jamesagnew

jamesagnew Sep 9, 2016

Owner

The serialized date is definitely not correct in that block (not the
"-" in the milliseconds part).

Is that what's being produced by
"Date.from(ldt.toInstant(ZoneOffset.UTC))"? If so, I don't think this
is a HAPI issue..

Cheers,
James

On Fri, Sep 9, 2016 at 5:01 PM, CarthageKing notifications@github.com
wrote:

Hi,

The following code will generate a DiagnosticReport XML string and then
attempts to convert that back into the FHIR object.

    LocalDateTime ldt = LocalDateTime.of(1960, 9, 7, 0, 44, 25, 12387401);
    DiagnosticReport dr = new DiagnosticReport();
    // HAPI FHIR 1.4 and 1.5 generate a valid timestamp, but 1.6 and 2.0 generated an invalid timestamp
    dr.setIssued(Date.from(ldt.toInstant(ZoneOffset.UTC)));

    FhirContext fctx = FhirContext.forDstu3();
    IParser xmlParser = fctx.newXmlParser().setPrettyPrint(true);

    String xml = xmlParser.encodeResourceToString(dr);
    System.out.println(xml);

    IBaseResource ibr = xmlParser.parseResource(xml);

Using HAPI FHIR 1.6 or 2.0 or the latest snapshot available, I'm getting a
DateFormatException when deserializing the XML string to the FHIR object.

Exception in thread "main" ca.uhn.fhir.parser.DataFormatException: DataFormatException at [[row,col {unknown-source}]: [2,4]]: Invalid date/time format: "1960-09-06T19:44:25.-988-05:00": Timezone offset must be in the form "Z", "-HH:mm", or "+HH:mm" at ca.uhn.fhir.parser.XmlParser.doXmlLoop(XmlParser.java:268) at ca.uhn.fhir.parser.XmlParser.parseResource(XmlParser.java:1045) at ca.uhn.fhir.parser.XmlParser.doParseResource(XmlParser.java:187) at ca.uhn.fhir.parser.BaseParser.parseResource(BaseParser.java:641) at ca.uhn.fhir.parser.BaseParser.parseResource(BaseParser.java:689) at ca.uhn.fhir.parser.BaseParser.parseResource(BaseParser.java:699) at com.accenture.ips.fhir.ingest.Test.main(Test.java:29) Caused by: ca.uhn.fhir.parser.DataFormatException: Invalid date/time format: "1960-09-06T19:44:25.-988-05:00": Timezone offset must be in the form "Z", "-HH:mm", or "+HH:mm" at org.hl7.fhir.dstu3.model.BaseDateTimeType.throwBadDateFormat(BaseDateTimeType.java:449) at org.hl7.fhir.dstu3.model.BaseDateTimeType.setTimeZone(BaseDateTimeType.java:378) at org.hl7.fhir.dstu3.model.BaseDateTimeType.parse(BaseDateTimeType.java:281) at org.hl7.fhir.dstu3.model.BaseDateTimeType.parse(BaseDateTimeType.java:17) at org.hl7.fhir.dstu3.model.PrimitiveType.fromStringValue(PrimitiveType.java:77) at org.hl7.fhir.dstu3.model.PrimitiveType.setValueAsString(PrimitiveType.java:134) at org.hl7.fhir.dstu3.model.BaseDateTimeType.setValueAsString(BaseDateTimeType.java:441) at ca.uhn.fhir.parser.ParserState$PrimitiveState.attributeValue(ParserState.java:2349) at ca.uhn.fhir.parser.ParserState.attributeValue(ParserState.java:117) at ca.uhn.fhir.parser.XmlParser.doXmlLoop(XmlParser.java:234) ... 6 more

The issue does not exist in HAPI FHIR 1.4 or 1.5.

I attached a Maven project to reproduce the issue.
hapi-date-parse-test.zip
https://github.com/jamesagnew/hapi-fhir/files/464828/hapi-date-parse-test.zip


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
#444, or mute the thread
https://github.com/notifications/unsubscribe-auth/ADTfncKuDIM4bwli8jQ2OD5NdLeqgkEJks5qockZgaJpZM4J5dcg
.

Owner

jamesagnew commented Sep 9, 2016

The serialized date is definitely not correct in that block (not the
"-" in the milliseconds part).

Is that what's being produced by
"Date.from(ldt.toInstant(ZoneOffset.UTC))"? If so, I don't think this
is a HAPI issue..

Cheers,
James

On Fri, Sep 9, 2016 at 5:01 PM, CarthageKing notifications@github.com
wrote:

Hi,

The following code will generate a DiagnosticReport XML string and then
attempts to convert that back into the FHIR object.

    LocalDateTime ldt = LocalDateTime.of(1960, 9, 7, 0, 44, 25, 12387401);
    DiagnosticReport dr = new DiagnosticReport();
    // HAPI FHIR 1.4 and 1.5 generate a valid timestamp, but 1.6 and 2.0 generated an invalid timestamp
    dr.setIssued(Date.from(ldt.toInstant(ZoneOffset.UTC)));

    FhirContext fctx = FhirContext.forDstu3();
    IParser xmlParser = fctx.newXmlParser().setPrettyPrint(true);

    String xml = xmlParser.encodeResourceToString(dr);
    System.out.println(xml);

    IBaseResource ibr = xmlParser.parseResource(xml);

Using HAPI FHIR 1.6 or 2.0 or the latest snapshot available, I'm getting a
DateFormatException when deserializing the XML string to the FHIR object.

Exception in thread "main" ca.uhn.fhir.parser.DataFormatException: DataFormatException at [[row,col {unknown-source}]: [2,4]]: Invalid date/time format: "1960-09-06T19:44:25.-988-05:00": Timezone offset must be in the form "Z", "-HH:mm", or "+HH:mm" at ca.uhn.fhir.parser.XmlParser.doXmlLoop(XmlParser.java:268) at ca.uhn.fhir.parser.XmlParser.parseResource(XmlParser.java:1045) at ca.uhn.fhir.parser.XmlParser.doParseResource(XmlParser.java:187) at ca.uhn.fhir.parser.BaseParser.parseResource(BaseParser.java:641) at ca.uhn.fhir.parser.BaseParser.parseResource(BaseParser.java:689) at ca.uhn.fhir.parser.BaseParser.parseResource(BaseParser.java:699) at com.accenture.ips.fhir.ingest.Test.main(Test.java:29) Caused by: ca.uhn.fhir.parser.DataFormatException: Invalid date/time format: "1960-09-06T19:44:25.-988-05:00": Timezone offset must be in the form "Z", "-HH:mm", or "+HH:mm" at org.hl7.fhir.dstu3.model.BaseDateTimeType.throwBadDateFormat(BaseDateTimeType.java:449) at org.hl7.fhir.dstu3.model.BaseDateTimeType.setTimeZone(BaseDateTimeType.java:378) at org.hl7.fhir.dstu3.model.BaseDateTimeType.parse(BaseDateTimeType.java:281) at org.hl7.fhir.dstu3.model.BaseDateTimeType.parse(BaseDateTimeType.java:17) at org.hl7.fhir.dstu3.model.PrimitiveType.fromStringValue(PrimitiveType.java:77) at org.hl7.fhir.dstu3.model.PrimitiveType.setValueAsString(PrimitiveType.java:134) at org.hl7.fhir.dstu3.model.BaseDateTimeType.setValueAsString(BaseDateTimeType.java:441) at ca.uhn.fhir.parser.ParserState$PrimitiveState.attributeValue(ParserState.java:2349) at ca.uhn.fhir.parser.ParserState.attributeValue(ParserState.java:117) at ca.uhn.fhir.parser.XmlParser.doXmlLoop(XmlParser.java:234) ... 6 more

The issue does not exist in HAPI FHIR 1.4 or 1.5.

I attached a Maven project to reproduce the issue.
hapi-date-parse-test.zip
https://github.com/jamesagnew/hapi-fhir/files/464828/hapi-date-parse-test.zip


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
#444, or mute the thread
https://github.com/notifications/unsubscribe-auth/ADTfncKuDIM4bwli8jQ2OD5NdLeqgkEJks5qockZgaJpZM4J5dcg
.

@CarthageKing

This comment has been minimized.

Show comment
Hide comment
@CarthageKing

CarthageKing Sep 9, 2016

Hello James,

Date.from... is a standard Java library call so I don't think it's a bug in the Java library itself.

I noticed that the internals of the BaseDateTimeType have been updated from 1.4/1.5 to 1.6/newer. I traced the execution and I'm getting the -988 value in this line in BaseDateTimeType.setValue(Date theValue, TemporalPrecisionEnum thePrecision):

    if (theValue != null) {
        String fractionalSeconds = Integer.toString((int) (theValue.getTime() % 1000));
        myFractionalSeconds = StringUtils.leftPad(fractionalSeconds, 3, '0');
    }

The value returned by theValue.getTime() is a negative number and this leads to the negative milliseconds value being used by the BaseDateTimeType.

CarthageKing commented Sep 9, 2016

Hello James,

Date.from... is a standard Java library call so I don't think it's a bug in the Java library itself.

I noticed that the internals of the BaseDateTimeType have been updated from 1.4/1.5 to 1.6/newer. I traced the execution and I'm getting the -988 value in this line in BaseDateTimeType.setValue(Date theValue, TemporalPrecisionEnum thePrecision):

    if (theValue != null) {
        String fractionalSeconds = Integer.toString((int) (theValue.getTime() % 1000));
        myFractionalSeconds = StringUtils.leftPad(fractionalSeconds, 3, '0');
    }

The value returned by theValue.getTime() is a negative number and this leads to the negative milliseconds value being used by the BaseDateTimeType.

@jamesagnew

This comment has been minimized.

Show comment
Hide comment
@jamesagnew

jamesagnew Sep 9, 2016

Owner

Oh! You're right actually. This is a bug in our handling of values with milliseconds before 1970. Yikes.

I'll commit a fix shortly. Thanks for reporting!

Owner

jamesagnew commented Sep 9, 2016

Oh! You're right actually. This is a bug in our handling of values with milliseconds before 1970. Yikes.

I'll commit a fix shortly. Thanks for reporting!

@jamesagnew jamesagnew closed this in a2ffc6a Sep 9, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment