Sub-second timestamps in Postgres

227 views
Skip to first unread message

Matt Allen

unread,
Nov 29, 2012, 9:28:13 PM11/29/12
to rails-oceania@googlegroups.com list
Afternoon All;

Has anyone stored sub-second timestamps in Postgres? and then used them in rails? Any gotchas I should be looking out for?

Cheers,

Matta
Matt Allen
ma...@devlogic.com.au
0413 777 771

Julio Cesar Ody

unread,
Nov 29, 2012, 9:32:50 PM11/29/12
to rails-...@googlegroups.com
Brief check using Ruby's Time#at, it seems to parse it wrong (output
of `+new Date` in a JS console).

Removing the milliseconds from it, parses it ok.
> --
> You received this message because you are subscribed to the Google Groups "Ruby or Rails Oceania" group.
> To post to this group, send email to rails-...@googlegroups.com.
> To unsubscribe from this group, send email to rails-oceani...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/rails-oceania?hl=en.
>

Matt Allen

unread,
Nov 29, 2012, 10:52:24 PM11/29/12
to rails-...@googlegroups.com
OK, turns out I was just not thinking it through.

if you were to hand in a decimal, like 1354246278.761 to rails and your DB column is a timestamp type. It Just Works™

Model.create column => Time.at(timestamp.to_f).utc

Will do it for you.

Sorry for the noise.

Cheers,

Matta
Matt Allen
ma...@devlogic.com.au
0413 777 771

Iain Beeston

unread,
Nov 29, 2012, 10:56:56 PM11/29/12
to rails-...@googlegroups.com

I've had problems with different operating systems not supporting the same accuracy of timestamps. The details are hazy, but I think in ruby on mac timestamps are in milliseconds whereas on linux they're in nanoseconds. I think postgres is consistent about it, but your timestamps can get truncated when saving/loading from the database

Rob Dawson

unread,
Nov 30, 2012, 5:22:39 AM11/30/12
to rails-...@googlegroups.com
Yeah -- I found jruby and ruby 1.9 give different results running on ubuntu.  

Here are the specifics:

Jruby -- stores to milli seconds

Ruby 1.9 gave to nano seconds

Postgres was storing milli seconds.

It caused some fun in tests, but rounding the time to millis in ruby time.round(3) gave consistent testable results, and meant that times were not being truncated in the database. (instead truncating them early in ruby code)

Rob
Reply all
Reply to author
Forward
0 new messages