Search in sources :

Example 1 with TemporalPrecisionEnum

use of ca.uhn.fhir.model.api.TemporalPrecisionEnum in project org.hl7.fhir.core by hapifhir.

the class BaseDateTimeType method parse.

@Override
protected Date parse(String theValue) throws DataFormatException {
    Calendar cal = new GregorianCalendar(0, 0, 0);
    cal.setTimeZone(TimeZone.getDefault());
    String value = theValue;
    boolean fractionalSecondsSet = false;
    if (value.length() > 0 && (value.charAt(0) == ' ' || value.charAt(value.length() - 1) == ' ')) {
        value = value.trim();
    }
    int length = value.length();
    if (length == 0) {
        return null;
    }
    if (length < 4) {
        throwBadDateFormat(value);
    }
    TemporalPrecisionEnum precision = null;
    cal.set(Calendar.YEAR, parseInt(value, value.substring(0, 4), 0, 9999));
    precision = TemporalPrecisionEnum.YEAR;
    if (length > 4) {
        validateCharAtIndexIs(value, 4, '-');
        validateLengthIsAtLeast(value, 7);
        int monthVal = parseInt(value, value.substring(5, 7), 1, 12) - 1;
        cal.set(Calendar.MONTH, monthVal);
        precision = TemporalPrecisionEnum.MONTH;
        if (length > 7) {
            validateCharAtIndexIs(value, 7, '-');
            validateLengthIsAtLeast(value, 10);
            // for some reason getActualMaximum works incorrectly if date isn't set
            cal.set(Calendar.DATE, 1);
            int actualMaximum = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
            cal.set(Calendar.DAY_OF_MONTH, parseInt(value, value.substring(8, 10), 1, actualMaximum));
            precision = TemporalPrecisionEnum.DAY;
            if (length > 10) {
                validateLengthIsAtLeast(value, 16);
                // yyyy-mm-ddThh:mm:ss
                validateCharAtIndexIs(value, 10, 'T');
                int offsetIdx = getOffsetIndex(value);
                String time;
                if (offsetIdx == -1) {
                    // throwBadDateFormat(theValue);
                    // No offset - should this be an error?
                    time = value.substring(11);
                } else {
                    time = value.substring(11, offsetIdx);
                    String offsetString = value.substring(offsetIdx);
                    setTimeZone(value, offsetString);
                    cal.setTimeZone(getTimeZone());
                }
                int timeLength = time.length();
                validateCharAtIndexIs(value, 13, ':');
                cal.set(Calendar.HOUR_OF_DAY, parseInt(value, value.substring(11, 13), 0, 23));
                cal.set(Calendar.MINUTE, parseInt(value, value.substring(14, 16), 0, 59));
                precision = TemporalPrecisionEnum.MINUTE;
                if (timeLength > 5) {
                    validateLengthIsAtLeast(value, 19);
                    // yyyy-mm-ddThh:mm:ss
                    validateCharAtIndexIs(value, 16, ':');
                    cal.set(Calendar.SECOND, parseInt(value, value.substring(17, 19), 0, 59));
                    precision = TemporalPrecisionEnum.SECOND;
                    if (timeLength > 8) {
                        // yyyy-mm-ddThh:mm:ss.SSSS
                        validateCharAtIndexIs(value, 19, '.');
                        validateLengthIsAtLeast(value, 20);
                        int endIndex = getOffsetIndex(value);
                        if (endIndex == -1) {
                            endIndex = value.length();
                        }
                        int millis;
                        String millisString;
                        if (endIndex > 23) {
                            myFractionalSeconds = value.substring(20, endIndex);
                            fractionalSecondsSet = true;
                            endIndex = 23;
                            millisString = value.substring(20, endIndex);
                            millis = parseInt(value, millisString, 0, 999);
                        } else {
                            millisString = value.substring(20, endIndex);
                            millis = parseInt(value, millisString, 0, 999);
                            myFractionalSeconds = millisString;
                            fractionalSecondsSet = true;
                        }
                        if (millisString.length() == 1) {
                            millis = millis * 100;
                        } else if (millisString.length() == 2) {
                            millis = millis * 10;
                        }
                        cal.set(Calendar.MILLISECOND, millis);
                        precision = TemporalPrecisionEnum.MILLI;
                    }
                }
            }
        } else {
            cal.set(Calendar.DATE, 1);
        }
    } else {
        cal.set(Calendar.DATE, 1);
    }
    if (fractionalSecondsSet == false) {
        myFractionalSeconds = "";
    }
    if (precision == TemporalPrecisionEnum.MINUTE) {
        validatePrecisionAndThrowDataFormatException(value, precision);
    }
    myPrecision = precision;
    return cal.getTime();
}
Also used : TemporalPrecisionEnum(ca.uhn.fhir.model.api.TemporalPrecisionEnum) GregorianCalendar(java.util.GregorianCalendar) Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar)

Example 2 with TemporalPrecisionEnum

use of ca.uhn.fhir.model.api.TemporalPrecisionEnum in project org.hl7.fhir.core by hapifhir.

the class BaseDateTimeType method parse.

@Override
protected Date parse(String theValue) throws DataFormatException {
    Calendar cal = new GregorianCalendar(0, 0, 0);
    cal.setTimeZone(TimeZone.getDefault());
    String value = theValue;
    boolean fractionalSecondsSet = false;
    if (value.length() > 0 && (value.charAt(0) == ' ' || value.charAt(value.length() - 1) == ' ')) {
        value = value.trim();
    }
    int length = value.length();
    if (length == 0) {
        return null;
    }
    if (length < 4) {
        throwBadDateFormat(value);
    }
    TemporalPrecisionEnum precision = null;
    cal.set(Calendar.YEAR, parseInt(value, value.substring(0, 4), 0, 9999));
    precision = TemporalPrecisionEnum.YEAR;
    if (length > 4) {
        validateCharAtIndexIs(value, 4, '-');
        validateLengthIsAtLeast(value, 7);
        int monthVal = parseInt(value, value.substring(5, 7), 1, 12) - 1;
        cal.set(Calendar.MONTH, monthVal);
        precision = TemporalPrecisionEnum.MONTH;
        if (length > 7) {
            validateCharAtIndexIs(value, 7, '-');
            validateLengthIsAtLeast(value, 10);
            // for some reason getActualMaximum works incorrectly if date isn't set
            cal.set(Calendar.DATE, 1);
            int actualMaximum = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
            cal.set(Calendar.DAY_OF_MONTH, parseInt(value, value.substring(8, 10), 1, actualMaximum));
            precision = TemporalPrecisionEnum.DAY;
            if (length > 10) {
                validateLengthIsAtLeast(value, 17);
                // yyyy-mm-ddThh:mm:ss
                validateCharAtIndexIs(value, 10, 'T');
                int offsetIdx = getOffsetIndex(value);
                String time;
                if (offsetIdx == -1) {
                    // throwBadDateFormat(theValue);
                    // No offset - should this be an error?
                    time = value.substring(11);
                } else {
                    time = value.substring(11, offsetIdx);
                    String offsetString = value.substring(offsetIdx);
                    setTimeZone(value, offsetString);
                    cal.setTimeZone(getTimeZone());
                }
                int timeLength = time.length();
                validateCharAtIndexIs(value, 13, ':');
                cal.set(Calendar.HOUR_OF_DAY, parseInt(value, value.substring(11, 13), 0, 23));
                cal.set(Calendar.MINUTE, parseInt(value, value.substring(14, 16), 0, 59));
                precision = TemporalPrecisionEnum.MINUTE;
                if (timeLength > 5) {
                    validateLengthIsAtLeast(value, 19);
                    // yyyy-mm-ddThh:mm:ss
                    validateCharAtIndexIs(value, 16, ':');
                    // note: this allows leap seconds
                    cal.set(Calendar.SECOND, parseInt(value, value.substring(17, 19), 0, 60));
                    precision = TemporalPrecisionEnum.SECOND;
                    if (timeLength > 8) {
                        // yyyy-mm-ddThh:mm:ss.SSSS
                        validateCharAtIndexIs(value, 19, '.');
                        validateLengthIsAtLeast(value, 20);
                        int endIndex = getOffsetIndex(value);
                        if (endIndex == -1) {
                            endIndex = value.length();
                        }
                        int millis;
                        String millisString;
                        if (endIndex > 23) {
                            myFractionalSeconds = value.substring(20, endIndex);
                            fractionalSecondsSet = true;
                            endIndex = 23;
                            millisString = value.substring(20, endIndex);
                            millis = parseInt(value, millisString, 0, 999);
                        } else {
                            millisString = value.substring(20, endIndex);
                            millis = parseInt(value, millisString, 0, 999);
                            myFractionalSeconds = millisString;
                            fractionalSecondsSet = true;
                        }
                        if (millisString.length() == 1) {
                            millis = millis * 100;
                        } else if (millisString.length() == 2) {
                            millis = millis * 10;
                        }
                        cal.set(Calendar.MILLISECOND, millis);
                        precision = TemporalPrecisionEnum.MILLI;
                    }
                }
            }
        } else {
            cal.set(Calendar.DATE, 1);
        }
    } else {
        cal.set(Calendar.DATE, 1);
    }
    if (fractionalSecondsSet == false) {
        myFractionalSeconds = "";
    }
    myPrecision = precision;
    return cal.getTime();
}
Also used : TemporalPrecisionEnum(ca.uhn.fhir.model.api.TemporalPrecisionEnum) GregorianCalendar(java.util.GregorianCalendar) Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar)

Example 3 with TemporalPrecisionEnum

use of ca.uhn.fhir.model.api.TemporalPrecisionEnum in project org.hl7.fhir.core by hapifhir.

the class FHIRPathEngine method processDateConstant.

private Base processDateConstant(Object appInfo, String value, ExpressionNode expr) throws PathEngineException {
    String date = null;
    String time = null;
    String tz = null;
    TemporalPrecisionEnum temp = null;
    if (value.startsWith("T")) {
        time = value.substring(1);
    } else if (!value.contains("T")) {
        date = value;
    } else {
        String[] p = value.split("T");
        date = p[0];
        if (p.length > 1) {
            time = p[1];
        }
    }
    if (time != null) {
        int i = time.indexOf("-");
        if (i == -1) {
            i = time.indexOf("+");
        }
        if (i == -1) {
            i = time.indexOf("Z");
        }
        if (i > -1) {
            tz = time.substring(i);
            time = time.substring(0, i);
        }
        if (time.length() == 2) {
            time = time + ":00:00";
            temp = TemporalPrecisionEnum.MINUTE;
        } else if (time.length() == 5) {
            temp = TemporalPrecisionEnum.MINUTE;
            time = time + ":00";
        } else if (time.contains(".")) {
            temp = TemporalPrecisionEnum.MILLI;
        } else {
            temp = TemporalPrecisionEnum.SECOND;
        }
    }
    if (date == null) {
        if (tz != null) {
            throw makeException(expr, I18nConstants.FHIRPATH_UNKNOWN_CONTEXT, value);
        } else {
            TimeType tt = new TimeType(time);
            tt.setPrecision(temp);
            return tt.noExtensions();
        }
    } else if (time != null) {
        DateTimeType dt = new DateTimeType(date + "T" + time + (tz == null ? "" : tz));
        dt.setPrecision(temp);
        return dt.noExtensions();
    } else {
        return new DateType(date).noExtensions();
    }
}
Also used : BaseDateTimeType(org.hl7.fhir.r4b.model.BaseDateTimeType) DateTimeType(org.hl7.fhir.r4b.model.DateTimeType) TemporalPrecisionEnum(ca.uhn.fhir.model.api.TemporalPrecisionEnum) DateType(org.hl7.fhir.r4b.model.DateType) BaseDateTimeType(org.hl7.fhir.r4b.model.BaseDateTimeType) TimeType(org.hl7.fhir.r4b.model.TimeType) DateTimeType(org.hl7.fhir.r4b.model.DateTimeType)

Example 4 with TemporalPrecisionEnum

use of ca.uhn.fhir.model.api.TemporalPrecisionEnum in project MobileAccessGateway by i4mi.

the class BaseQueryResponseConverter method transformToDate.

/**
 * XDS Timestamp -> FHIR Date
 * @param timestamp
 * @return
 */
public DateType transformToDate(Timestamp timestamp) {
    if (timestamp == null)
        return null;
    Date date = Date.from(timestamp.getDateTime().toInstant());
    Precision precision = timestamp.getPrecision();
    TemporalPrecisionEnum fhirPrecision;
    switch(precision) {
        case YEAR:
            fhirPrecision = TemporalPrecisionEnum.YEAR;
            break;
        case DAY:
            fhirPrecision = TemporalPrecisionEnum.DAY;
            break;
        // case SECOND: fhirPrecision = TemporalPrecisionEnum.SECOND;break;
        default:
            fhirPrecision = TemporalPrecisionEnum.DAY;
            break;
    }
    return new DateType(date, fhirPrecision);
}
Also used : Precision(org.openehealth.ipf.commons.ihe.xds.core.metadata.Timestamp.Precision) TemporalPrecisionEnum(ca.uhn.fhir.model.api.TemporalPrecisionEnum) DateType(org.hl7.fhir.r4.model.DateType) Date(java.util.Date)

Example 5 with TemporalPrecisionEnum

use of ca.uhn.fhir.model.api.TemporalPrecisionEnum in project MobileAccessGateway by i4mi.

the class BaseQueryResponseConverter method transform.

/**
 * XDS timestamp -> FHIR DateTime
 * @param timestamp
 * @return
 */
public DateTimeType transform(Timestamp timestamp) {
    if (timestamp == null)
        return null;
    Date date = Date.from(timestamp.getDateTime().toInstant());
    Precision precision = timestamp.getPrecision();
    TemporalPrecisionEnum fhirPrecision;
    switch(precision) {
        case YEAR:
            fhirPrecision = TemporalPrecisionEnum.YEAR;
            break;
        case DAY:
            fhirPrecision = TemporalPrecisionEnum.DAY;
            break;
        // There is no mapping for HOUR; MINUTE is not accepted
        case HOUR:
            fhirPrecision = TemporalPrecisionEnum.SECOND;
            break;
        case MINUTE:
            fhirPrecision = TemporalPrecisionEnum.SECOND;
            break;
        case SECOND:
            fhirPrecision = TemporalPrecisionEnum.SECOND;
            break;
        default:
            fhirPrecision = TemporalPrecisionEnum.MILLI;
            break;
    }
    return new DateTimeType(date, fhirPrecision);
}
Also used : DateTimeType(org.hl7.fhir.r4.model.DateTimeType) Precision(org.openehealth.ipf.commons.ihe.xds.core.metadata.Timestamp.Precision) TemporalPrecisionEnum(ca.uhn.fhir.model.api.TemporalPrecisionEnum) Date(java.util.Date)

Aggregations

TemporalPrecisionEnum (ca.uhn.fhir.model.api.TemporalPrecisionEnum)11 Calendar (java.util.Calendar)6 GregorianCalendar (java.util.GregorianCalendar)5 Date (java.util.Date)3 Precision (org.openehealth.ipf.commons.ihe.xds.core.metadata.Timestamp.Precision)2 DateAndListParam (ca.uhn.fhir.rest.param.DateAndListParam)1 DateOrListParam (ca.uhn.fhir.rest.param.DateOrListParam)1 DateParam (ca.uhn.fhir.rest.param.DateParam)1 ParamPrefixEnum (ca.uhn.fhir.rest.param.ParamPrefixEnum)1 StringAndListParam (ca.uhn.fhir.rest.param.StringAndListParam)1 StringParam (ca.uhn.fhir.rest.param.StringParam)1 TokenAndListParam (ca.uhn.fhir.rest.param.TokenAndListParam)1 TokenOrListParam (ca.uhn.fhir.rest.param.TokenOrListParam)1 TokenParam (ca.uhn.fhir.rest.param.TokenParam)1 InvalidRequestException (ca.uhn.fhir.rest.server.exceptions.InvalidRequestException)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 AD (net.ihe.gazelle.hl7v3.datatypes.AD)1 CD (net.ihe.gazelle.hl7v3.datatypes.CD)1 CE (net.ihe.gazelle.hl7v3.datatypes.CE)1 CS (net.ihe.gazelle.hl7v3.datatypes.CS)1