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
Improve the comment for toJavaDate
  • Loading branch information
MaxGekk committed Jun 3, 2020
commit 2fcdaed6d6a8e1fc6bde8c7c63bdc8d8eb79f301
Original file line number Diff line number Diff line change
Expand Up @@ -112,22 +112,17 @@ object DateTimeUtils {
}

/**
* The opposite to `fromJavaDate` method which converts a number of days to an
* instance of `java.sql.Date`. It builds a local date in Proleptic Gregorian
* calendar, extracts date fields `year`, `month`, `day`, and creates a local
* date in the hybrid calendar (Julian + Gregorian calendars) from the fields.
* Converts days since the epoch 1970-01-01 in Proleptic Gregorian calendar to a local date
* at the given time zone in the hybrid calendar (Julian + Gregorian). It rebases the given
* days from Proleptic Gregorian to the hybrid calendar at UTC time zone for simplicity because
* the difference between two calendars doesn't depend on any time zone. The result is shifted
* by time zone offset in wall clock to have the same date fields (year, month, day)
* at the given `timeZone` as the input `daysSinceEpoch` in Proleptic Gregorian calendar.
*
* The purpose of the conversion is to have the same local date as the triple
* of `year`, `month`, `day` in the original Proleptic Gregorian calendar and
* in the target calender.
*
* @param daysSinceEpoch The number of days since 1970-01-01.
* @return A `java.sql.Date` from number of days since epoch.
* @param daysSinceEpoch The number of days since 1970-01-01 in Proleptic Gregorian calendar.
* @param timeZone The time zone of the desired local date.
* @return A local date in the hybrid calendar as `java.sql.Date` from number of days since epoch.
*/
def toJavaDate(daysSinceEpoch: SQLDate): Date = {
toJavaDate(daysSinceEpoch, TimeZone.getDefault)
}

def toJavaDate(daysSinceEpoch: SQLDate, timeZone: TimeZone): Date = {
val rebasedDays = rebaseGregorianToJulianDays(daysSinceEpoch)
val localMillis = Math.multiplyExact(rebasedDays, MILLIS_PER_DAY)
Expand All @@ -138,6 +133,10 @@ object DateTimeUtils {
new Date(localMillis - timeZoneOffset)
}

def toJavaDate(daysSinceEpoch: SQLDate): Date = {
toJavaDate(daysSinceEpoch, TimeZone.getDefault)
}

/**
* Converts microseconds since the epoch to an instance of `java.sql.Timestamp`
* via creating a local timestamp at the system time zone in Proleptic Gregorian
Expand Down