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 ca.uhn.fhir.model.api.TemporalPrecisionEnum;
033import ca.uhn.fhir.model.api.annotation.DatatypeDef;
034import org.apache.commons.lang3.time.DateUtils;
035
036import java.util.Calendar;
037import java.util.Date;
038import java.util.TimeZone;
039import java.util.zip.DataFormatException;
040
041/**
042 * Represents a FHIR dateTime datatype. Valid precisions values for this type are:
043 * <ul>
044 * <li>{@link TemporalPrecisionEnum#YEAR}
045 * <li>{@link TemporalPrecisionEnum#MONTH}
046 * <li>{@link TemporalPrecisionEnum#DAY}
047 * <li>{@link TemporalPrecisionEnum#SECOND}
048 * <li>{@link TemporalPrecisionEnum#MILLI}
049 * </ul>
050 */
051@DatatypeDef(name = "dateTime")
052public class DateTimeType extends BaseDateTimeType {
053
054        private static final long serialVersionUID = 3L;
055        
056        /**
057         * The default precision for this type
058         */
059        public static final TemporalPrecisionEnum DEFAULT_PRECISION = TemporalPrecisionEnum.SECOND;
060
061        /**
062         * Constructor
063         */
064        public DateTimeType() {
065                super();
066        }
067
068        /**
069         * Create a new DateTimeDt with seconds precision and the local time zone
070         */
071        public DateTimeType(Date theDate) {
072                super(theDate, DEFAULT_PRECISION, TimeZone.getDefault());
073        }
074
075        /**
076         * Constructor which accepts a date value and a precision value. Valid precisions values for this type are:
077         * <ul>
078         * <li>{@link TemporalPrecisionEnum#YEAR}
079         * <li>{@link TemporalPrecisionEnum#MONTH}
080         * <li>{@link TemporalPrecisionEnum#DAY}
081         * <li>{@link TemporalPrecisionEnum#SECOND}
082         * <li>{@link TemporalPrecisionEnum#MILLI}
083         * </ul>
084         * 
085         * @throws DataFormatException
086         *             If the specified precision is not allowed for this type
087         */
088        public DateTimeType(Date theDate, TemporalPrecisionEnum thePrecision) {
089                super(theDate, thePrecision, TimeZone.getDefault());
090        }
091
092        /**
093         * Create a new instance using a string date/time
094         * 
095         * @throws DataFormatException
096         *             If the specified precision is not allowed for this type
097         */
098        public DateTimeType(String theValue) {
099                super(theValue);
100        }
101
102        /**
103         * Constructor which accepts a date value, precision value, and time zone. Valid precisions values for this type
104         * are:
105         * <ul>
106         * <li>{@link TemporalPrecisionEnum#YEAR}
107         * <li>{@link TemporalPrecisionEnum#MONTH}
108         * <li>{@link TemporalPrecisionEnum#DAY}
109         * <li>{@link TemporalPrecisionEnum#SECOND}
110         * <li>{@link TemporalPrecisionEnum#MILLI}
111         * </ul>
112         */
113        public DateTimeType(Date theDate, TemporalPrecisionEnum thePrecision, TimeZone theTimezone) {
114                super(theDate, thePrecision, theTimezone);
115        }
116
117        /**
118         * Constructor
119         */
120        public DateTimeType(Calendar theCalendar) {
121                if (theCalendar != null) {
122                        setValue(theCalendar.getTime());
123                        setPrecision(DEFAULT_PRECISION);
124                        setTimeZone(theCalendar.getTimeZone());
125                }
126        }
127
128        @Override
129        boolean isPrecisionAllowed(TemporalPrecisionEnum thePrecision) {
130                switch (thePrecision) {
131                case YEAR:
132                case MONTH:
133                case DAY:
134                case SECOND:
135                case MILLI:
136                        return true;
137                default:
138                        return false;
139                }
140        }
141
142        /**
143         * Returns a new instance of DateTimeType with the current system time and SECOND precision and the system local time
144         * zone
145         */
146        public static DateTimeType now() {
147                return new DateTimeType(new Date(), TemporalPrecisionEnum.SECOND, TimeZone.getDefault());
148        }
149
150        /**
151         * Returns the default precision for this datatype
152         * 
153         * @see #DEFAULT_PRECISION
154         */
155        @Override
156        protected TemporalPrecisionEnum getDefaultPrecisionForDatatype() {
157                return DEFAULT_PRECISION;
158        }
159
160        @Override
161        public DateTimeType copy() {
162                DateTimeType ret = new DateTimeType(getValueAsString());
163    copyValues(ret);
164    return ret;
165        }
166
167        /**
168         * Creates a new instance by parsing an HL7 v3 format date time string
169         */
170        public static DateTimeType parseV3(String theV3String) {
171                DateTimeType retVal = new DateTimeType();
172                retVal.setValueAsV3String(theV3String);
173                return retVal;
174        }
175
176        public static DateTimeType today() {
177                DateTimeType retVal = now();
178                retVal.setPrecision(TemporalPrecisionEnum.DAY);
179                return retVal;
180        }
181
182        public boolean getTzSign() {
183                return getTimeZone().getRawOffset() >= 0;
184        }
185
186        public int getTzHour() {
187                return (int) (getTimeZone().getRawOffset() / DateUtils.MILLIS_PER_MINUTE) / 60;
188        }
189
190        public int getTzMin() {
191                return (int) (getTimeZone().getRawOffset() / DateUtils.MILLIS_PER_MINUTE) % 60;
192        }
193
194        
195        public String fhirType() {
196                return "dateTime";              
197        }
198
199  public String getAsV3() {
200    String r = getValueAsString();
201    r = stripChar(r, 16, ':');
202    r = stripChar(r, 13, ':');
203    r = stripChar(r, 10, 'T');
204    r = stripChar(r, 7, '-');
205    r = stripChar(r, 4, '-');
206    r = r.replaceAll(":", ""); // might be in the timezone
207    return r;
208  }
209
210  private String stripChar(String r, int i, char c) {
211    if (r.length() <= i || r.charAt(i) != c)
212      return r;
213    return r.substring(0, i)+r.substring(i+1);
214  }
215
216}