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