Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 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 @@ -385,9 +385,16 @@ object DateTimeUtils {
* microseconds where microsecond 0 is 1970-01-01 00:00:00Z.
*/
def instantToMicros(instant: Instant): Long = {
val us = Math.multiplyExact(instant.getEpochSecond, MICROS_PER_SECOND)
val result = Math.addExact(us, NANOSECONDS.toMicros(instant.getNano))
result
val secs = instant.getEpochSecond
if (secs == Math.floorDiv(Long.MinValue, MICROS_PER_SECOND)) {
Comment thread
dgd-contributor marked this conversation as resolved.
Outdated
val us = Math.multiplyExact(secs + 1, MICROS_PER_SECOND)
Comment thread
dgd-contributor marked this conversation as resolved.
val result = Math.addExact(us, NANOSECONDS.toMicros(instant.getNano) - MICROS_PER_SECOND)
Comment thread
dgd-contributor marked this conversation as resolved.
Outdated
result
} else {
val us = Math.multiplyExact(secs, MICROS_PER_SECOND)
val result = Math.addExact(us, NANOSECONDS.toMicros(instant.getNano))
result
Comment thread
dgd-contributor marked this conversation as resolved.
Outdated
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -820,4 +820,9 @@ class DateTimeUtilsSuite extends SparkFunSuite with Matchers with SQLHelper {
}
}
}

test("SPARK-35679: instantToMicros should be able to return microseconds of Long.MinValue") {
assert(instantToMicros(microsToInstant(Long.MaxValue)) === Long.MaxValue)
assert(instantToMicros(microsToInstant(Long.MinValue)) === Long.MinValue)
}
}