Skip to content

Commit

Permalink
Support time zone literals (deephaven#4704)
Browse files Browse the repository at this point in the history
* Add support for Time Zone literals.

* Better error message
  • Loading branch information
chipkent committed Oct 24, 2023
1 parent 50b4a03 commit a4b9533
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public static TimeLiteralReplacedExpression convertExpression(String expression)
int nanosIndex = 0;
int periodIndex = 0;
int durationIndex = 0;
int tzIndex = 0;

final Matcher matcher = Pattern.compile("'[^']*'").matcher(expression);

Expand Down Expand Up @@ -129,8 +130,16 @@ public static TimeLiteralReplacedExpression convertExpression(String expression)
.append(expression, matcher.start() + 1, matcher.end() - 1).append("\");\n");
newVariables.put("_localTime" + nanosIndex, LocalTime.class);
nanosIndex++;
} else if (DateTimeUtils.parseTimeZoneQuiet(s) != null) {
matcher.appendReplacement(convertedFormula, "_timeZone" + tzIndex);
instanceVariablesString.append(" private java.time.ZoneId _timeZone").append(tzIndex)
.append("=DateTimeUtils.parseTimeZone(\"")
.append(expression, matcher.start() + 1, matcher.end() - 1).append("\");\n");
newVariables.put("_timeZone" + tzIndex, ZoneId.class);
tzIndex++;
} else {
throw new Exception("Cannot parse datetime/time/period : " + s);
throw new Exception(
"Cannot parse literal as a datetime, date, time, duration, period, or timezone: " + s);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,32 @@ public void testConvertExpressionLocalTime() throws Exception {
tlre.getInstanceVariablesString());
}

public void testConvertExpressionTimeZone() throws Exception {
final TimeLiteralReplacedExpression tlre = TimeLiteralReplacedExpression.convertExpression("'America/Denver'");
TestCase.assertEquals("_timeZone0", tlre.getConvertedFormula());

final HashMap<String, Class<?>> newVars = new HashMap<>();
newVars.put("_timeZone0", ZoneId.class);
TestCase.assertEquals(newVars, tlre.getNewVariables());

TestCase.assertEquals(
" private java.time.ZoneId _timeZone0=DateTimeUtils.parseTimeZone(\"America/Denver\");\n",
tlre.getInstanceVariablesString());
}

public void testConvertExpressionTimeZone2() throws Exception {
final TimeLiteralReplacedExpression tlre = TimeLiteralReplacedExpression.convertExpression("'NY'");
TestCase.assertEquals("_timeZone0", tlre.getConvertedFormula());

final HashMap<String, Class<?>> newVars = new HashMap<>();
newVars.put("_timeZone0", ZoneId.class);
TestCase.assertEquals(newVars, tlre.getNewVariables());

TestCase.assertEquals(
" private java.time.ZoneId _timeZone0=DateTimeUtils.parseTimeZone(\"NY\");\n",
tlre.getInstanceVariablesString());
}

public void testConvertExpressionUnknown() throws Exception {
final TimeLiteralReplacedExpression tlre = TimeLiteralReplacedExpression.convertExpression("'g'");
TestCase.assertEquals("'g'", tlre.getConvertedFormula());
Expand Down

0 comments on commit a4b9533

Please sign in to comment.