Created
March 19, 2019 13:19
-
-
Save orca-zhang/bfd4a08dbf9a99b4130ffee2cb70a0eb to your computer and use it in GitHub Desktop.
Fast conversion from date to Unix Timestamp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func toUnix(year, month, day, hour, min, sec int) int64 { | |
if month < 1 || month > 12 { | |
return -1 | |
} | |
leap := 0 | |
if (year%4 == 0 && (year%100 != 0 || year%400 == 0)) && month >= 3 { | |
leap = 1 // February 29 | |
} | |
return int64((365*year - 719528 + day - 1 + (year+3)/4 - (year+99)/100 + (year+399)/400 + int([]int{0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}[month-1]) + leap) * 86400 + hour * 3600 + min * 60 + sec) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment