简体   繁体   English

Postgres准备的语句中的Rails时间相差1小时

[英]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. 我有一个问题,在rails中执行查询会在生成的postgres sql语句中转换错误的时间。 The time in the shell, ruby and postgres is 23:32, but in the generated sql statement it is 22:32 shell,ruby和postgres中的时间为23:32,但是在生成的sql语句中为22:32

Any Ideas whats wrong? 任何想法怎么了? I Use rails 3.2.11, pg gem 0.14.1 (installed with bundle install) 我使用rails 3.2.11,pg gem 0.14.1(捆绑安装安装)

debian system time: debian系统时间:

$ 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: 在postgres中:

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

generated rails query: 生成的rails查询:

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 . 看起来您已经在客户端中设置了UTC时区-即使这似乎与Time.now的结果相矛盾。 What do you get for Time.zone ? 您会从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. 由于夏季没有额外的DST偏移量(因为我们现在有冬天),您的时区可能是CET(欧洲中部时间),而Time.now似乎已转换为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). 您可以使用Time.now.in_time_zone("Europe/Paris") -(或任何适合您数据库时区设置的时区名称Time.now.in_time_zone("Europe/Paris")在Ruby中强制使用特定时区。

Or you can force UTC timestamps with Time.now.utc and append AT TIME ZONE 'Europe/Berlin' in Postgres. 或者,您可以使用Time.now.utc强制使用UTC时间戳,并在Postgres中的AT TIME ZONE 'Europe/Berlin' Time.now.utc添加时间戳。

Or you operate with timestamp with time zone everywhere. 或者您使用带有timestamp with time zone操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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