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 @@ -286,6 +286,17 @@ object RebaseDateTime {
*/
private val gregJulianRebaseMap = loadRebaseRecords("gregorian-julian-rebase-micros.json")

private def getLastSwitchTs(rebaseMap: AnyRefMap[String, RebaseInfo]): Long = {
val latestTs = rebaseMap.values.map(_.switches.last).max
require(rebaseMap.values.forall(_.diffs.last == 0),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ideally require should be the first line in a method.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It uses latestTs in the error message. I am going to improve the message by converting micros to Instant, so, toString should form nicer string:

    require(rebaseMap.values.forall(_.diffs.last == 0),
      s"Differences between Julian and Gregorian calendar after ${microsToInstant(latestTs)} " +
      "are expected to be zero for available time zones.")

s"Differences between Julian and Gregorian calendar after ${microsToInstant(latestTs)} " +
"are expected to be zero for all available time zones.")
latestTs
}
// The switch time point after which all diffs between Gregorian and Julian calendars
// across all time zones are zero
private final val lastSwitchGregorianTs: Long = getLastSwitchTs(gregJulianRebaseMap)

private final val gregorianStartTs = LocalDateTime.of(gregorianStartDate, LocalTime.MIDNIGHT)
private final val julianEndTs = LocalDateTime.of(
julianEndDate,
Expand Down Expand Up @@ -344,13 +355,17 @@ object RebaseDateTime {
* @return The rebased microseconds since the epoch in Julian calendar.
*/
def rebaseGregorianToJulianMicros(micros: Long): Long = {
val timeZone = TimeZone.getDefault
val tzId = timeZone.getID
val rebaseRecord = gregJulianRebaseMap.getOrNull(tzId)
if (rebaseRecord == null || micros < rebaseRecord.switches(0)) {
rebaseGregorianToJulianMicros(timeZone.toZoneId, micros)
if (micros >= lastSwitchGregorianTs) {
micros
} else {
rebaseMicros(rebaseRecord, micros)
val timeZone = TimeZone.getDefault
val tzId = timeZone.getID
val rebaseRecord = gregJulianRebaseMap.getOrNull(tzId)
if (rebaseRecord == null || micros < rebaseRecord.switches(0)) {
rebaseGregorianToJulianMicros(timeZone.toZoneId, micros)
} else {
rebaseMicros(rebaseRecord, micros)
}
}
}

Expand Down Expand Up @@ -418,7 +433,9 @@ object RebaseDateTime {
// in the interval: [julianGregDiffSwitchMicros(i), julianGregDiffSwitchMicros(i+1))
private val julianGregRebaseMap = loadRebaseRecords("julian-gregorian-rebase-micros.json")

final val lastSwitchJulianTs: Long = julianGregRebaseMap.values.map(_.switches.last).max
// The switch time point after which all diffs between Julian and Gregorian calendars
// across all time zones are zero
final val lastSwitchJulianTs: Long = getLastSwitchTs(julianGregRebaseMap)

/**
* An optimized version of [[rebaseJulianToGregorianMicros(ZoneId, Long)]]. This method leverages
Expand All @@ -434,13 +451,17 @@ object RebaseDateTime {
* @return The rebased microseconds since the epoch in Proleptic Gregorian calendar.
*/
def rebaseJulianToGregorianMicros(micros: Long): Long = {
val timeZone = TimeZone.getDefault
val tzId = timeZone.getID
val rebaseRecord = julianGregRebaseMap.getOrNull(tzId)
if (rebaseRecord == null || micros < rebaseRecord.switches(0)) {
rebaseJulianToGregorianMicros(timeZone.toZoneId, micros)
if (micros >= lastSwitchJulianTs) {
micros
} else {
rebaseMicros(rebaseRecord, micros)
val timeZone = TimeZone.getDefault
val tzId = timeZone.getID
val rebaseRecord = julianGregRebaseMap.getOrNull(tzId)
if (rebaseRecord == null || micros < rebaseRecord.switches(0)) {
rebaseJulianToGregorianMicros(timeZone.toZoneId, micros)
} else {
rebaseMicros(rebaseRecord, micros)
}
}
}
}
Loading