Cameron
Cameron

Reputation: 28803

Ruby unix date incorrect

I have the following Unix timestamp: 1478698378000

And I'm trying to show this as a datetime in Ruby, e.g.

<%= Time.at(@timestamp).to_datetime %>

Which should be returning a date of: Wed, 09 Nov 2016 13:32:58 GMT but the above code actually returns a date of: 48828-02-01T13:26:40+00:00 Ignore formatting!

As you can see it thinks that timestamp is 2nd Feb 48828 13:26:40.

Why is the datetime coming out completely incorrect and the year so far into the future like that? Checking the timestamp on http://www.epochconverter.com/ reveals the timestamp to be correct, so it's Ruby that's returning it incorrectly.

Upvotes: 1

Views: 153

Answers (2)

Slava.K
Slava.K

Reputation: 3080

Time.at expects seconds as an argument and your timestamp is an amount of milliseconds. See documentation on Time.at

Upvotes: 5

Aleksei Matiushkin
Aleksei Matiushkin

Reputation: 121000

Why won’t you check the unix timestamp correctness against “Fashion Week Magazine” or “Cosmopolitan” Site?

Unix timestamp is an amount of seconds lasted since 1970-01-01 UTC:

date --date='@1478698378000'
mar feb  1 14:26:40 CET 48828

BTW, dropping last three zeroes gives you back what you’ve expected:

date --date='@1478698378'
mié nov  9 14:32:58 CET 2016

Upvotes: 1

Related Questions