1616import java .time .LocalTime ;
1717import java .time .ZoneOffset ;
1818import java .time .ZonedDateTime ;
19+ import java .time .format .DateTimeFormatter ;
1920import org .junit .jupiter .api .Test ;
2021import org .opensearch .sql .exception .ExpressionEvaluationException ;
2122import org .opensearch .sql .expression .function .FunctionProperties ;
@@ -112,15 +113,21 @@ public void timeInUnsupportedFormat() {
112113 }
113114
114115 @ Test
115- public void timestampInUnsupportedFormat () {
116- Throwable exception =
117- assertThrows (
118- ExpressionEvaluationException .class ,
119- () -> new ExprTimestampValue ("2020-07-07T01:01:01Z" ));
116+ public void timestampInISO8601Format () {
117+ ExprTimestampValue timestampValue = new ExprTimestampValue ("2020-07-07T01:01:01Z" );
120118 assertEquals (
121- "timestamp:2020-07-07T01:01:01Z in unsupported format, "
122- + "please use 'yyyy-MM-dd HH:mm:ss[.SSSSSSSSS]'" ,
123- exception .getMessage ());
119+ LocalDateTime .parse ("2020-07-07T01:01:01Z" , DateTimeFormatter .ISO_DATE_TIME )
120+ .toInstant (ZoneOffset .UTC ),
121+ timestampValue .timestampValue ());
122+ }
123+
124+ @ Test
125+ public void timestampInISO8601FormatWithTimeZone () {
126+ ExprTimestampValue timestampValue = new ExprTimestampValue ("2020-07-07T01:01:01-01:00" );
127+ assertEquals (
128+ LocalDateTime .parse ("2020-07-07T02:01:01Z" , DateTimeFormatter .ISO_DATE_TIME )
129+ .toInstant (ZoneOffset .UTC ),
130+ timestampValue .timestampValue ());
124131 }
125132
126133 @ Test
@@ -134,13 +141,10 @@ public void stringTimestampValue() {
134141 assertEquals (LocalTime .parse ("19:44:00" ), stringValue .timeValue ());
135142 assertEquals ("\" 2020-08-17 19:44:00\" " , stringValue .toString ());
136143
137- Throwable exception =
138- assertThrows (
139- ExpressionEvaluationException .class ,
140- () -> new ExprStringValue ("2020-07-07T01:01:01Z" ).timestampValue ());
144+ ExprValue stringValueWithIsoTimestamp = new ExprStringValue ("2020-07-07T01:01:01Z" );
141145 assertEquals (
142- "date: 2020-07-07T01:01:01Z in unsupported format, " + "please use 'yyyy-MM-dd'" ,
143- exception . getMessage ());
146+ LocalDateTime . parse ( " 2020-07-07T01:01:01Z" , DateTimeFormatter . ISO_DATE_TIME ). toInstant ( ZoneOffset . UTC ) ,
147+ stringValueWithIsoTimestamp . timestampValue ());
144148 }
145149
146150 @ Test
@@ -221,7 +225,7 @@ public void timestampOverMaxNanoPrecision() {
221225 () -> new ExprTimestampValue ("2020-07-07 01:01:01.1234567890" ));
222226 assertEquals (
223227 "timestamp:2020-07-07 01:01:01.1234567890 in unsupported format, please use "
224- + "'yyyy-MM-dd HH:mm:ss[.SSSSSSSSS]'" ,
228+ + "'yyyy-MM-dd HH:mm:ss[.SSSSSSSSS]' or ISO 8601 format " ,
225229 exception .getMessage ());
226230 }
227231
0 commit comments