Skip to content

Instantly share code, notes, and snippets.

@orca-zhang
Created March 19, 2019 13:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save orca-zhang/bfd4a08dbf9a99b4130ffee2cb70a0eb to your computer and use it in GitHub Desktop.
Save orca-zhang/bfd4a08dbf9a99b4130ffee2cb70a0eb to your computer and use it in GitHub Desktop.
Fast conversion from date to Unix Timestamp
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