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        /**
055         * The default precision for this type
056         */
057        public static final TemporalPrecisionEnum DEFAULT_PRECISION = TemporalPrecisionEnum.SECOND;
058        private static final long serialVersionUID = 3L;
059
060        /**
061         * Constructor
062         */
063        public DateTimeType() {
064                super();
065        }
066
067        /**
068         * Create a new DateTimeDt with seconds precision and the local time zone
069         */
070        public DateTimeType(Date theDate) {
071                super(theDate, DEFAULT_PRECISION, TimeZone.getDefault());
072        }
073
074        /**
075         * Constructor which accepts a date value and a precision value. Valid precisions values for this type are:
076         * <ul>
077         * <li>{@link TemporalPrecisionEnum#YEAR}
078         * <li>{@link TemporalPrecisionEnum#MONTH}
079         * <li>{@link TemporalPrecisionEnum#DAY}
080         * <li>{@link TemporalPrecisionEnum#SECOND}
081         * <li>{@link TemporalPrecisionEnum#MILLI}
082         * </ul>
083         * 
084         * @throws DataFormatException
085         *             If the specified precision is not allowed for this type
086         */
087        public DateTimeType(Date theDate, TemporalPrecisionEnum thePrecision) {
088                super(theDate, thePrecision, TimeZone.getDefault());
089        }
090
091        /**
092         * Create a new instance using a string date/time
093         * 
094         * @throws DataFormatException
095         *             If the specified precision is not allowed for this type
096         */
097        public DateTimeType(String theValue) {
098                super(theValue);
099        }
100
101        /**
102         * Constructor which accepts a date value, precision value, and time zone. Valid precisions values for this type
103         * are:
104         * <ul>
105         * <li>{@link TemporalPrecisionEnum#YEAR}
106         * <li>{@link TemporalPrecisionEnum#MONTH}
107         * <li>{@link TemporalPrecisionEnum#DAY}
108         * <li>{@link TemporalPrecisionEnum#SECOND}
109         * <li>{@link TemporalPrecisionEnum#MILLI}
110         * </ul>
111         */
112        public DateTimeType(Date theDate, TemporalPrecisionEnum thePrecision, TimeZone theTimezone) {
113                super(theDate, thePrecision, theTimezone);
114        }
115
116        /**
117         * Constructor
118         */
119        public DateTimeType(Calendar theCalendar) {
120                if (theCalendar != null) {
121                        setValue(theCalendar.getTime());
122                        setPrecision(DEFAULT_PRECISION);
123                        setTimeZone(theCalendar.getTimeZone());
124                }
125        }
126
127        @Override
128        public DateTimeType copy() {
129                return new DateTimeType(getValueAsString());
130        }
131
132        public String fhirType() {
133                return "dateTime";
134        }
135
136        /**
137         * Returns the default precision for this datatype
138         * 
139         * @see #DEFAULT_PRECISION
140         */
141        @Override
142        protected TemporalPrecisionEnum getDefaultPrecisionForDatatype() {
143                return DEFAULT_PRECISION;
144        }
145
146        public int getTzHour() {
147                return (int) (getTimeZone().getRawOffset() / DateUtils.MILLIS_PER_MINUTE) / 60;
148        }
149
150        public int getTzMin() {
151                return (int) (getTimeZone().getRawOffset() / DateUtils.MILLIS_PER_MINUTE) % 60;
152        }
153
154        public boolean getTzSign() {
155                return getTimeZone().getRawOffset() >= 0;
156        }
157
158        @Override
159        boolean isPrecisionAllowed(TemporalPrecisionEnum thePrecision) {
160                switch (thePrecision) {
161                case YEAR:
162                case MONTH:
163                case DAY:
164                case SECOND:
165                case MILLI:
166                        return true;
167                default:
168                        return false;
169                }
170        }
171
172        /**
173         * Returns a new instance of DateTimeType with the current system time and SECOND precision and the system local time
174         * zone
175         */
176        public static DateTimeType now() {
177                return new DateTimeType(new Date(), TemporalPrecisionEnum.SECOND, TimeZone.getDefault());
178        }
179
180        /**
181         * Creates a new instance by parsing an HL7 v3 format date time string
182         */
183        public static DateTimeType parseV3(String theV3String) {
184                DateTimeType retVal = new DateTimeType();
185                retVal.setValueAsV3String(theV3String);
186                return retVal;
187        }
188
189        public static DateTimeType today() {
190                DateTimeType retVal = now();
191                retVal.setPrecision(TemporalPrecisionEnum.DAY);
192                return retVal;
193        }
194}