001package org.hl7.fhir.dstu2.model;
002
003/*
004  Copyright (c) 2011+, HL7, Inc.
005  All rights reserved.
006  
007  Redistribution and use in source and binary forms, with or without modification, 
008  are permitted provided that the following conditions are met:
009    
010   * Redistributions of source code must retain the above copyright notice, this 
011     list of conditions and the following disclaimer.
012   * Redistributions in binary form must reproduce the above copyright notice, 
013     this list of conditions and the following disclaimer in the documentation 
014     and/or other materials provided with the distribution.
015   * Neither the name of HL7 nor the names of its contributors may be used to 
016     endorse or promote products derived from this software without specific 
017     prior written permission.
018  
019  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
020  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
021  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
022  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
023  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
024  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
025  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
026  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
027  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
028  POSSIBILITY OF SUCH DAMAGE.
029  
030 */
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
043 * are:
044 * <ul>
045 * <li>{@link TemporalPrecisionEnum#YEAR}
046 * <li>{@link TemporalPrecisionEnum#MONTH}
047 * <li>{@link TemporalPrecisionEnum#DAY}
048 * <li>{@link TemporalPrecisionEnum#SECOND}
049 * <li>{@link TemporalPrecisionEnum#MILLI}
050 * </ul>
051 */
052@DatatypeDef(name = "dateTime")
053public class DateTimeType extends BaseDateTimeType {
054
055  private static final long serialVersionUID = 3L;
056
057  /**
058   * The default precision for this type
059   */
060  public static final TemporalPrecisionEnum DEFAULT_PRECISION = TemporalPrecisionEnum.SECOND;
061
062  /**
063   * Constructor
064   */
065  public DateTimeType() {
066    super();
067  }
068
069  /**
070   * Create a new DateTimeDt with seconds precision and the local time zone
071   */
072  public DateTimeType(Date theDate) {
073    super(theDate, DEFAULT_PRECISION, TimeZone.getDefault());
074  }
075
076  /**
077   * Constructor which accepts a date value and a precision value. Valid
078   * precisions values for this type are:
079   * <ul>
080   * <li>{@link TemporalPrecisionEnum#YEAR}
081   * <li>{@link TemporalPrecisionEnum#MONTH}
082   * <li>{@link TemporalPrecisionEnum#DAY}
083   * <li>{@link TemporalPrecisionEnum#SECOND}
084   * <li>{@link TemporalPrecisionEnum#MILLI}
085   * </ul>
086   * 
087   * @throws DataFormatException If the specified precision is not allowed for
088   *                             this type
089   */
090  public DateTimeType(Date theDate, TemporalPrecisionEnum thePrecision) {
091    super(theDate, thePrecision, TimeZone.getDefault());
092  }
093
094  /**
095   * Create a new instance using a string date/time
096   * 
097   * @throws DataFormatException If the specified precision is not allowed for
098   *                             this type
099   */
100  public DateTimeType(String theValue) {
101    super(theValue);
102  }
103
104  /**
105   * Constructor which accepts a date value, precision value, and time zone. Valid
106   * precisions values for this type are:
107   * <ul>
108   * <li>{@link TemporalPrecisionEnum#YEAR}
109   * <li>{@link TemporalPrecisionEnum#MONTH}
110   * <li>{@link TemporalPrecisionEnum#DAY}
111   * <li>{@link TemporalPrecisionEnum#SECOND}
112   * <li>{@link TemporalPrecisionEnum#MILLI}
113   * </ul>
114   */
115  public DateTimeType(Date theDate, TemporalPrecisionEnum thePrecision, TimeZone theTimezone) {
116    super(theDate, thePrecision, theTimezone);
117  }
118
119  /**
120   * Constructor
121   */
122  public DateTimeType(Calendar theCalendar) {
123    if (theCalendar != null) {
124      setValue(theCalendar.getTime());
125      setPrecision(DEFAULT_PRECISION);
126      setTimeZone(theCalendar.getTimeZone());
127    }
128  }
129
130  @Override
131  boolean isPrecisionAllowed(TemporalPrecisionEnum thePrecision) {
132    switch (thePrecision) {
133    case YEAR:
134    case MONTH:
135    case DAY:
136    case SECOND:
137    case MILLI:
138      return true;
139    default:
140      return false;
141    }
142  }
143
144  /**
145   * Returns a new instance of DateTimeType with the current system time and
146   * SECOND precision and the system local time zone
147   */
148  public static DateTimeType now() {
149    return new DateTimeType(new Date(), TemporalPrecisionEnum.SECOND, TimeZone.getDefault());
150  }
151
152  /**
153   * Returns the default precision for this datatype
154   * 
155   * @see #DEFAULT_PRECISION
156   */
157  @Override
158  protected TemporalPrecisionEnum getDefaultPrecisionForDatatype() {
159    return DEFAULT_PRECISION;
160  }
161
162  @Override
163  public DateTimeType copy() {
164    return new DateTimeType(getValueAsString());
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  public String fhirType() {
195    return "dateTime";
196  }
197}