aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/com/healthmarketscience/jackcess/expr/TemporalConfig.java
blob: cfe08e1cb073393c95c43c2054453c1916167244 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
Copyright (c) 2017 James Ahlborn

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package com.healthmarketscience.jackcess.expr;

import java.text.DateFormatSymbols;
import java.util.Locale;

/**
 * A TemporalConfig encapsulates date/time formatting options for expression
 * evaluation.  The default {@link #US_TEMPORAL_CONFIG} instance provides US
 * specific locale configuration.  Databases which have been built for other
 * locales can utilize custom implementations of TemporalConfig in order to
 * evaluate expressions correctly.
 *
 * @author James Ahlborn
 */
public class TemporalConfig
{
  public static final String US_DATE_FORMAT = "M/d[/uuuu]";
  public static final String US_TIME_FORMAT_12_FORMAT = "h:mm:ss a";
  public static final String US_TIME_FORMAT_24_FORMAT = "H:mm:ss";
  public static final String US_LONG_DATE_FORMAT = "EEEE, MMMM dd, uuuu";

  public static final String MEDIUM_DATE_FORMAT = "dd-MMM-uu";
  public static final String MEDIUM_TIME_FORMAT = "hh:mm a";
  public static final String SHORT_TIME_FORMAT = "HH:mm";

  /** default implementation which is configured for the US locale */
  public static final TemporalConfig US_TEMPORAL_CONFIG = new TemporalConfig(
      US_DATE_FORMAT, US_LONG_DATE_FORMAT,
      US_TIME_FORMAT_12_FORMAT, US_TIME_FORMAT_24_FORMAT, '/', ':', Locale.US);

  public enum Type {
    DATE, TIME, DATE_TIME, TIME_12, TIME_24, DATE_TIME_12, DATE_TIME_24,
    GENERAL_DATE, LONG_DATE, MEDIUM_DATE, SHORT_DATE,
    LONG_TIME, MEDIUM_TIME, SHORT_TIME;

    public Type getDefaultType() {
      switch(this) {
      case DATE:
      case LONG_DATE:
      case MEDIUM_DATE:
      case SHORT_DATE:
        return DATE;
      case TIME:
      case TIME_12:
      case TIME_24:
      case LONG_TIME:
      case MEDIUM_TIME:
      case SHORT_TIME:
        return TIME;
      case DATE_TIME:
      case DATE_TIME_12:
      case DATE_TIME_24:
      case GENERAL_DATE:
        return DATE_TIME;
      default:
        throw new RuntimeException("invalid type " + this);
      }
    }

    public Value.Type getValueType() {
      switch(this) {
      case DATE:
      case LONG_DATE:
      case MEDIUM_DATE:
      case SHORT_DATE:
        return Value.Type.DATE;
      case TIME:
      case TIME_12:
      case TIME_24:
      case LONG_TIME:
      case MEDIUM_TIME:
      case SHORT_TIME:
        return Value.Type.TIME;
      case DATE_TIME:
      case DATE_TIME_12:
      case DATE_TIME_24:
      case GENERAL_DATE:
        return Value.Type.DATE_TIME;
      default:
        throw new RuntimeException("invalid type " + this);
      }
    }

    public boolean includesTime() {
      return !isDateOnly();
    }

    public boolean includesDate() {
      return !isTimeOnly();
    }

    public boolean isDateOnly() {
      switch(this) {
      case DATE:
      case LONG_DATE:
      case MEDIUM_DATE:
      case SHORT_DATE:
        return true;
      default:
        return false;
      }
    }

    public boolean isTimeOnly() {
      switch(this) {
      case TIME:
      case TIME_12:
      case TIME_24:
      case LONG_TIME:
      case MEDIUM_TIME:
      case SHORT_TIME:
        return true;
      default:
        return false;
      }
    }
  }

  private final Locale _locale;
  private final String _dateFormat;
  private final String _longDateFormat;
  private final String _timeFormat12;
  private final String _timeFormat24;
  private final char _dateSeparator;
  private final char _timeSeparator;
  private final String _dateTimeFormat12;
  private final String _dateTimeFormat24;
  private final String[] _amPmStrings;

  /**
   * Instantiates a new TemporalConfig with the given configuration.  Note
   * that the date/time format variants will be created by concatenating the
   * relevant date and time formats, separated by a single space,
   * e.g. "<date> <time>".
   *
   * @param dateFormat the date (no time) format
   * @param timeFormat12 the 12 hour time format
   * @param timeFormat24 the 24 hour time format
   * @param dateSeparator the primary separator used to separate elements in
   *                      the date format.  this is used to identify the
   *                      components of date/time string.
   * @param timeSeparator the primary separator used to separate elements in
   *                      the time format (both 12 hour and 24 hour).  this is
   *                      used to identify the components of a date/time
   *                      string.  This value should differ from the
   *                      dateSeparator.
   */
  public TemporalConfig(String dateFormat, String longDateFormat,
                        String timeFormat12, String timeFormat24,
                        char dateSeparator, char timeSeparator, Locale locale)
  {
    _locale = locale;
    _dateFormat = dateFormat;
    _longDateFormat = longDateFormat;
    _timeFormat12 = timeFormat12;
    _timeFormat24 = timeFormat24;
    _dateSeparator = dateSeparator;
    _timeSeparator = timeSeparator;
    _dateTimeFormat12 = toDateTimeFormat(_dateFormat, _timeFormat12);
    _dateTimeFormat24 = toDateTimeFormat(_dateFormat, _timeFormat24);
    // there doesn't seem to be a good/easy way to get this in new jave.time
    // api, so just use old api
    _amPmStrings = DateFormatSymbols.getInstance(locale).getAmPmStrings();
  }

  public Locale getLocale() {
    return _locale;
  }

  public String getDateFormat() {
    return _dateFormat;
  }

  public String getTimeFormat12() {
    return _timeFormat12;
  }

  public String getTimeFormat24() {
    return _timeFormat24;
  }

  public String getDateTimeFormat12() {
    return _dateTimeFormat12;
  }

  public String getDateTimeFormat24() {
    return _dateTimeFormat24;
  }

  public String getDefaultDateFormat() {
    return getDateFormat();
  }

  public String getDefaultTimeFormat() {
    return getTimeFormat12();
  }

  public String getDefaultDateTimeFormat() {
    return getDateTimeFormat12();
  }

  public char getDateSeparator() {
    return _dateSeparator;
  }

  public char getTimeSeparator() {
    return _timeSeparator;
  }

  public String getDateTimeFormat(Type type) {
    switch(type) {
    case DATE:
    case SHORT_DATE:
      return getDefaultDateFormat();
    case TIME:
      return getDefaultTimeFormat();
    case DATE_TIME:
    case GENERAL_DATE:
      return getDefaultDateTimeFormat();
    case TIME_12:
    case LONG_TIME:
      return getTimeFormat12();
    case TIME_24:
      return getTimeFormat24();
    case DATE_TIME_12:
      return getDateTimeFormat12();
    case DATE_TIME_24:
      return getDateTimeFormat24();
    case LONG_DATE:
      return getLongDateFormat();
    case MEDIUM_DATE:
      return getMediumDateFormat();
    case MEDIUM_TIME:
      return getMediumTimeFormat();
    case SHORT_TIME:
      return getShortTimeFormat();
    default:
      throw new IllegalArgumentException("unknown date/time type " + type);
    }
  }

  public String[] getAmPmStrings() {
    return _amPmStrings;
  }

  private static String toDateTimeFormat(String dateFormat, String timeFormat) {
    return dateFormat + " " + timeFormat;
  }

  protected String getLongDateFormat() {
    return _longDateFormat;
  }

  protected String getMediumDateFormat() {
    return MEDIUM_DATE_FORMAT;
  }

  protected String getMediumTimeFormat() {
    return MEDIUM_TIME_FORMAT;
  }

  protected String getShortTimeFormat() {
    return SHORT_TIME_FORMAT;
  }
}