Browse Source

adds time functions from unix micros/millis

master
Jeffrey Paul 5 months ago
parent
commit
f1397c40b1
3 changed files with 38 additions and 0 deletions
  1. +14
    -0
      LICENSE
  2. +4
    -0
      README.md
  3. +20
    -0
      util.go

+ 14
- 0
LICENSE View File

@@ -0,0 +1,14 @@
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
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.

+ 4
- 0
README.md View File

@@ -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)

+ 20
- 0
util.go View File

@@ -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)
}

Loading…
Cancel
Save