Skip to content

Instantly share code, notes, and snippets.

@spellgen
Created September 3, 2021 18:42
Show Gist options
  • Save spellgen/11237bf3275c8554527c3f846a72e0c4 to your computer and use it in GitHub Desktop.
Save spellgen/11237bf3275c8554527c3f846a72e0c4 to your computer and use it in GitHub Desktop.
convert webkit timestamp to go

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)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment