我有一个 Sidekiq 工作程序,从远程 Oracle 数据库获取记录并将其保存在 postgres 数据库中。大多数数据由日期和时间字段组成。我在生产中遇到的问题是,它保存了不同时区的数据时间,相差 5 小时。虽然它在本地运行良好。这是一个例子:
From Oracle DB => 2016-01-27 07:59:29 +0500
Saved on local => 2016-01-27 07:59:29 -0500
On Production => 2016-01-27 02:59:29 -0500
在 config/application.rb 中我设置了:
config.time_zone = 'Eastern Time (US & Canada)'
我应该怎样做才能使其在本地和生产上保持一致?
最佳答案
检查这个http://www.elabs.se/blog/36-working-with-time-zones-in-ruby-on-rails ,看起来你正在做一些不该做的事情:
Time.now # Returns system time and ignores your configured time zone. (2015-08-27 14:09:36 +0200)
Time.parse("2015-08-27T12:09:36Z") # Will assume time string given is in the system's time zone. (2015-08-27 12:09:36 UTC)
Time.strptime("2015-08-27T12:09:36Z", "%Y-%m-%dT%H:%M:%S%z") # Same problem as with Time.parse. (2015-08-27 12:09:36 UTC)
Date.today # This could be yesterday or tomorrow depending on the machine's time zone, see https://github.com/ramhoj/time-zone-article/issues/1 for more info. (Thu, 27 Aug 2015)
关于ruby-on-rails-4 - 导轨 : Timezone difference on local and production,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35133302/