001package org.hl7.fhir.utilities;
002
003import java.util.Calendar;
004import java.util.Date;
005import java.util.Locale;
006import java.util.TimeZone;
007
008import org.apache.commons.lang3.time.FastDateFormat;
009
010import ca.uhn.fhir.model.api.TemporalPrecisionEnum;
011import org.hl7.fhir.exceptions.FHIRException;
012
013public class DateTimeUtil {
014
015  public static String toHumanDisplay(TimeZone theTimeZone, TemporalPrecisionEnum thePrecision, Date theValue, String theValueAsString) {
016    Calendar value = theTimeZone != null ? Calendar.getInstance(theTimeZone) : Calendar.getInstance();
017    value.setTime(theValue);
018
019    switch (thePrecision) {
020      case YEAR:
021      case MONTH:
022      case DAY:
023        return theValueAsString;
024      case MILLI:
025      case SECOND:
026      default:
027        return FastDateFormat.getDateTimeInstance(FastDateFormat.MEDIUM, FastDateFormat.MEDIUM).format(value);
028    }
029
030  }
031
032  public static String toHumanDisplayLocalTimezone(TemporalPrecisionEnum thePrecision, Date theValue, String theValueAsString) {
033    switch (thePrecision) {
034      case YEAR:
035      case MONTH:
036      case DAY:
037        return theValueAsString;
038      case MILLI:
039      case SECOND:
040      default:
041        return FastDateFormat.getDateTimeInstance(FastDateFormat.MEDIUM, FastDateFormat.MEDIUM).format(theValue);
042    }
043  }
044
045  public static String toHumanDisplay(Locale locale, TimeZone theTimeZone, TemporalPrecisionEnum thePrecision, Date theValue) {
046    Calendar value = theTimeZone != null ? Calendar.getInstance(theTimeZone) : Calendar.getInstance();
047    value.setTime(theValue);
048
049    FastDateFormat dateFormat = FastDateFormat.getDateInstance(FastDateFormat.MEDIUM, locale);
050    FastDateFormat dateTimeFormat = FastDateFormat.getDateTimeInstance(FastDateFormat.MEDIUM, FastDateFormat.MEDIUM, locale);
051
052    switch (thePrecision) {
053      case YEAR:
054      case MONTH:
055      case DAY:
056        return dateFormat.format(value);
057      case MILLI:
058      case SECOND:
059      default:
060        return dateTimeFormat.format(value);
061    }
062
063  }
064
065  public static String lowBoundaryForDate(String value, int precision) {
066    String[] res = splitTimezone(value);
067    StringBuilder b = new StringBuilder(res[0]);
068    if (b.length() == 4) {
069      b.append("-01");
070    }
071    if (b.length() == 7) {
072      b.append("-01");
073    }
074    if (b.length() == 10) {
075      b.append("T00:00");
076    }
077    if (b.length() == 16) {
078      b.append(":00");
079    }
080    if (b.length() == 19) {
081      b.append(".000");
082    }
083    String tz;
084    if (precision <= 10) {
085      tz = "";
086    } else {
087      tz = Utilities.noString(res[1]) ? defLowTimezone(b.toString()) : res[1];
088    }
089    return applyDatePrecision(b.toString(), precision)+tz;
090  }
091
092  private static String defLowTimezone(String string) {
093    return "+14:00"; // Kiribati permanent timezone since 1994 and we don't worry about before then
094  }
095
096  private static String defHighTimezone(String string) {
097    return "-12:00"; // Parts of Russia, Antarctica, Baker Island and Midway Atoll
098  }
099
100  public static String lowBoundaryForTime(String value, int precision) {
101    String[] res = splitTimezone(value);
102    StringBuilder b = new StringBuilder(res[0]);
103    if (b.length() == 2) {
104      b.append(":00");
105    }
106    if (b.length() == 5) {
107      b.append(":00");
108    }
109    if (b.length() == 8) {
110      b.append(".000");
111    }
112    return applyTimePrecision(b.toString(), precision)+res[1];
113  }
114
115  public static String highBoundaryForTime(String value, int precision) {
116    String[] res = splitTimezone(value);
117    StringBuilder b = new StringBuilder(res[0]);
118    if (b.length() == 2) {
119      b.append(":59");
120    }
121    if (b.length() == 5) {
122      b.append(":59");
123    }
124    if (b.length() == 8) {
125      b.append(".999");
126    }
127    return applyTimePrecision(b.toString(), precision)+res[1];
128  }
129
130  private static Object applyDatePrecision(String v, int precision) {
131    switch (precision) {
132    case 4:
133      return v.substring(0, 4);
134    case 6:
135    case 7:
136      return v.substring(0, 7);
137    case 8:
138    case 10:
139      return v.substring(0, 10);
140    case 14: return v.substring(0, 17);
141    case 17: return v;
142    }
143    throw new FHIRException("Unsupported Date precision for boundary operation: "+precision);
144  }
145
146  private static Object applyTimePrecision(String v, int precision) {
147    switch (precision) {
148    case 2: return v.substring(0, 3);
149    case 4: return v.substring(0, 6);
150    case 6: return v.substring(0, 9);
151    case 9: return v;
152    }
153    throw new FHIRException("Unsupported Time precision for boundary operation: "+precision);
154  }
155
156  public static String highBoundaryForDate(String value, int precision) {
157    String[] res = splitTimezone(value);
158    StringBuilder b = new StringBuilder(res[0]);
159    if (b.length() == 4) {
160      b.append("-12");
161    }
162    if (b.length() == 7) {
163      b.append("-"+ dayCount(Integer.parseInt(b.substring(0,4)), Integer.parseInt(b.substring(5,7))));
164    }
165    if (b.length() == 10) {
166      b.append("T23:59");
167    }
168    if (b.length() == 16) {
169      b.append(":59");
170    }
171    if (b.length() == 19) {
172      b.append(".999");
173    }
174    String tz;
175    if (precision <= 10) {
176      tz = "";
177    } else {
178      tz = Utilities.noString(res[1]) ? defHighTimezone(b.toString()) : res[1];
179    }
180    return applyDatePrecision(b.toString(), precision)+tz;
181  }
182
183  private static String[] splitTimezone(String value) {
184    String[] res = new String[2];
185
186    if (value.contains("+")) {
187      res[0] = value.substring(0, value.indexOf("+"));
188      res[1] = value.substring(value.indexOf("+"));
189    } else if (value.contains("-") && value.contains("T") && value.lastIndexOf("-") > value.indexOf("T")) {
190      res[0] = value.substring(0, value.lastIndexOf("-"));
191      res[1]  = value.substring(value.lastIndexOf("-"));
192    } else if (value.contains("Z")) {
193      res[0] = value.substring(0, value.indexOf("Z"));
194      res[1] = value.substring(value.indexOf("Z"));
195    } else {
196      res[0] = value;
197      res[1] = "";
198    }
199    return res;
200  }
201
202  public static Integer getDatePrecision(String value) {
203    return splitTimezone(value)[0].replace("-", "").replace("T", "").replace(":", "").replace(".", "").length();
204  }
205
206  public static Integer getTimePrecision(String value) {
207    return splitTimezone(value)[0].replace("T", "").replace(":", "").replace(".", "").length();
208  }
209
210  private static String dayCount(int y, int m) {
211    switch (m) {
212    case 1: return "31";
213    case 2: return ((y % 4 == 0) && (y % 400 == 0 || !(y % 100 == 0))) ? "29" : "28";
214    case 3: return "31";
215    case 4: return "30";
216    case 5: return "31";
217    case 6: return "30";
218    case 7: return "31";
219    case 8: return "31";
220    case 9: return "30";
221    case 10: return "31";
222    case 11: return "30";
223    case 12: return "31";
224    default: return "30"; // make the compiler happy
225    }
226  }
227}