001/* 002Copyright (c) 2011+, HL7, Inc 003All rights reserved. 004 005Redistribution and use in source and binary forms, with or without modification, 006are permitted provided that the following conditions are met: 007 008 * Redistributions of source code must retain the above copyright notice, this 009 list of conditions and the following disclaimer. 010 * Redistributions in binary form must reproduce the above copyright notice, 011 this list of conditions and the following disclaimer in the documentation 012 and/or other materials provided with the distribution. 013 * Neither the name of HL7 nor the names of its contributors may be used to 014 endorse or promote products derived from this software without specific 015 prior written permission. 016 017THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 018ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 019WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 020IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 021INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 022NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 023PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 024WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 025ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 026POSSIBILITY OF SUCH DAMAGE. 027 028*/ 029 030package org.hl7.fhir.r4.model; 031 032import java.util.Calendar; 033import java.util.Date; 034import java.util.TimeZone; 035import java.util.zip.DataFormatException; 036 037import ca.uhn.fhir.model.api.TemporalPrecisionEnum; 038import org.apache.commons.lang3.time.DateUtils; 039 040import ca.uhn.fhir.model.api.annotation.DatatypeDef; 041 042/** 043 * Represents a FHIR dateTime datatype. Valid precisions values for this type are: 044 * <ul> 045 * <li>{@link TemporalPrecisionEnum#YEAR} 046 * <li>{@link TemporalPrecisionEnum#MONTH} 047 * <li>{@link TemporalPrecisionEnum#DAY} 048 * <li>{@link TemporalPrecisionEnum#SECOND} 049 * <li>{@link TemporalPrecisionEnum#MILLI} 050 * </ul> 051 */ 052@DatatypeDef(name = "dateTime") 053public class DateTimeType extends BaseDateTimeType { 054 055 private static final long serialVersionUID = 3L; 056 057 /** 058 * The default precision for this type 059 */ 060 public static final TemporalPrecisionEnum DEFAULT_PRECISION = TemporalPrecisionEnum.SECOND; 061 062 /** 063 * Constructor 064 */ 065 public DateTimeType() { 066 super(); 067 } 068 069 /** 070 * Create a new DateTimeDt with seconds precision and the local time zone 071 */ 072 public DateTimeType(Date theDate) { 073 super(theDate, DEFAULT_PRECISION, TimeZone.getDefault()); 074 } 075 076 /** 077 * Constructor which accepts a date value and a precision value. Valid precisions values for this type are: 078 * <ul> 079 * <li>{@link TemporalPrecisionEnum#YEAR} 080 * <li>{@link TemporalPrecisionEnum#MONTH} 081 * <li>{@link TemporalPrecisionEnum#DAY} 082 * <li>{@link TemporalPrecisionEnum#SECOND} 083 * <li>{@link TemporalPrecisionEnum#MILLI} 084 * </ul> 085 * 086 * @throws DataFormatException 087 * If the specified precision is not allowed for this type 088 */ 089 public DateTimeType(Date theDate, TemporalPrecisionEnum thePrecision) { 090 super(theDate, thePrecision, TimeZone.getDefault()); 091 } 092 093 /** 094 * Create a new instance using a string date/time 095 * 096 * @throws DataFormatException 097 * If the specified precision is not allowed for this type 098 */ 099 public DateTimeType(String theValue) { 100 super(theValue); 101 } 102 103 /** 104 * Constructor which accepts a date value, precision value, and time zone. Valid precisions values for this type 105 * are: 106 * <ul> 107 * <li>{@link TemporalPrecisionEnum#YEAR} 108 * <li>{@link TemporalPrecisionEnum#MONTH} 109 * <li>{@link TemporalPrecisionEnum#DAY} 110 * <li>{@link TemporalPrecisionEnum#SECOND} 111 * <li>{@link TemporalPrecisionEnum#MILLI} 112 * </ul> 113 */ 114 public DateTimeType(Date theDate, TemporalPrecisionEnum thePrecision, TimeZone theTimezone) { 115 super(theDate, thePrecision, theTimezone); 116 } 117 118 /** 119 * Constructor 120 */ 121 public DateTimeType(Calendar theCalendar) { 122 if (theCalendar != null) { 123 setValue(theCalendar.getTime()); 124 setPrecision(DEFAULT_PRECISION); 125 setTimeZone(theCalendar.getTimeZone()); 126 } 127 } 128 129 @Override 130 boolean isPrecisionAllowed(TemporalPrecisionEnum thePrecision) { 131 switch (thePrecision) { 132 case YEAR: 133 case MONTH: 134 case DAY: 135 case SECOND: 136 case MILLI: 137 return true; 138 default: 139 return false; 140 } 141 } 142 143 /** 144 * Returns a new instance of DateTimeType with the current system time and SECOND precision and the system local time 145 * zone 146 */ 147 public static DateTimeType now() { 148 return new DateTimeType(new Date(), TemporalPrecisionEnum.SECOND, TimeZone.getDefault()); 149 } 150 151 /** 152 * Returns the default precision for this datatype 153 * 154 * @see #DEFAULT_PRECISION 155 */ 156 @Override 157 protected TemporalPrecisionEnum getDefaultPrecisionForDatatype() { 158 return DEFAULT_PRECISION; 159 } 160 161 @Override 162 public DateTimeType copy() { 163 return new DateTimeType(getValueAsString()); 164 } 165 166 /** 167 * Creates a new instance by parsing an HL7 v3 format date time string 168 */ 169 public static DateTimeType parseV3(String theV3String) { 170 DateTimeType retVal = new DateTimeType(); 171 retVal.setValueAsV3String(theV3String); 172 return retVal; 173 } 174 175 public static DateTimeType today() { 176 DateTimeType retVal = now(); 177 retVal.setPrecision(TemporalPrecisionEnum.DAY); 178 return retVal; 179 } 180 181 public boolean getTzSign() { 182 return getTimeZone().getRawOffset() >= 0; 183 } 184 185 public int getTzHour() { 186 return (int) (getTimeZone().getRawOffset() / DateUtils.MILLIS_PER_MINUTE) / 60; 187 } 188 189 public int getTzMin() { 190 return (int) (getTimeZone().getRawOffset() / DateUtils.MILLIS_PER_MINUTE) % 60; 191 } 192 193 194 public String fhirType() { 195 return "dateTime"; 196 } 197 198 public String getAsV3() { 199 String r = getValueAsString(); 200 r = stripChar(r, 16, ':'); 201 r = stripChar(r, 13, ':'); 202 r = stripChar(r, 10, 'T'); 203 r = stripChar(r, 7, '-'); 204 r = stripChar(r, 4, '-'); 205 r = r.replaceAll(":", ""); // might be in the timezone 206 return r; 207 } 208 209 private String stripChar(String r, int i, char c) { 210 if (r.length() <= i || r.charAt(i) != c) 211 return r; 212 return r.substring(0, i)+r.substring(i+1); 213 } 214 215}