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();
}
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();
}
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();
}
}
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);
}
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);
}
Aggregations