Rails 中的时区怪异 [英] Time Zone Weirdness in Rails

查看:37
本文介绍了Rails 中的时区怪异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个非常奇怪的 Rails 问题 - 我用谷歌搜索过这是否与已知错误有关,但没有找到任何东西 - 非常感谢有用的链接和解决方案.

This is a really weird Rails problem - I have googled to see if this is related to a known bug, but haven't found anything - would be gratfeul for useful links as well as solutions.

我可以将问题归结为:

如果我启动我的 Rails 应用程序,并通过 HTTP 请求执行以下 Rails 代码.

If I start up my Rails App, and execute the following Rails code via an HTTP request.

Time.zone = 'Europe/London'
logger.info Time.zone.inspect

日志将此显示为时区

#<ActiveSupport::TimeZone:0x3d7438c @tzinfo=nil, @name="UTC", @utc_offset=0>

在下一个请求(以及大多数后续请求)中,相同代码行的日志显示了这一点

On the very next request (and most subsequent requests), the log for the same lines of code shows this

#<ActiveSupport::TimeZone:0x46cc100 @tzinfo=#<TZInfo::DataTimezone: Europe/London>, @name="Europe/London", @utc_offset=nil>

有人知道这是怎么回事吗?

Anyone know what the hell is going on?

(如果有帮助,我正在运行 Rails 2.3.2)

(I'm running Rails 2.3.2 if it helps)

Rails 2.2.2 似乎没有这个问题,所以我现在将回到那个问题.

It appears that Rails 2.2.2 doesn't have this problem, so I will revert to that for now.

推荐答案

ActiveSupport::TimeZone 正在拦截 Time.zone 以便它可以被腌制和恢复.由于某种原因,这是懒惰地完成的(这就是为什么您的更改不会立即显示).这里有跨会话和多线程的问题,不知道对不对.我只能说:

ActiveSupport::TimeZone is intercepting Time.zone so that it can be pickled and restored. This is done lazily for some reason (which is why your changes don't show up immediately). There are cross session and multi-thread issues here, and I don't know the right of it. All I can say is:

  • 你所看到的正在发生
  • 发生这种情况是因为它是这样编码的
  • 有一些关于解决这个问题的待办事项开放
  • 不清楚(至少对我而言)全局正确"行为应该是什么.

这种特性的基本问题是没有明确的方法可以从 setter 内部辨别应用程序程序员的意图范围.这是站点范围的设置吗?会话范围的设置?一闪而过的幻想?

The basic problem with this sort of feature is that there's no clear way to discern the scope of the application programmer's intent from inside a setter. Is this a site-wide setting? A session wide setting? A passing fancy?

在约定优于配置范式中,这种事情需要约定来解决,而这方面还没有.

In the convention over configuration paradigm, such things need a convention to resolve them, and this area hasn't gotten one yet.

这篇关于Rails 中的时区怪异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆