Skip to content

Function to convert a Unix Millisecond Epoch Timestamp to time.Time

License

Notifications You must be signed in to change notification settings

sneak/go-timemilli

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 

Repository files navigation

go-timemilli

The go standard library does not contain a convenient conversion function from Unix milliseconds to time.Time.

The only method to convert a unix timestamp is using time.Unix which only takes seconds and nanoseconds. Thus making converting milliseconds a tad too complicated if you don't want to sacrifice accuracy.

You either have to multiply the millis to nanoseconds or split them into seconds and nanoseconds. This small library (3 LOC) fixes that by providing a convenience function for the conversion.

Usage

import "tigraine/go-timemilli"

msTime := 1542810446506
ts := timemilli.FromUnixMilli(msTime)

Performance Considerations

Shockingly multiplying the millisecond timestamp by 1.000.000 to get nanoseconds is half as slow as turning milliseconds into seconds and then turning the remainder into nanos.

timemilli_test.go contains two benchmark tests I used to verify this:

goos: darwin
goarch: amd64
pkg: github.com/tigraine/go-timemillis
BenchmarkMult-8         2000000000               0.50 ns/op
BenchmarkDiv-8          2000000000               0.25 ns/op

About

Function to convert a Unix Millisecond Epoch Timestamp to time.Time

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 100.0%