Last active
July 25, 2022 12:36
-
-
Save rsperl/32e3acb03fa12389f96f4597a6d90fcc to your computer and use it in GitHub Desktop.
convert filetime to time.Time in #go #snippet
This file contains hidden or 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" | |
"math" | |
"os" | |
"strconv" | |
"time" | |
) | |
func main() { | |
if len(os.Args) != 2 { | |
fmt.Println("Usage: ft2date <filetime>") | |
os.Exit(1) | |
} | |
ft, err := strconv.ParseInt(os.Args[1], 10, 64) | |
if err != nil { | |
fmt.Println("invalid date time") | |
os.Exit(1) | |
} | |
t := getTime(ft) | |
fmt.Println(t) | |
fmt.Println(t.Local()) | |
} | |
func getTime(input int64) time.Time { | |
maxd := time.Duration(math.MaxInt64).Truncate(100 * time.Nanosecond) | |
maxdUnits := int64(maxd / 100) // number of 100-ns units | |
t := time.Date(1601, 1, 1, 0, 0, 0, 0, time.UTC) | |
for input > maxdUnits { | |
t = t.Add(maxd) | |
input -= maxdUnits | |
} | |
if input != 0 { | |
t = t.Add(time.Duration(input * 100)) | |
} | |
return t | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment