LuciferYang commented on code in PR #50190:
URL: https://github.com/apache/spark/pull/50190#discussion_r1986371822


##########
sql/api/src/main/scala/org/apache/spark/sql/catalyst/util/TimeFormatter.scala:
##########
@@ -0,0 +1,96 @@
+/*
+ * 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.spark.sql.catalyst.util
+
+import java.time.LocalTime
+import java.util.Locale
+
+import org.apache.spark.sql.catalyst.util.SparkDateTimeUtils._
+
+sealed trait TimeFormatter extends Serializable {
+  def parse(s: String): Long // returns microseconds since midnight
+
+  def format(localTime: LocalTime): String
+  // Converts microseconds since the midnight to time string
+  def format(micros: Long): String
+
+  def validatePatternString(): Unit
+}
+
+/**
+ * The ISO time formatter is capable of formatting and parsing the ISO-8601 
extended time format.
+ */
+class Iso8601TimeFormatter(pattern: String, locale: Locale, isParsing: Boolean)
+    extends TimeFormatter
+    with DateTimeFormatterHelper {
+
+  @transient
+  protected lazy val formatter = getOrCreateFormatter(pattern, locale, 
isParsing)

Review Comment:
   nit: for a `protected` field, readability might be improved if a type 
declaration could be added.



##########
sql/api/src/main/scala/org/apache/spark/sql/catalyst/util/TimeFormatter.scala:
##########
@@ -0,0 +1,96 @@
+/*
+ * 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.spark.sql.catalyst.util
+
+import java.time.LocalTime
+import java.util.Locale
+
+import org.apache.spark.sql.catalyst.util.SparkDateTimeUtils._
+
+sealed trait TimeFormatter extends Serializable {
+  def parse(s: String): Long // returns microseconds since midnight
+
+  def format(localTime: LocalTime): String
+  // Converts microseconds since the midnight to time string
+  def format(micros: Long): String
+
+  def validatePatternString(): Unit
+}
+
+/**
+ * The ISO time formatter is capable of formatting and parsing the ISO-8601 
extended time format.
+ */
+class Iso8601TimeFormatter(pattern: String, locale: Locale, isParsing: Boolean)
+    extends TimeFormatter
+    with DateTimeFormatterHelper {
+
+  @transient
+  protected lazy val formatter = getOrCreateFormatter(pattern, locale, 
isParsing)
+
+  override def parse(s: String): Long = {
+    val localTime = toLocalTime(formatter.parse(s))
+    localTimeToMicros(localTime)
+  }
+
+  override def format(localTime: LocalTime): String = {
+    localTime.format(formatter)
+  }
+
+  override def format(micros: Long): String = {
+    format(microsToLocalTime(micros))
+  }
+
+  override def validatePatternString(): Unit = {
+    try {
+      formatter
+    } catch checkInvalidPattern(pattern)
+    ()
+  }
+}
+
+/**
+ * The formatter parses/formats times according to the pattern 
`HH:mm:ss.[..fff..]` where
+ * `[..fff..]` is a fraction of second up to microsecond resolution. The 
formatter does not output
+ * trailing zeros in the fraction. For example, the time `15:00:01.123400` is 
formatted as the
+ * string `15:00:01.1234`.
+ */
+class FractionTimeFormatter
+    extends Iso8601TimeFormatter(
+      TimeFormatter.defaultPattern,
+      TimeFormatter.defaultLocale,
+      isParsing = false) {
+
+  @transient
+  override protected lazy val formatter = 
DateTimeFormatterHelper.fractionTimeFormatter

Review Comment:
   ditto



##########
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/util/DateTimeTestUtils.scala:
##########
@@ -112,4 +112,15 @@ object DateTimeTestUtils {
     result = Math.addExact(result, Math.multiplyExact(seconds, 
MICROS_PER_SECOND))
     result
   }
+
+  // Returns microseconds since midnight
+  def localtime(

Review Comment:
   `def localtime` -> `def localTime`



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to