Created
August 18, 2022 21:56
Parse internal go time.Time wall
This file contains 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
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