From f1397c40b1374f5b64dde8cea66f06a37bb843c7 Mon Sep 17 00:00:00 2001 From: sneak Date: Mon, 21 Sep 2020 16:37:01 -0700 Subject: [PATCH] adds time functions from unix micros/millis --- LICENSE | 14 ++++++++++++++ README.md | 4 ++++ util.go | 20 ++++++++++++++++++++ 3 files changed, 38 insertions(+) create mode 100644 LICENSE diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..ee7d6a5 --- /dev/null +++ b/LICENSE @@ -0,0 +1,14 @@ + DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE + Version 2, December 2004 + + Copyright (C) 2004 Sam Hocevar + + Everyone is permitted to copy and distribute verbatim or modified + copies of this license document, and changing it is allowed as long + as the name is changed. + + DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. You just DO WHAT THE FUCK YOU WANT TO. + diff --git a/README.md b/README.md index 7340fa1..636e6a0 100644 --- a/README.md +++ b/README.md @@ -6,10 +6,14 @@ general, so I put them here. Suggestions are welcome. +Really I think most of these should probably be in the stdlib. + # License WTFPL (free software, no restrictions) + + # Author * [sneak@sneak.berlin](mailto:sneak@sneak.berlin) diff --git a/util.go b/util.go index 289ec9d..ac13284 100644 --- a/util.go +++ b/util.go @@ -69,3 +69,23 @@ func CopyFile(src, dst string) (err error) { err = out.Sync() return } + +func TimeFromUnixMilli(ms int64) time.Time { + const millisInSecond = 1000 + const nsInSecond = 1000000 + return time.Unix(ms/int64(millisInSecond), (ms%int64(millisInSecond))*int64(nsInSecond)) +} + +func TimeFromUnixMicro(ms int64) time.Time { + const microInSecond = 1000000 + const nsInSecond = 1000000 + return time.Unix(ms/int64(microInSecond), (ms%int64(microInSecond))*int64(nsInSecond)) +} + +func TimeFromWebKit(input int64) time.Time { + // webkit time is stupid and uses 1601-01-01 for epoch + // it's 11644473600 seconds behind unix 1970-01-01 epoch + // it's also in microseconds + unixMicrosCorrected := input - (11644473600 * 1000000) + return TimeFromUnixMicro(unixMicrosCorrected) +}