Date calculations can overflow with webkit times (microseconds since Jan 1 1601).
This works in go1.17
package main | |
import ( | |
"fmt" | |
"time" | |
) | |
var webkitToUnixDiff int64 // duration between epochs in microseconds | |
func init() { | |
var epoch1, epoch2 time.Time | |
epoch1 = time.Date(1601, 1, 1, 0, 0, 0, 0, time.UTC) | |
epoch2 = time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC) | |
webkitToUnixDiff = epoch2.UnixMicro()-epoch1.UnixMicro() // pre-calculate the epoch difference | |
} | |
func webkitToTime(wkt int64) time.Time { | |
return time.UnixMicro(wkt - webkitToUnixDiff) | |
} | |
func main() { | |
fmt.Printf("webkitToUnixDiff=%d, years=%d\n", webkitToUnixDiff, webkitToUnixDiff/1000000/3600/24/365) | |
fmt.Println(webkitToTime(13260664392205302).In(time.UTC)) // Friday, March 19, 2021 9:53:12 PM (UTC) | |
} |