Skip to content

Commit

Permalink
rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
sumeet-db committed Jul 31, 2024
1 parent 40d94b6 commit 885e5af
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -435,11 +435,11 @@ class LegacyFastTimestampFormatter(
val micros = cal.getMicros()
cal.set(Calendar.MILLISECOND, 0)
val julianMicros = Math.addExact(millisToMicros(cal.getTimeInMillis), micros)
rebaseJulianToGregorianMicros(julianMicros)
rebaseJulianToGregorianMicros(TimeZone.getTimeZone(zoneId), julianMicros)
}

override def format(timestamp: Long): String = {
val julianMicros = rebaseGregorianToJulianMicros(timestamp)
val julianMicros = rebaseGregorianToJulianMicros(TimeZone.getTimeZone(zoneId), timestamp)
cal.setTimeInMillis(Math.floorDiv(julianMicros, MICROS_PER_SECOND) * MILLIS_PER_SECOND)
cal.setMicros(Math.floorMod(julianMicros, MICROS_PER_SECOND))
fastDateFormat.format(cal)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,47 @@ class TimestampFormatterSuite extends DatetimeFormatterSuite {
}
}

test("SPARK-[TODO]: rebasing in legacy formatters/parsers with non-default time zone") {
val defaultTimeZone = LA
withSQLConf(SQLConf.LEGACY_TIME_PARSER_POLICY.key -> LegacyBehaviorPolicy.LEGACY.toString) {
outstandingZoneIds.foreach { zoneId =>
withSQLConf(SQLConf.SESSION_LOCAL_TIMEZONE.key -> defaultTimeZone.getId) {
withDefaultTimeZone(defaultTimeZone) {
withClue(s"zoneId = ${zoneId.getId}") {
val formatters = LegacyDateFormats.values.toSeq.map { legacyFormat =>
TimestampFormatter(
TimestampFormatter.defaultPattern(),
zoneId,
TimestampFormatter.defaultLocale,
legacyFormat,
isParsing = false)
} :+ TimestampFormatter.getFractionFormatter(zoneId)
formatters.foreach { formatter =>
assert(
microsToInstant(formatter.parse("1000-01-01 01:02:03"))
.atZone(zoneId)
.toLocalDateTime === LocalDateTime.of(1000, 1, 1, 1, 2, 3))

assert(
formatter.format(
LocalDateTime.of(1000, 1, 1, 1, 2, 3).atZone(zoneId).toInstant) ===
"1000-01-01 01:02:03")
assert(
formatter.format(
instantToMicros(LocalDateTime
.of(1000, 1, 1, 1, 2, 3)
.atZone(zoneId)
.toInstant)) === "1000-01-01 01:02:03")
assert(formatter.format(java.sql.Timestamp.valueOf("1000-01-01 01:02:03")) ===
"1000-01-01 01:02:03")
}
}
}
}
}
}
}

test("parsing hour with various patterns") {
def createFormatter(pattern: String): TimestampFormatter = {
// Use `SIMPLE_DATE_FORMAT`, so that the legacy parser also fails with invalid value range.
Expand Down

0 comments on commit 885e5af

Please sign in to comment.