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 @@ -395,33 +395,35 @@ object DateTimeUtils {
/**
* Returns the microseconds since year zero (-17999) from microseconds since epoch.
*/
def absoluteMicroSecond(microsec: SQLTimestamp): SQLTimestamp = {
private def absoluteMicroSecond(microsec: SQLTimestamp): SQLTimestamp = {
microsec + toYearZero * MICROS_PER_DAY
}

private def localTimestamp(microsec: SQLTimestamp): SQLTimestamp = {
absoluteMicroSecond(microsec) + defaultTimeZone.getOffset(microsec / 1000) * 1000L
}

/**
* Returns the hour value of a given timestamp value. The timestamp is expressed in microseconds.
*/
def getHours(microsec: SQLTimestamp): Int = {
val localTs = absoluteMicroSecond(microsec) + defaultTimeZone.getOffset(microsec / 1000) * 1000L
((localTs / MICROS_PER_SECOND / 3600) % 24).toInt
((localTimestamp(microsec) / MICROS_PER_SECOND / 3600) % 24).toInt
}

/**
* Returns the minute value of a given timestamp value. The timestamp is expressed in
* microseconds.
*/
def getMinutes(microsec: SQLTimestamp): Int = {
val localTs = absoluteMicroSecond(microsec) + defaultTimeZone.getOffset(microsec / 1000) * 1000L
((localTs / MICROS_PER_SECOND / 60) % 60).toInt
((localTimestamp(microsec) / MICROS_PER_SECOND / 60) % 60).toInt
}

/**
* Returns the second value of a given timestamp value. The timestamp is expressed in
* microseconds.
*/
def getSeconds(microsec: SQLTimestamp): Int = {
((absoluteMicroSecond(microsec) / MICROS_PER_SECOND) % 60).toInt
((localTimestamp(microsec) / MICROS_PER_SECOND) % 60).toInt
}

private[this] def isLeapYear(year: Int): Boolean = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ class DateTimeUtilsSuite extends SparkFunSuite {
assert(getSeconds(c.getTimeInMillis * 1000) === 9)
}

test("hours / miniute / seconds") {
test("hours / minutes / seconds") {
Seq(Timestamp.valueOf("2015-06-11 10:12:35.789"),
Timestamp.valueOf("2015-06-11 20:13:40.789"),
Timestamp.valueOf("1900-06-11 12:14:50.789"),
Expand Down