Skip to content

Commit

Permalink
[SPARK-26593][SQL] Use Proleptic Gregorian calendar in casting UTF8St…
Browse files Browse the repository at this point in the history
…ring to Date/TimestampType

## What changes were proposed in this pull request?

In the PR, I propose to use *java.time* classes in `stringToDate` and `stringToTimestamp`. This switches the methods from the hybrid calendar (Gregorian+Julian) to Proleptic Gregorian calendar. And it should make the casting consistent to other Spark classes that converts textual representation of dates/timestamps to `DateType`/`TimestampType`.

## How was this patch tested?

The changes were tested by existing suites - `HashExpressionsSuite`, `CastSuite` and `DateTimeUtilsSuite`.

Closes apache#23512 from MaxGekk/utf8string-timestamp-parsing.

Lead-authored-by: Maxim Gekk <[email protected]>
Co-authored-by: Maxim Gekk <[email protected]>
Signed-off-by: Herman van Hovell <[email protected]>
  • Loading branch information
2 people authored and hvanhovell committed Jan 17, 2019
1 parent c0632ce commit 6f8c0e5
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 432 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ package org.apache.spark.sql.catalyst.util
import java.time.{Instant, ZoneId}
import java.util.Locale

import org.apache.spark.sql.catalyst.util.DateTimeUtils.instantToDays

sealed trait DateFormatter extends Serializable {
def parse(s: String): Int // returns days since epoch
def format(days: Int): String
Expand All @@ -38,11 +40,7 @@ class Iso8601DateFormatter(
toInstantWithZoneId(temporalAccessor, UTC)
}

override def parse(s: String): Int = {
val seconds = toInstant(s).getEpochSecond
val days = Math.floorDiv(seconds, DateTimeUtils.SECONDS_PER_DAY)
days.toInt
}
override def parse(s: String): Int = instantToDays(toInstant(s))

override def format(days: Int): String = {
val instant = Instant.ofEpochSecond(days * DateTimeUtils.SECONDS_PER_DAY)
Expand Down
Loading

0 comments on commit 6f8c0e5

Please sign in to comment.