You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TemporalConfig.java 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. Copyright (c) 2017 James Ahlborn
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package com.healthmarketscience.jackcess.expr;
  14. import java.text.DateFormatSymbols;
  15. import java.util.Locale;
  16. /**
  17. * A TemporalConfig encapsulates date/time formatting options for expression
  18. * evaluation. The default {@link #US_TEMPORAL_CONFIG} instance provides US
  19. * specific locale configuration. Databases which have been built for other
  20. * locales can utilize custom implementations of TemporalConfig in order to
  21. * evaluate expressions correctly.
  22. *
  23. * @author James Ahlborn
  24. */
  25. public class TemporalConfig
  26. {
  27. public static final String US_DATE_FORMAT = "M/d/yyyy";
  28. public static final String US_DATE_IMPLICIT_YEAR_FORMAT = "M/d";
  29. public static final String US_TIME_FORMAT_12_FORMAT = "h:mm:ss a";
  30. public static final String US_TIME_FORMAT_24_FORMAT = "H:mm:ss";
  31. public static final String US_LONG_DATE_FORMAT = "EEEE, MMMM dd, yyyy";
  32. public static final String MEDIUM_DATE_FORMAT = "dd-MMM-yy";
  33. public static final String MEDIUM_TIME_FORMAT = "hh:mm a";
  34. public static final String SHORT_TIME_FORMAT = "HH:mm";
  35. /** default implementation which is configured for the US locale */
  36. public static final TemporalConfig US_TEMPORAL_CONFIG = new TemporalConfig(
  37. US_DATE_FORMAT, US_DATE_IMPLICIT_YEAR_FORMAT, US_LONG_DATE_FORMAT,
  38. US_TIME_FORMAT_12_FORMAT, US_TIME_FORMAT_24_FORMAT, '/', ':', Locale.US);
  39. public enum Type {
  40. DATE, TIME, DATE_TIME, TIME_12, TIME_24, DATE_TIME_12, DATE_TIME_24,
  41. GENERAL_DATE, LONG_DATE, MEDIUM_DATE, SHORT_DATE,
  42. LONG_TIME, MEDIUM_TIME, SHORT_TIME;
  43. public Type getDefaultType() {
  44. switch(this) {
  45. case DATE:
  46. case LONG_DATE:
  47. case MEDIUM_DATE:
  48. case SHORT_DATE:
  49. return DATE;
  50. case TIME:
  51. case TIME_12:
  52. case TIME_24:
  53. case LONG_TIME:
  54. case MEDIUM_TIME:
  55. case SHORT_TIME:
  56. return TIME;
  57. case DATE_TIME:
  58. case DATE_TIME_12:
  59. case DATE_TIME_24:
  60. case GENERAL_DATE:
  61. return DATE_TIME;
  62. default:
  63. throw new RuntimeException("invalid type " + this);
  64. }
  65. }
  66. public Value.Type getValueType() {
  67. switch(this) {
  68. case DATE:
  69. case LONG_DATE:
  70. case MEDIUM_DATE:
  71. case SHORT_DATE:
  72. return Value.Type.DATE;
  73. case TIME:
  74. case TIME_12:
  75. case TIME_24:
  76. case LONG_TIME:
  77. case MEDIUM_TIME:
  78. case SHORT_TIME:
  79. return Value.Type.TIME;
  80. case DATE_TIME:
  81. case DATE_TIME_12:
  82. case DATE_TIME_24:
  83. case GENERAL_DATE:
  84. return Value.Type.DATE_TIME;
  85. default:
  86. throw new RuntimeException("invalid type " + this);
  87. }
  88. }
  89. public boolean includesTime() {
  90. return !isDateOnly();
  91. }
  92. public boolean includesDate() {
  93. return !isTimeOnly();
  94. }
  95. public boolean isDateOnly() {
  96. switch(this) {
  97. case DATE:
  98. case LONG_DATE:
  99. case MEDIUM_DATE:
  100. case SHORT_DATE:
  101. return true;
  102. default:
  103. return false;
  104. }
  105. }
  106. public boolean isTimeOnly() {
  107. switch(this) {
  108. case TIME:
  109. case TIME_12:
  110. case TIME_24:
  111. case LONG_TIME:
  112. case MEDIUM_TIME:
  113. case SHORT_TIME:
  114. return true;
  115. default:
  116. return false;
  117. }
  118. }
  119. }
  120. private final String _dateFormat;
  121. private final String _dateImplicitYearFormat;
  122. private final String _longDateFormat;
  123. private final String _timeFormat12;
  124. private final String _timeFormat24;
  125. private final char _dateSeparator;
  126. private final char _timeSeparator;
  127. private final String _dateTimeFormat12;
  128. private final String _dateTimeFormat24;
  129. private final DateFormatSymbols _symbols;
  130. /**
  131. * Instantiates a new TemporalConfig with the given configuration. Note
  132. * that the date/time format variants will be created by concatenating the
  133. * relevant date and time formats, separated by a single space, e.g. "<date>
  134. * <time>".
  135. *
  136. * @param dateFormat the date (no time) format
  137. * @param dateImplicitYearFormat the date (no time) with no year format
  138. * @param timeFormat12 the 12 hour time format
  139. * @param timeFormat24 the 24 hour time format
  140. * @param dateSeparator the primary separator used to separate elements in
  141. * the date format. this is used to identify the
  142. * components of date/time string.
  143. * @param timeSeparator the primary separator used to separate elements in
  144. * the time format (both 12 hour and 24 hour). this is
  145. * used to identify the components of a date/time
  146. * string. This value should differ from the
  147. * dateSeparator.
  148. */
  149. public TemporalConfig(String dateFormat, String dateImplicitYearFormat,
  150. String longDateFormat,
  151. String timeFormat12, String timeFormat24,
  152. char dateSeparator, char timeSeparator, Locale locale)
  153. {
  154. _dateFormat = dateFormat;
  155. _dateImplicitYearFormat = dateImplicitYearFormat;
  156. _longDateFormat = longDateFormat;
  157. _timeFormat12 = timeFormat12;
  158. _timeFormat24 = timeFormat24;
  159. _dateSeparator = dateSeparator;
  160. _timeSeparator = timeSeparator;
  161. _dateTimeFormat12 = _dateFormat + " " + _timeFormat12;
  162. _dateTimeFormat24 = _dateFormat + " " + _timeFormat24;
  163. _symbols = DateFormatSymbols.getInstance(locale);
  164. }
  165. public String getDateFormat() {
  166. return _dateFormat;
  167. }
  168. public String getTimeFormat12() {
  169. return _timeFormat12;
  170. }
  171. public String getTimeFormat24() {
  172. return _timeFormat24;
  173. }
  174. public String getDateTimeFormat12() {
  175. return _dateTimeFormat12;
  176. }
  177. public String getDateTimeFormat24() {
  178. return _dateTimeFormat24;
  179. }
  180. public String getDefaultDateFormat() {
  181. return getDateFormat();
  182. }
  183. public String getDefaultTimeFormat() {
  184. return getTimeFormat12();
  185. }
  186. public String getDefaultDateTimeFormat() {
  187. return getDateTimeFormat12();
  188. }
  189. public char getDateSeparator() {
  190. return _dateSeparator;
  191. }
  192. public char getTimeSeparator() {
  193. return _timeSeparator;
  194. }
  195. public String getDateTimeFormat(Type type) {
  196. switch(type) {
  197. case DATE:
  198. case SHORT_DATE:
  199. return getDefaultDateFormat();
  200. case TIME:
  201. return getDefaultTimeFormat();
  202. case DATE_TIME:
  203. case GENERAL_DATE:
  204. return getDefaultDateTimeFormat();
  205. case TIME_12:
  206. case LONG_TIME:
  207. return getTimeFormat12();
  208. case TIME_24:
  209. return getTimeFormat24();
  210. case DATE_TIME_12:
  211. return getDateTimeFormat12();
  212. case DATE_TIME_24:
  213. return getDateTimeFormat24();
  214. case LONG_DATE:
  215. return getLongDateFormat();
  216. case MEDIUM_DATE:
  217. return getMediumDateFormat();
  218. case MEDIUM_TIME:
  219. return getMediumTimeFormat();
  220. case SHORT_TIME:
  221. return getShortTimeFormat();
  222. default:
  223. throw new IllegalArgumentException("unknown date/time type " + type);
  224. }
  225. }
  226. public String getImplicitYearDateTimeFormat(Type type) {
  227. switch(type) {
  228. case DATE:
  229. return _dateImplicitYearFormat;
  230. case DATE_TIME:
  231. return toDateTimeFormat(_dateImplicitYearFormat, getDefaultTimeFormat());
  232. case DATE_TIME_12:
  233. return toDateTimeFormat(_dateImplicitYearFormat, getTimeFormat12());
  234. case DATE_TIME_24:
  235. return toDateTimeFormat(_dateImplicitYearFormat, getTimeFormat24());
  236. default:
  237. throw new IllegalArgumentException(
  238. "the given format does not include a date " + type);
  239. }
  240. }
  241. public DateFormatSymbols getDateFormatSymbols() {
  242. return _symbols;
  243. }
  244. private static String toDateTimeFormat(String dateFormat, String timeFormat) {
  245. return dateFormat + " " + timeFormat;
  246. }
  247. protected String getLongDateFormat() {
  248. return _longDateFormat;
  249. }
  250. protected String getMediumDateFormat() {
  251. return MEDIUM_DATE_FORMAT;
  252. }
  253. protected String getMediumTimeFormat() {
  254. return MEDIUM_TIME_FORMAT;
  255. }
  256. protected String getShortTimeFormat() {
  257. return SHORT_TIME_FORMAT;
  258. }
  259. }