Skip to content

Instantly share code, notes, and snippets.

@dustMason
Created August 18, 2022 21:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dustMason/384e5104a8e5f6724efe3442603fe9aa to your computer and use it in GitHub Desktop.
Save dustMason/384e5104a8e5f6724efe3442603fe9aa to your computer and use it in GitHub Desktop.
Parse internal go time.Time wall
package main
import (
"fmt"
"time"
)
const (
secondsPerMinute = 60
secondsPerHour = 60 * secondsPerMinute
secondsPerDay = 24 * secondsPerHour
nsecMask = 1<<30 - 1
wallToInternal int64 = (1884*365 + 1884/4 - 1884/100 + 1884/400) * secondsPerDay
nsecShift = 30
unixToInternal int64 = (1969*365 + 1969/4 - 1969/100 + 1969/400) * secondsPerDay
internalToUnix int64 = -unixToInternal
)
func main() {
loc, _ := time.LoadLocation("America/Los_Angeles")
var wall uint64 = 13886766125597691483
nanos := int32(wall & nsecMask)
fmt.Println(nanos)
secs := wallToInternal + int64(wall<<1>>(nsecShift+1))
fmt.Println(secs + internalToUnix)
t := time.Unix(secs+internalToUnix, int64(nanos))
fmt.Println(t)
fmt.Println(t.In(loc))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment