Giter Site home page Giter Site logo

go-timemilli's Introduction

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

go-timemilli's People

Contributors

tigraine avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

go-timemilli's Issues

math bug?

I added a test, I think the calculation you're using is incorrect?

func FromUnixMilli(ms int64) time.Time {
	return time.Unix(ms/int64(millisInSecond), (ms%int64(millisInSecond))*int64(nsInSecond))
}

I don't think you get the integer nanoseconds portion by doing (ms%int64(millisInSecond))*int64(nsInSecond). I used this and got some incorrect results.

Test (failing) is here:

https://github.com/Tigraine/go-timemilli/pull/3/files

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.