ИМХО, один из самых быстрых способов вычислить метку времени Epoch:
scala> :paste
// Entering paste mode (ctrl-D to finish)
def epochDayForYear(year: Int): Long =
365L * year + (((year + 3) >> 2) - (if (year < 0) {
val century = year * 1374389535L >> 37 // divide int by 100 (a sign correction is not required)
century - (century >> 2)
} else ((year + 99) * 1374389535L >> 37) - ((year + 399) * 1374389535L >> 39))) // divide int by 100 and by 400 accordingly (a sign correction is not required)
def dayOfYearForYearMonth(year: Int, month: Int): Int =
((month * 1050835331877L - 1036518774222L) >> 35).toInt - // == (367 * month - 362) / 12
(if (month <= 2) 0
else if (isLeap(year)) 1
else 2)
def isLeap(year: Int): Boolean = (year & 3) == 0 && {
val century = (year * 1374389535L >> 37).toInt - (year >> 31) // divide int by 100
century * 100 != year || (century & 3) == 0
}
def secondOfDay(hour: Int, month: Int, day: Int): Int =
hour * 3600 + month * 60 + day
val (year, month, day, hour, minute, second, timeZoneOffsetHour, timeZoneOffsetMinute) =
(2016, 12, 1, 0, 0, 2, 0, 0)
val epochSecond =
(epochDayForYear(year) + (dayOfYearForYearMonth(year, month) + day - 719529)) * 86400 +
secondOfDay(hour + timeZoneOffsetHour, minute + timeZoneOffsetMinute, second) // where 719528 is days 0000 to 1970
// Exiting paste mode, now interpreting.
epochDayForYear: (year: Int)Long
dayOfYearForYearMonth: (year: Int, month: Int)Int
isLeap: (year: Int)Boolean
secondOfDay: (hour: Int, month: Int, day: Int)Int
year: Int = 2016
month: Int = 12
day: Int = 1
hour: Int = 0
minute: Int = 0
second: Int = 2
timeZoneOffsetHour: Int = 0
timeZoneOffsetMinute: Int = 0
epochSecond: Long = 1480550402
Кстати, большая часть этого кода заимствована из проекта jsoniter-scala .