简体   繁体   中英

1 hour difference with rails time in postgres prepared statement

I have the problem, that doing a query in rails converts the the wrong time in the generated postgres sql statement. The time in the shell, ruby and postgres is 23:32, but in the generated sql statement it is 22:32

Any Ideas whats wrong? I Use rails 3.2.11, pg gem 0.14.1 (installed with bundle install)

debian system time:

$ date
Sat Feb  9 23:32:10 CET 2013

in ruby:

1.9.2p290 :004 > Time.now
=> 2013-02-09 23:32:15 +0100

in postgres:

=> select current_time;
   timetz       
--------------------
23:32:24.213487+01
(1 row)

generated rails query:

1.9.2p290 :003 > Item.where "next_crawling_at < ?", Time.now
Item Load (1.1ms)  SELECT "items".* FROM "items" WHERE (next_crawling_at < '2013-02-09 22:32:17.935595')

Looks like you have set UTC time zone in your client - even though that seems to contradict your result from Time.now . What do you get for Time.zone ?

Without the additional DST-offset for summertime (since we have winter now), your time zone is probably CET (Central European Time), while Time.now seems to be translated to UTC.

You could force a particular time zone in Ruby with Time.now.in_time_zone("Europe/Paris") - (or whatever time zone name fits the time zone setting of your DB).

Or you can force UTC timestamps with Time.now.utc and append AT TIME ZONE 'Europe/Berlin' in Postgres.

Or you operate with timestamp with time zone everywhere.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM