gpt4 book ai didi

ruby-on-rails - 理解 Rails TimeZone

转载 作者:数据小太阳 更新时间:2023-10-29 08:01:57 24 4
gpt4 key购买 nike

考虑这个要点:https://gist.github.com/752934

从 String 转换为 Time 一切顺利。它返回为 UTC。但是,当您不时转换时,它会返回 WEST 而不是 UTC(或返回 self: http://api.rubyonrails.org/classes/Time.html#method-i-to_time )

知道为什么时区从 UTC 更改为 WES​​T 吗?

提前致谢,数据库管理员

最佳答案

我猜它遇到了与 DateTime.to_time 相同的问题。这是作为 Ruby 中的错误提交的,但被拒绝了。更多信息在这里:

http://redmine.ruby-lang.org/issues/show/3737

更新: https://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/time/conversions.rb

API 文档似乎已过时。该方法有一个条件,用于检查 to_time 是否已定义。所以看起来它甚至没有使用这个 ext 方法:

  # A method to keep Time, Date and DateTime instances interchangeable on conversions.
# In this case, it simply returns +self+.
def to_time
self
end unless method_defined?(:to_time)

这是提交:https://github.com/rails/rails/blob/4817bf94d135c44ddfae1a30acb15de989e3c86c/activesupport/lib/active_support/core_ext/time/conversions.rb

我测试了猴子补丁,它的工作方式与您预期的一样:

ruby-1.9.2-p0 > class Time
ruby-1.9.2-p0 ?> def to_time
ruby-1.9.2-p0 ?> self
ruby-1.9.2-p0 ?> end
ruby-1.9.2-p0 ?>end
=> nil
ruby-1.9.2-p0 > Time.zone = Time.zone_default = "UTC"
=> "UTC"
ruby-1.9.2-p0 > t = "2008-04-01".to_time
=> 2008-04-01 00:00:00 UTC
ruby-1.9.2-p0 > p t.zone, t.to_time.zone
"UTC"
"UTC"
=> ["UTC", "UTC"]

我会认为这是一个错误

关于ruby-on-rails - 理解 Rails TimeZone,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4518967/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com