001package ca.uhn.fhir.model.primitive;
002
003/*
004 * #%L
005 * HAPI FHIR - Core Library
006 * %%
007 * Copyright (C) 2014 - 2021 Smile CDR, Inc.
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 *
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023import java.util.Date;
024import java.util.TimeZone;
025
026import ca.uhn.fhir.model.api.TemporalPrecisionEnum;
027import ca.uhn.fhir.model.api.annotation.DatatypeDef;
028import ca.uhn.fhir.model.api.annotation.SimpleSetter;
029import ca.uhn.fhir.parser.DataFormatException;
030
031/**
032 * Represents a FHIR dateTime datatype. Valid precisions values for this type are:
033 * <ul>
034 * <li>{@link TemporalPrecisionEnum#YEAR}
035 * <li>{@link TemporalPrecisionEnum#MONTH}
036 * <li>{@link TemporalPrecisionEnum#DAY}
037 * <li>{@link TemporalPrecisionEnum#SECOND}
038 * <li>{@link TemporalPrecisionEnum#MILLI}
039 * </ul>
040 */
041@DatatypeDef(name = "dateTime")
042public class DateTimeDt extends BaseDateTimeDt {
043
044        /**
045         * The default precision for this type
046         */
047        public static final TemporalPrecisionEnum DEFAULT_PRECISION = TemporalPrecisionEnum.SECOND;
048
049        /**
050         * Constructor
051         */
052        public DateTimeDt() {
053                super();
054        }
055
056        /**
057         * Create a new DateTimeDt with seconds precision and the local time zone
058         */
059        @SimpleSetter(suffix = "WithSecondsPrecision")
060        public DateTimeDt(@SimpleSetter.Parameter(name = "theDate") Date theDate) {
061                super(theDate, DEFAULT_PRECISION, TimeZone.getDefault());
062        }
063
064        /**
065         * Constructor which accepts a date value and a precision value. Valid precisions values for this type are:
066         * <ul>
067         * <li>{@link TemporalPrecisionEnum#YEAR}
068         * <li>{@link TemporalPrecisionEnum#MONTH}
069         * <li>{@link TemporalPrecisionEnum#DAY}
070         * <li>{@link TemporalPrecisionEnum#SECOND}
071         * <li>{@link TemporalPrecisionEnum#MILLI}
072         * </ul>
073         * 
074         * @throws DataFormatException
075         *             If the specified precision is not allowed for this type
076         */
077        @SimpleSetter
078        public DateTimeDt(@SimpleSetter.Parameter(name = "theDate") Date theDate, @SimpleSetter.Parameter(name = "thePrecision") TemporalPrecisionEnum thePrecision) {
079                super(theDate, thePrecision, TimeZone.getDefault());
080        }
081
082        /**
083         * Create a new instance using a string date/time
084         * 
085         * @throws DataFormatException
086         *             If the specified precision is not allowed for this type
087         */
088        public DateTimeDt(String theValue) {
089                super(theValue);
090        }
091
092        /**
093         * Constructor which accepts a date value, precision value, and time zone. Valid precisions values for this type
094         * are:
095         * <ul>
096         * <li>{@link TemporalPrecisionEnum#YEAR}
097         * <li>{@link TemporalPrecisionEnum#MONTH}
098         * <li>{@link TemporalPrecisionEnum#DAY}
099         * <li>{@link TemporalPrecisionEnum#SECOND}
100         * <li>{@link TemporalPrecisionEnum#MILLI}
101         * </ul>
102         */
103        public DateTimeDt(Date theDate, TemporalPrecisionEnum thePrecision, TimeZone theTimezone) {
104                super(theDate, thePrecision, theTimezone);
105        }
106
107        @Override
108        protected boolean isPrecisionAllowed(TemporalPrecisionEnum thePrecision) {
109                switch (thePrecision) {
110                case YEAR:
111                case MONTH:
112                case DAY:
113                case SECOND:
114                case MILLI:
115                        return true;
116                default:
117                        return false;
118                }
119        }
120
121        /**
122         * Returns a new instance of DateTimeDt with the current system time and SECOND precision and the system local time
123         * zone
124         */
125        public static DateTimeDt withCurrentTime() {
126                return new DateTimeDt(new Date(), TemporalPrecisionEnum.SECOND, TimeZone.getDefault());
127        }
128
129        /**
130         * Returns the default precision for this datatype
131         * 
132         * @see #DEFAULT_PRECISION
133         */
134        @Override
135        protected TemporalPrecisionEnum getDefaultPrecisionForDatatype() {
136                return DEFAULT_PRECISION;
137        }
138
139}