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.dstu3.model;
031
032import java.util.Calendar;
033import java.util.Date;
034import java.util.TimeZone;
035import java.util.zip.DataFormatException;
036
037import org.apache.commons.lang3.time.DateUtils;
038
039import ca.uhn.fhir.model.api.annotation.DatatypeDef;
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                return new DateTimeType(getValueAsString());
163        }
164
165        /**
166         * Creates a new instance by parsing an HL7 v3 format date time string
167         */
168        public static DateTimeType parseV3(String theV3String) {
169                DateTimeType retVal = new DateTimeType();
170                retVal.setValueAsV3String(theV3String);
171                return retVal;
172        }
173
174        public static DateTimeType today() {
175                DateTimeType retVal = now();
176                retVal.setPrecision(TemporalPrecisionEnum.DAY);
177                return retVal;
178        }
179
180        public boolean getTzSign() {
181                return getTimeZone().getRawOffset() >= 0;
182        }
183
184        public int getTzHour() {
185                return (int) (getTimeZone().getRawOffset() / DateUtils.MILLIS_PER_MINUTE) / 60;
186        }
187
188        public int getTzMin() {
189                return (int) (getTimeZone().getRawOffset() / DateUtils.MILLIS_PER_MINUTE) % 60;
190        }
191
192        
193        public String fhirType() {
194                return "dateTime";              
195        }
196}