/* 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/yyyy"; public static final String US_DATE_IMPLICIT_YEAR_FORMAT = "M/d"; 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, yyyy"; public static final String MEDIUM_DATE_FORMAT = "dd-MMM-yy"; 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_DATE_IMPLICIT_YEAR_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 String _dateFormat; private final String _dateImplicitYearFormat; 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 DateFormatSymbols _symbols; /** * 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. " *