Skip to content
Closed
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Copy-paste tests to DateTimeUtilsSuite
  • Loading branch information
MaxGekk committed Mar 18, 2020
commit 5837181ab9cc8ce988d5bdafd4cad3cb41b5cc87
Original file line number Diff line number Diff line change
Expand Up @@ -686,4 +686,64 @@ class DateTimeUtilsSuite extends SparkFunSuite with Matchers with SQLHelper {
assert(convertIncompatiblePattern("yyyy-MM-dd'T'HH:mm:ss.SSSz G")
=== "yyyy-MM-dd'T'HH:mm:ss.SSSz G")
}

test("rebase julian to/from gregorian micros") {
outstandingTimezones.foreach { timeZone =>
withDefaultTimeZone(timeZone) {
Seq(
"0001-01-01 01:02:03.654321",
"1000-01-01 03:02:01.123456",
"1582-10-04 00:00:00.000000",
"1582-10-15 00:00:00.999999", // Gregorian cutover day
"1883-11-10 00:00:00.000000", // America/Los_Angeles -7:52:58 zone offset
"1883-11-20 00:00:00.000000", // America/Los_Angeles -08:00 zone offset
"1969-12-31 11:22:33.000100",
"1970-01-01 00:00:00.000001", // The epoch day
"2020-03-14 09:33:01.500000").foreach { ts =>
withClue(s"time zone = ${timeZone.getID} ts = $ts") {
val julianTs = Timestamp.valueOf(ts)
val julianMicros = millisToMicros(julianTs.getTime) +
((julianTs.getNanos / NANOS_PER_MICROS) % MICROS_PER_MILLIS)
val gregorianMicros = instantToMicros(LocalDateTime.parse(ts.replace(' ', 'T'))
.atZone(timeZone.toZoneId)
.toInstant)

assert(rebaseJulianToGregorianMicros(julianMicros) === gregorianMicros)
assert(rebaseGregorianToJulianMicros(gregorianMicros) === julianMicros)
}
}
}
}
}

test("rebase gregorian to/from julian days") {
// millisToDays() and fromJavaDate() are taken from Spark 2.4
def millisToDays(millisUtc: Long, timeZone: TimeZone): Int = {
val millisLocal = millisUtc + timeZone.getOffset(millisUtc)
Math.floor(millisLocal.toDouble / MILLIS_PER_DAY).toInt
}
def fromJavaDate(date: Date): Int = {
millisToDays(date.getTime, defaultTimeZone())
}
outstandingTimezones.foreach { timeZone =>
withDefaultTimeZone(timeZone) {
Seq(
"0001-01-01",
"1000-01-01",
"1582-10-04",
"1582-10-15", // Gregorian cutover day
"1883-11-10", // America/Los_Angeles -7:52:58 zone offset
"1883-11-20", // America/Los_Angeles -08:00 zone offset
"1969-12-31",
"1970-01-01", // The epoch day
"2020-03-14").foreach { date =>
val julianDays = fromJavaDate(Date.valueOf(date))
val gregorianDays = localDateToDays(LocalDate.parse(date))

assert(rebaseGregorianToJulianDays(gregorianDays) === julianDays)
assert(rebaseJulianToGregorianDays(julianDays) === gregorianDays)
}
}
}
}
}