Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse unix timestamp with decimal from float using time.Unix()?

This is pretty straightforward, but I couldn't find a response and figured that others might have the same question.

I have a Unix timestamp as a float, that includes a decimal value for fractions of seconds. What's the conversion factor to pass this float as two separate integers to the time package's time.Unix(sec int64, nsec int64) function, without losing the decimal precision?

like image 478
Ben Guild Avatar asked Jun 04 '16 08:06

Ben Guild


People also ask

How do I convert timestamp to time in Unix?

The getTime method returns the number of milliseconds since the Unix Epoch (1st of January, 1970 00:00:00). To get a Unix timestamp, we have to divide the result from calling the getTime() method by 1000 to convert the milliseconds to seconds. What is this?

Is Unix timestamp a float?

Current Unix Timestamptime() function by itself returns the Unix timestamp as a float , with any value to the right of the decimal point representing fractions of a second.

Can epoch time have decimals?

No. epoch time is how time is kept track of internally in UNIX. It's seconds, counting upward from January 1st, 1970. This number hit 1 million (1,000,000) in March of 1973, and will hit one billion (1,000,000,000) on Sun Sep 9 01:46:39 2001 UTC.

Is Unix timestamp int or float?

Unix timestamps are always integers.


1 Answers

sec, dec := math.Modf(timeFloat);
time.Unix(int64(sec), int64(dec*(1e9)));
like image 157
Ben Guild Avatar answered Sep 27 '22 20:09

Ben Guild