Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,12 @@ object DateTimeUtils {
* @return A `java.sql.Timestamp` from number of micros since epoch.
*/
def toJavaTimestamp(us: SQLTimestamp): Timestamp = {
val ldt = microsToInstant(us).atZone(ZoneId.systemDefault()).toLocalDateTime
Timestamp.valueOf(ldt)
val rebasedMicros = rebaseGregorianToJulianMicros(us)
val seconds = Math.floorDiv(rebasedMicros, MICROS_PER_SECOND)
val ts = new Timestamp(seconds * MILLIS_PER_SECOND)
val nanos = (rebasedMicros - seconds * MICROS_PER_SECOND) * NANOS_PER_MICROS
ts.setNanos(nanos.toInt)
ts
}

/**
Expand All @@ -172,17 +176,8 @@ object DateTimeUtils {
* @return The number of micros since epoch from `java.sql.Timestamp`.
*/
def fromJavaTimestamp(t: Timestamp): SQLTimestamp = {
val era = if (t.before(julianCommonEraStart)) 0 else 1
val localDateTime = LocalDateTime.of(
t.getYear + 1900, t.getMonth + 1, 1,
t.getHours, t.getMinutes, t.getSeconds, t.getNanos)
.`with`(ChronoField.ERA, era)
// Add days separately to convert dates existed in Julian calendar but not
// in Proleptic Gregorian calendar. For example, 1000-02-29 is valid date
// in Julian calendar because 1000 is a leap year but 1000 is not a leap
// year in Proleptic Gregorian calendar. And 1000-02-29 doesn't exist in it.
.plusDays(t.getDate - 1) // Returns the next valid date after `date.getDate - 1` days
instantToMicros(localDateTime.atZone(ZoneId.systemDefault).toInstant)
val micros = millisToMicros(t.getTime) + (t.getNanos / NANOS_PER_MICROS) % MICROS_PER_MILLIS
Copy link
Member

@dongjoon-hyun dongjoon-hyun Apr 14, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to be missing in branch-3.0.

$ git grep millisToMicros
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/DateTimeUtils.scala: 
val micros = millisToMicros(t.getTime) + (t.getNanos / NANOS_PER_MICROS) % MICROS_PER_MILLIS

rebaseJulianToGregorianMicros(micros)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -496,8 +496,8 @@ class DateTimeUtilsSuite extends SparkFunSuite with Matchers with SQLHelper {
test("2016-03-13 02:00:00", tz, "2016-03-13 10:00:00.0")
test("2016-03-13 03:00:00", tz, "2016-03-13 10:00:00.0")
test("2016-11-06 00:59:59", tz, "2016-11-06 07:59:59.0")
test("2016-11-06 01:00:00", tz, "2016-11-06 08:00:00.0")
test("2016-11-06 01:59:59", tz, "2016-11-06 08:59:59.0")
test("2016-11-06 01:00:00", tz, "2016-11-06 09:00:00.0")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Screenshot 2020-04-11 at 19 01 34

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the behavior of Spark 2.4?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as we have looked into the JDK code before, PST is treated as LA. So the behavior is the same as 2.4, and it's great!

can we highlight it in the Does this PR introduce any user-facing change? section? Say it's changed back to the 2.4 behavior.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

test("2016-11-06 01:59:59", tz, "2016-11-06 09:59:59.0")
test("2016-11-06 02:00:00", tz, "2016-11-06 10:00:00.0")
}
}
Expand Down
Loading