cloud-fan commented on a change in pull request #23196: [SPARK-26243][SQL] Use
java.time API for parsing timestamps and dates from JSON
URL: https://github.com/apache/spark/pull/23196#discussion_r241254182
##########
File path:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/DateTimeFormatter.scala
##########
@@ -33,26 +33,37 @@ sealed trait DateTimeFormatter {
def format(us: Long): String
}
+trait FormatterUtils {
+ def zoneId: ZoneId
+ def buildFormatter(pattern: String, locale: Locale):
java.time.format.DateTimeFormatter = {
+ new DateTimeFormatterBuilder()
+ .appendPattern(pattern)
+ .parseDefaulting(ChronoField.YEAR_OF_ERA, 1970)
+ .parseDefaulting(ChronoField.MONTH_OF_YEAR, 1)
+ .parseDefaulting(ChronoField.DAY_OF_MONTH, 1)
+ .parseDefaulting(ChronoField.HOUR_OF_DAY, 0)
+ .parseDefaulting(ChronoField.MINUTE_OF_HOUR, 0)
+ .parseDefaulting(ChronoField.SECOND_OF_MINUTE, 0)
+ .toFormatter(locale)
+ }
+ def toInstant(temporalAccessor: TemporalAccessor): java.time.Instant = {
+ val localDateTime = LocalDateTime.from(temporalAccessor)
+ val zonedDateTime = ZonedDateTime.of(localDateTime, zoneId)
+ Instant.from(zonedDateTime)
+ }
+}
+
class Iso8601DateTimeFormatter(
pattern: String,
timeZone: TimeZone,
- locale: Locale) extends DateTimeFormatter {
- val formatter = new DateTimeFormatterBuilder()
- .appendPattern(pattern)
- .parseDefaulting(ChronoField.YEAR_OF_ERA, 1970)
- .parseDefaulting(ChronoField.MONTH_OF_YEAR, 1)
- .parseDefaulting(ChronoField.DAY_OF_MONTH, 1)
- .parseDefaulting(ChronoField.HOUR_OF_DAY, 0)
- .parseDefaulting(ChronoField.MINUTE_OF_HOUR, 0)
- .parseDefaulting(ChronoField.SECOND_OF_MINUTE, 0)
- .toFormatter(locale)
+ locale: Locale) extends DateTimeFormatter with FormatterUtils {
+ val zoneId = timeZone.toZoneId
+ val formatter = buildFormatter(pattern, locale)
def toInstant(s: String): Instant = {
val temporalAccessor = formatter.parse(s)
if (temporalAccessor.query(TemporalQueries.offset()) == null) {
Review comment:
sorry I'm not very familiar with this API. what does this condition mean?
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]