Skip to content

Commit 8670bb6

Browse files
committed
Add back tests and add some more notes
1 parent 7c28252 commit 8670bb6

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/DateTimeUtils.scala

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -877,11 +877,17 @@ object DateTimeUtils {
877877
val hh = seconds / 3600
878878
val mm = seconds / 60 % 60
879879
val ss = seconds % 60
880+
val ms = millisOfDay % 1000
880881
val calendar = Calendar.getInstance(tz)
881-
882-
calendar.set(year, month, day, hh, mm, ss)
883-
calendar.getTime() // Set the timezone info
882+
calendar.set(year, month - 1, day, hh, mm, ss)
883+
calendar.set(Calendar.MILLISECOND, ms)
884+
val date = calendar.getTime()
885+
// TODO decide between return the offset from the calendar:
884886
guess = calendar.get(Calendar.ZONE_OFFSET) + calendar.get(Calendar.DST_OFFSET)
887+
// And returning the difference between timestamps (end up broken in different ways)
888+
// This way appears to return a non-DST value even when in DST - but our round trips tests
889+
// will work.
890+
// guess = (millisLocal - calendar.getTimeInMillis()).toInt
885891
}
886892
}
887893
guess

sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/util/DateTimeUtilsSuite.scala

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,29 @@ class DateTimeUtilsSuite extends SparkFunSuite {
536536
}
537537
}
538538

539+
test("daysToMillis and millisToDays") {
540+
// There are some days are skipped entirely in some timezone, skip them here.
541+
val skipped_days = Map[String, Int](
542+
"Kwajalein" -> 8632,
543+
"Pacific/Apia" -> 15338,
544+
"Pacific/Enderbury" -> 9131,
545+
"Pacific/Fakaofo" -> 15338,
546+
"Pacific/Kiritimati" -> 9131,
547+
"Pacific/Kwajalein" -> 8632,
548+
"MIT" -> 15338)
549+
for (tz <- DateTimeTestUtils.ALL_TIMEZONES) {
550+
DateTimeTestUtils.withDefaultTimeZone(tz) {
551+
val skipped = skipped_days.getOrElse(tz.getID, Int.MinValue)
552+
(-20000 to 20000).foreach { d =>
553+
if (d != skipped) {
554+
assert(millisToDays(daysToMillis(d)) === d,
555+
s"Round trip of ${d} did not work in tz ${tz}")
556+
}
557+
}
558+
}
559+
}
560+
}
561+
539562
test("convert TZ with boundary time pacific") {
540563
val tz = TimeZone.getTimeZone("America/Los_Angeles")
541564
val boundaryDates = List(

0 commit comments

Comments
 (0)