aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/com/healthmarketscience/jackcess/expr
diff options
context:
space:
mode:
authorJames Ahlborn <jtahlborn@yahoo.com>2018-09-29 19:36:51 +0000
committerJames Ahlborn <jtahlborn@yahoo.com>2018-09-29 19:36:51 +0000
commita314d6501dd4266ad197847f8879ac0a119ec930 (patch)
tree76606167d0cc6cbdcf96c4cbe9dde378359af864 /src/main/java/com/healthmarketscience/jackcess/expr
parent0dea2079f785d3f1b70a2a2263c12b3e511ad94d (diff)
downloadjackcess-a314d6501dd4266ad197847f8879ac0a119ec930.tar.gz
jackcess-a314d6501dd4266ad197847f8879ac0a119ec930.zip
add support for monthname function; implement better string to date/time conversions
git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/trunk@1202 f203690c-595d-4dc9-a70b-905162fa7fd2
Diffstat (limited to 'src/main/java/com/healthmarketscience/jackcess/expr')
-rw-r--r--src/main/java/com/healthmarketscience/jackcess/expr/EvalContext.java15
-rw-r--r--src/main/java/com/healthmarketscience/jackcess/expr/LocaleContext.java41
-rw-r--r--src/main/java/com/healthmarketscience/jackcess/expr/TemporalConfig.java72
-rw-r--r--src/main/java/com/healthmarketscience/jackcess/expr/package-info.java4
4 files changed, 114 insertions, 18 deletions
diff --git a/src/main/java/com/healthmarketscience/jackcess/expr/EvalContext.java b/src/main/java/com/healthmarketscience/jackcess/expr/EvalContext.java
index a7e0ecd..c82fe43 100644
--- a/src/main/java/com/healthmarketscience/jackcess/expr/EvalContext.java
+++ b/src/main/java/com/healthmarketscience/jackcess/expr/EvalContext.java
@@ -16,7 +16,6 @@ limitations under the License.
package com.healthmarketscience.jackcess.expr;
-import java.text.SimpleDateFormat;
import javax.script.Bindings;
/**
@@ -26,21 +25,9 @@ import javax.script.Bindings;
*
* @author James Ahlborn
*/
-public interface EvalContext
+public interface EvalContext extends LocaleContext
{
/**
- * @return the currently configured TemporalConfig (from the
- * {@link EvalConfig})
- */
- public TemporalConfig getTemporalConfig();
-
- /**
- * @return an appropriately configured (i.e. TimeZone and other date/time
- * flags) SimpleDateFormat for the given format.
- */
- public SimpleDateFormat createDateFormat(String formatStr);
-
- /**
* @param seed the seed for the random value, following the rules for the
* "Rnd" function
* @return a random value for the given seed following the statefulness
diff --git a/src/main/java/com/healthmarketscience/jackcess/expr/LocaleContext.java b/src/main/java/com/healthmarketscience/jackcess/expr/LocaleContext.java
new file mode 100644
index 0000000..8a9a649
--- /dev/null
+++ b/src/main/java/com/healthmarketscience/jackcess/expr/LocaleContext.java
@@ -0,0 +1,41 @@
+/*
+Copyright (c) 2018 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.SimpleDateFormat;
+
+/**
+ * LocaleContext encapsulates all shared localization state for expression
+ * parsing and evaluation.
+ *
+ * @author James Ahlborn
+ */
+public interface LocaleContext
+{
+ /**
+ * @return the currently configured TemporalConfig (from the
+ * {@link EvalConfig})
+ */
+ public TemporalConfig getTemporalConfig();
+
+ /**
+ * @return an appropriately configured (i.e. TimeZone and other date/time
+ * flags) SimpleDateFormat for the given format.
+ */
+ public SimpleDateFormat createDateFormat(String formatStr);
+
+}
diff --git a/src/main/java/com/healthmarketscience/jackcess/expr/TemporalConfig.java b/src/main/java/com/healthmarketscience/jackcess/expr/TemporalConfig.java
index 2ea1a12..d7061ca 100644
--- a/src/main/java/com/healthmarketscience/jackcess/expr/TemporalConfig.java
+++ b/src/main/java/com/healthmarketscience/jackcess/expr/TemporalConfig.java
@@ -16,6 +16,9 @@ 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
@@ -33,7 +36,45 @@ public class TemporalConfig
/** default implementation which is configured for the US locale */
public static final TemporalConfig US_TEMPORAL_CONFIG = new TemporalConfig(
- US_DATE_FORMAT, US_TIME_FORMAT_12, US_TIME_FORMAT_24, '/', ':');
+ US_DATE_FORMAT, US_TIME_FORMAT_12, US_TIME_FORMAT_24, '/', ':', Locale.US);
+
+ public enum Type {
+ DATE, TIME, DATE_TIME, TIME_12, TIME_24, DATE_TIME_12, DATE_TIME_24;
+
+ public Type getDefaultType() {
+ switch(this) {
+ case DATE:
+ return DATE;
+ case TIME:
+ case TIME_12:
+ case TIME_24:
+ return TIME;
+ case DATE_TIME:
+ case DATE_TIME_12:
+ case DATE_TIME_24:
+ return DATE_TIME;
+ default:
+ throw new RuntimeException("invalid type " + this);
+ }
+ }
+
+ public Value.Type getValueType() {
+ switch(this) {
+ case DATE:
+ return Value.Type.DATE;
+ case TIME:
+ case TIME_12:
+ case TIME_24:
+ return Value.Type.TIME;
+ case DATE_TIME:
+ case DATE_TIME_12:
+ case DATE_TIME_24:
+ return Value.Type.DATE_TIME;
+ default:
+ throw new RuntimeException("invalid type " + this);
+ }
+ }
+ }
private final String _dateFormat;
private final String _timeFormat12;
@@ -42,6 +83,7 @@ public class TemporalConfig
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
@@ -63,7 +105,7 @@ public class TemporalConfig
*/
public TemporalConfig(String dateFormat, String timeFormat12,
String timeFormat24, char dateSeparator,
- char timeSeparator)
+ char timeSeparator, Locale locale)
{
_dateFormat = dateFormat;
_timeFormat12 = timeFormat12;
@@ -72,6 +114,7 @@ public class TemporalConfig
_timeSeparator = timeSeparator;
_dateTimeFormat12 = _dateFormat + " " + _timeFormat12;
_dateTimeFormat24 = _dateFormat + " " + _timeFormat24;
+ _symbols = DateFormatSymbols.getInstance(locale);
}
public String getDateFormat() {
@@ -113,4 +156,29 @@ public class TemporalConfig
public char getTimeSeparator() {
return _timeSeparator;
}
+
+ public String getDateTimeFormat(Type type) {
+ switch(type) {
+ case DATE:
+ return getDefaultDateFormat();
+ case TIME:
+ return getDefaultTimeFormat();
+ case DATE_TIME:
+ return getDefaultDateTimeFormat();
+ case TIME_12:
+ return getTimeFormat12();
+ case TIME_24:
+ return getTimeFormat24();
+ case DATE_TIME_12:
+ return getDateTimeFormat12();
+ case DATE_TIME_24:
+ return getDateTimeFormat24();
+ default:
+ throw new IllegalArgumentException("unknown date/time type " + type);
+ }
+ }
+
+ public DateFormatSymbols getDateFormatSymbols() {
+ return _symbols;
+ }
}
diff --git a/src/main/java/com/healthmarketscience/jackcess/expr/package-info.java b/src/main/java/com/healthmarketscience/jackcess/expr/package-info.java
index 416c96e..05c72e6 100644
--- a/src/main/java/com/healthmarketscience/jackcess/expr/package-info.java
+++ b/src/main/java/com/healthmarketscience/jackcess/expr/package-info.java
@@ -138,7 +138,7 @@ limitations under the License.
* <tr class="TableRowColor"><td>Hour</td><td>Y</td></tr>
* <tr class="TableRowColor"><td>Minute</td><td>Y</td></tr>
* <tr class="TableRowColor"><td>Month</td><td>Y</td></tr>
- * <tr class="TableRowColor"><td>MonthName</td><td></td></tr>
+ * <tr class="TableRowColor"><td>MonthName</td><td>Y</td></tr>
* <tr class="TableRowColor"><td>Now</td><td>Y</td></tr>
* <tr class="TableRowColor"><td>Second</td><td>Y</td></tr>
* <tr class="TableRowColor"><td>Time</td><td>Y</td></tr>
@@ -146,7 +146,7 @@ limitations under the License.
* <tr class="TableRowColor"><td>TimeSerial</td><td>Y</td></tr>
* <tr class="TableRowColor"><td>TimeValue</td><td>Y</td></tr>
* <tr class="TableRowColor"><td>Weekday</td><td>Y</td></tr>
- * <tr class="TableRowColor"><td>WeekdayName</td><td></td></tr>
+ * <tr class="TableRowColor"><td>WeekdayName</td><td>Y</td></tr>
* <tr class="TableRowColor"><td>Year</td><td>Y</td></tr>
* </table>
*