This is an automated email from the ASF dual-hosted git repository.
gortiz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/pinot.git
The following commit(s) were added to refs/heads/master by this push:
new e64ef15330 Support more formats in TimestampUtils (#13609)
e64ef15330 is described below
commit e64ef15330f356dc96fae38d52bce80e6af2068f
Author: William Gan <[email protected]>
AuthorDate: Wed Jul 17 00:40:19 2024 -0700
Support more formats in TimestampUtils (#13609)
---
.../org/apache/pinot/spi/utils/TimestampUtils.java | 49 ++++++++++++---
.../apache/pinot/spi/utils/TimestampUtilsTest.java | 71 ++++++++++++++++++++++
2 files changed, 110 insertions(+), 10 deletions(-)
diff --git
a/pinot-spi/src/main/java/org/apache/pinot/spi/utils/TimestampUtils.java
b/pinot-spi/src/main/java/org/apache/pinot/spi/utils/TimestampUtils.java
index af7a513ffb..c0d23e95b7 100644
--- a/pinot-spi/src/main/java/org/apache/pinot/spi/utils/TimestampUtils.java
+++ b/pinot-spi/src/main/java/org/apache/pinot/spi/utils/TimestampUtils.java
@@ -19,9 +19,22 @@
package org.apache.pinot.spi.utils;
import java.sql.Timestamp;
+import java.time.LocalDateTime;
+import java.time.ZoneId;
+import java.time.format.DateTimeFormatter;
+import java.time.format.DateTimeFormatterBuilder;
+import java.time.format.DateTimeParseException;
+import java.time.temporal.ChronoField;
public class TimestampUtils {
+ private static final DateTimeFormatter DATE_TIME_FORMATTER = new
DateTimeFormatterBuilder()
+ .appendPattern("yyyy-MM-dd[ HH:mm[:ss]]")
+ .parseDefaulting(ChronoField.HOUR_OF_DAY, 0)
+ .parseDefaulting(ChronoField.MINUTE_OF_HOUR, 0)
+ .parseDefaulting(ChronoField.SECOND_OF_MINUTE, 0)
+ .toFormatter();
+
private TimestampUtils() {
}
@@ -30,6 +43,7 @@ public class TimestampUtils {
* <p>Two formats of timestamp are supported:
* <ul>
* <li>'yyyy-mm-dd hh:mm:ss[.fffffffff]'</li>
+ * <li>'yyyy-MM-dd[ HH:mm[:ss]]'</li>
* <li>Millis since epoch</li>
* </ul>
*/
@@ -37,11 +51,18 @@ public class TimestampUtils {
try {
return Timestamp.valueOf(timestampString);
} catch (Exception e) {
- try {
- return new Timestamp(Long.parseLong(timestampString));
- } catch (Exception e1) {
- throw new IllegalArgumentException(String.format("Invalid timestamp:
'%s'", timestampString));
- }
+ // Try the next format
+ }
+ try {
+ return new Timestamp(Long.parseLong(timestampString));
+ } catch (Exception e1) {
+ // Try the next format
+ }
+ try {
+ LocalDateTime dateTime = LocalDateTime.parse(timestampString,
DATE_TIME_FORMATTER);
+ return Timestamp.valueOf(dateTime);
+ } catch (DateTimeParseException e) {
+ throw new IllegalArgumentException(String.format("Invalid timestamp:
'%s'", timestampString));
}
}
@@ -50,6 +71,7 @@ public class TimestampUtils {
* <p>Two formats of timestamp are supported:
* <ul>
* <li>'yyyy-mm-dd hh:mm:ss[.fffffffff]'</li>
+ * <li>'yyyy-MM-dd[ HH:mm[:ss]]'</li>
* <li>Millis since epoch</li>
* </ul>
*/
@@ -57,11 +79,18 @@ public class TimestampUtils {
try {
return Timestamp.valueOf(timestampString).getTime();
} catch (Exception e) {
- try {
- return Long.parseLong(timestampString);
- } catch (Exception e1) {
- throw new IllegalArgumentException(String.format("Invalid timestamp:
'%s'", timestampString));
- }
+ // Try the next format
+ }
+ try {
+ return Long.parseLong(timestampString);
+ } catch (Exception e1) {
+ // Try the next format
+ }
+ try {
+ LocalDateTime dateTime = LocalDateTime.parse(timestampString,
DATE_TIME_FORMATTER);
+ return
dateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
+ } catch (DateTimeParseException e) {
+ throw new IllegalArgumentException(String.format("Invalid timestamp:
'%s'", timestampString));
}
}
}
diff --git
a/pinot-spi/src/test/java/org/apache/pinot/spi/utils/TimestampUtilsTest.java
b/pinot-spi/src/test/java/org/apache/pinot/spi/utils/TimestampUtilsTest.java
new file mode 100644
index 0000000000..142d99256a
--- /dev/null
+++ b/pinot-spi/src/test/java/org/apache/pinot/spi/utils/TimestampUtilsTest.java
@@ -0,0 +1,71 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.pinot.spi.utils;
+
+import java.sql.Timestamp;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+public class TimestampUtilsTest {
+ @Test
+ public void testToTimestamp() {
+ Assert.assertEquals(
+ TimestampUtils.toTimestamp("2024-07-12 15:32:36.111"),
+ Timestamp.valueOf("2024-07-12 15:32:36.111")
+ );
+ Assert.assertEquals(
+ TimestampUtils.toTimestamp("2024-07-12 15:32:36"),
+ Timestamp.valueOf(LocalDateTime.of(2024, 7, 12, 15, 32, 36))
+ );
+ Assert.assertEquals(
+ TimestampUtils.toTimestamp("2024-07-12 15:32"),
+ Timestamp.valueOf(LocalDateTime.of(2024, 7, 12, 15, 32))
+ );
+ Assert.assertEquals(
+ TimestampUtils.toTimestamp("2024-07-12"),
+ Timestamp.valueOf(LocalDate.of(2024, 7, 12).atStartOfDay())
+ );
+ Assert.assertEquals(TimestampUtils.toTimestamp("1720798356111"), new
Timestamp(1720798356111L));
+ Assert.assertThrows(IllegalArgumentException.class, () ->
TimestampUtils.toTimestamp("July 12, 2024"));
+ }
+
+ @Test
+ public void testToMillisSinceEpoch() {
+ Assert.assertEquals(
+ TimestampUtils.toMillisSinceEpoch("2024-07-12 15:32:36.111"),
+ Timestamp.valueOf("2024-07-12 15:32:36.111").getTime()
+ );
+ Assert.assertEquals(
+ TimestampUtils.toMillisSinceEpoch("2024-07-12 15:32:36"),
+ Timestamp.valueOf(LocalDateTime.of(2024, 7, 12, 15, 32, 36)).getTime()
+ );
+ Assert.assertEquals(
+ TimestampUtils.toMillisSinceEpoch("2024-07-12 15:32"),
+ Timestamp.valueOf(LocalDateTime.of(2024, 7, 12, 15, 32)).getTime()
+ );
+ Assert.assertEquals(
+ TimestampUtils.toMillisSinceEpoch("2024-07-12"),
+ Timestamp.valueOf(LocalDate.of(2024, 7, 12).atStartOfDay()).getTime()
+ );
+ Assert.assertEquals(TimestampUtils.toMillisSinceEpoch("1720798356111"),
1720798356111L);
+ Assert.assertThrows(IllegalArgumentException.class, () ->
TimestampUtils.toMillisSinceEpoch("July 12, 2024"));
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]