Rails 时区也不会被覆盖
我的 Rails 项目中存在 UTC 的噪音问题。
class ApplicationController < ActionController::Base
before_filter :set_timezone
def set_timezone
Time.zone = current_user.time_zone if current_user
end
凉爽的。我覆盖了时区。 现在,服务器的时区是+3。用户的时区是+5。我希望任何对 Time 的请求都应该获取用户的时区,但是此代码返回的不是预期值:
render :text => Time.zone.to_s + "<br/>" +
Time.now.to_s + "<br/>" +
Time.now.in_time_zone.to_s
结果:
(GMT+05:00) Tashkent
Thu Oct 20 19:41:11 +0300 2011
2011-10-20 21:41:11 +0500
从 +0300 偏移量来自哪里?
I have a noisy problems with UTC on my Rails project.
class ApplicationController < ActionController::Base
before_filter :set_timezone
def set_timezone
Time.zone = current_user.time_zone if current_user
end
Cool. I overrided the time zone.
And now, server' time zone is +3. User's time zone is +5. I hope that any requests to Time should get the User's time zone, but this code returns not expected values:
render :text => Time.zone.to_s + "<br/>" +
Time.now.to_s + "<br/>" +
Time.now.in_time_zone.to_s
RESULT:
(GMT+05:00) Tashkent
Thu Oct 20 19:41:11 +0300 2011
2011-10-20 21:41:11 +0500
Where does from +0300 offset comes??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要获取当前设置时区的当前时间,您可以使用
您的服务器时区为 +3 和
To get the current time in the currently set timezone you can use
Your server' time zone is +3 and
萨哈!抱歉,我没有 15 点声望来给你升级))。
不管怎样,谢谢你的帮助。
我写了一个 TimeUtil 助手,用它来校正时间。这是我当前的伪代码:
ActiveRecord getter 也适应用户的时区。时间以“utc+0”格式存储在数据库(mysql)中,我们希望以当前用户的时区格式获取该时间:
saha! Sorry, but I have not a 15 points of reputation to give you the level-up)).
Anyway thanks for your help.
I wrote a TimeUtil helper, an uses it for time correction. This is my current pseudo-code:
ActiveRecord getters adapted to user's time zones too. Time is stored in database (mysql) in "utc+0" format, and we want to get this time in current user's timezone format: