Rails:本地和生产的时区差异

时间:2016-02-01 14:40:48

标签: ruby-on-rails-4

我有一个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)'

如何在本地和制作方面保持一致?

2 个答案:

答案 0 :(得分:1)

检查这个http://www.elabs.se/blog/36-working-with-time-zones-in-ruby-on-rails,好像你正在做一些来自dont的部分:

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)

答案 1 :(得分:0)

用于避免使用TimeZone的问题,只需使用:

Date.current     # => Mon, 01 Feb 2016
DateTime.current # => Mon, 01 Feb 2016 17:32:18 +0000
Time.current     # => Mon, 01 Feb 2016 17:33:17 UTC +00:00