为什么 Time.now.today? return false 在我的 Rails 4 申请中?
Why does Time.now.today? return false in my Rails 4 application?
2016 年 2 月 5 日是东部标准时区(美国)8:04PM。
当我运行这个命令
Time.now.today?
#=> false
它 returns 错误。
为什么,我该如何纠正?谢谢
详情:
- Rails 4.0.0
- Ruby 2.2.1p85
- Mac OS X 埃尔卡皮坦
也许您的 Rails 应用配置(在 config/application.rb 或 config/environments/*.rb 中)
config.time_zone = 'UTC'
并且您的 Mac 在美国东部标准时间,反之亦然。设置我手边 Rails 4.1 应用程序的时区 'UTC'
重现了我的问题。
如果是这样,则出现差异是因为在您 运行 命令时,UTC 日期比您所在时区的日期晚一天。
我认为您的问题可能与时区设置有关。当您 运行 在 8:04PM EST
时,即 1:04AM UTC
,您的时区似乎设置为 UTC
因此 1:04AM UTC
是明天 (Feb 6, 2016
) .
我会检查你的 config/application.rb
并尝试将你的时区更改为 Eastern Time (US & Canada)
,从你上面的评论来看它目前设置为 UTC
(默认)。
运行 命令与 (Eastern Time (US & Canada)
:
Time.now
=> 2016-02-05 22:42:08 -0500
Time.now.today?
=> true
Date.today
=> Fri, 05 Feb 2016
Time.zone
=> #<ActiveSupport::TimeZone:0x007f9a1bcffd20 @name="Eastern Time (US & Canada)", @utc_offset=nil, @tzinfo=#<TZInfo::TimezoneProxy: America/New_York>, @current_period=#<TZInfo::TimezonePeriod: #<TZInfo::TimezoneTransitionDefinition: #<TZInfo::TimeOrDateTime: 1446357600>,#<TZInfo::TimezoneOffset: -18000,0,EST>>,#<TZInfo::TimezoneTransitionDefinition: #<TZInfo::TimeOrDateTime: 1457852400>,#<TZInfo::TimezoneOffset: -18000,3600,EDT>>>>
当我运行相同的命令时(UTC
时区):
注意 Time.now.in_time_zone
实际上是明天,因此 Time.now.today? => false
Time.now
=> 2016-02-05 22:27:15 -0500
Time.now.today?
=> false
Time.now.in_time_zone
=> Sat, 06 Feb 2016 03:27:29 UTC +00:00
Date.today
=> Fri, 05 Feb 2016
Date.current.in_time_zone
=> Sat, 06 Feb 2016 00:00:00 UTC +00:00
Time.zone
=> #<ActiveSupport::TimeZone:0x007fa19313f7e0 @name="UTC", @utc_offset=nil, @tzinfo=#<TZInfo::TimezoneProxy: Etc/UTC>, @current_period=#<TZInfo::TimezonePeriod: nil,nil,#<TZInfo::TimezoneOffset: 0,0,UTC>>>>
人们经常混淆的一件事是 Time.now
和 Time.current
是完全不同的对象。
Time.now
returns Ruby 图书馆的时间 class 原样。 return是系统时间。
Time.current
returns Rails ActiveSupport 库修改的时间。 returns系统时间由Railstime_zone设置修改。
today?
方法在ActiveSupport::TimeWithZone
中声明,它根据Time.current
而不是Time.now
.
判断给定对象的时间是否为今天
因此,如果您的 Time.now
和 Time.current
有不同的日期,调用 Time.now.today?
将 return 错误。
如果两者代表同一天,Time.now.today?
将 return 为真。
Time.current.today?
应该总是 return 正确。
除了一些特殊情况,最好避免Time.now
,在Rails中一直使用Time.current
。
纯ruby中没有Time
的#today?
方法,由Rails
引入。由于 Rails 使用区域时间模型,我相信它 returns false
因为当前时间与 UTC
有差距(当然 rails 应该配置为 UTC
), 要正确确保 #today?
总是 return true
, 使用 Time.zone
构造, 所以你会得到:
Time.zone.now.today? # => true
2016 年 2 月 5 日是东部标准时区(美国)8:04PM。
当我运行这个命令
Time.now.today?
#=> false
它 returns 错误。
为什么,我该如何纠正?谢谢
详情:
- Rails 4.0.0
- Ruby 2.2.1p85
- Mac OS X 埃尔卡皮坦
也许您的 Rails 应用配置(在 config/application.rb 或 config/environments/*.rb 中)
config.time_zone = 'UTC'
并且您的 Mac 在美国东部标准时间,反之亦然。设置我手边 Rails 4.1 应用程序的时区 'UTC'
重现了我的问题。
如果是这样,则出现差异是因为在您 运行 命令时,UTC 日期比您所在时区的日期晚一天。
我认为您的问题可能与时区设置有关。当您 运行 在 8:04PM EST
时,即 1:04AM UTC
,您的时区似乎设置为 UTC
因此 1:04AM UTC
是明天 (Feb 6, 2016
) .
我会检查你的 config/application.rb
并尝试将你的时区更改为 Eastern Time (US & Canada)
,从你上面的评论来看它目前设置为 UTC
(默认)。
运行 命令与 (Eastern Time (US & Canada)
:
Time.now
=> 2016-02-05 22:42:08 -0500
Time.now.today?
=> true
Date.today
=> Fri, 05 Feb 2016
Time.zone
=> #<ActiveSupport::TimeZone:0x007f9a1bcffd20 @name="Eastern Time (US & Canada)", @utc_offset=nil, @tzinfo=#<TZInfo::TimezoneProxy: America/New_York>, @current_period=#<TZInfo::TimezonePeriod: #<TZInfo::TimezoneTransitionDefinition: #<TZInfo::TimeOrDateTime: 1446357600>,#<TZInfo::TimezoneOffset: -18000,0,EST>>,#<TZInfo::TimezoneTransitionDefinition: #<TZInfo::TimeOrDateTime: 1457852400>,#<TZInfo::TimezoneOffset: -18000,3600,EDT>>>>
当我运行相同的命令时(UTC
时区):
注意 Time.now.in_time_zone
实际上是明天,因此 Time.now.today? => false
Time.now
=> 2016-02-05 22:27:15 -0500
Time.now.today?
=> false
Time.now.in_time_zone
=> Sat, 06 Feb 2016 03:27:29 UTC +00:00
Date.today
=> Fri, 05 Feb 2016
Date.current.in_time_zone
=> Sat, 06 Feb 2016 00:00:00 UTC +00:00
Time.zone
=> #<ActiveSupport::TimeZone:0x007fa19313f7e0 @name="UTC", @utc_offset=nil, @tzinfo=#<TZInfo::TimezoneProxy: Etc/UTC>, @current_period=#<TZInfo::TimezonePeriod: nil,nil,#<TZInfo::TimezoneOffset: 0,0,UTC>>>>
人们经常混淆的一件事是 Time.now
和 Time.current
是完全不同的对象。
Time.now
returns Ruby 图书馆的时间 class 原样。 return是系统时间。
Time.current
returns Rails ActiveSupport 库修改的时间。 returns系统时间由Railstime_zone设置修改。
today?
方法在ActiveSupport::TimeWithZone
中声明,它根据Time.current
而不是Time.now
.
因此,如果您的 Time.now
和 Time.current
有不同的日期,调用 Time.now.today?
将 return 错误。
如果两者代表同一天,Time.now.today?
将 return 为真。
Time.current.today?
应该总是 return 正确。
除了一些特殊情况,最好避免Time.now
,在Rails中一直使用Time.current
。
纯ruby中没有Time
的#today?
方法,由Rails
引入。由于 Rails 使用区域时间模型,我相信它 returns false
因为当前时间与 UTC
有差距(当然 rails 应该配置为 UTC
), 要正确确保 #today?
总是 return true
, 使用 Time.zone
构造, 所以你会得到:
Time.zone.now.today? # => true