Rails 时区也不会被覆盖

发布于 2024-12-11 05:13:31 字数 622 浏览 0 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

江南月 2024-12-18 05:13:31

要获取当前设置时区的当前时间,您可以使用

Time.zone.now

您的服务器时区为 +3 和

Time.now.to_s   is returning this 

To get the current time in the currently set timezone you can use

Time.zone.now

Your server' time zone is +3 and

Time.now.to_s   is returning this 
冷夜 2024-12-18 05:13:31

萨哈!抱歉,我没有 15 点声望来给你升级))。
不管怎样,谢谢你的帮助。
我写了一个 TimeUtil 助手,用它来校正时间。这是我当前的伪代码:

class RacesController < ApplicationController
  def create
    @race = Race.new(params[:race])
    @race.correct_time_before_save   #can be before_save
    @race.save
  end

class Race < ActiveRecord::Base
  def correct_time_before_save
    date = self.attributes["race_date"]
    time = self.attributes["race_time"]
    datetime = Time.local(date.year, date.month, date.day, time.hour, time.min, time.sec)
    datetime_corrected = TimeUtil::override_offset(datetime)
    self.race_date = datetime_corrected.to_date
    self.race_time = datetime_corrected.to_time
  end

# TimeUtil is uses for time correction. It should be very clear, please read description before using.
# It's for time correction, when server's and user's time zones are different.
# Example: User lives in Madrid where UTC +1 hour, Server had deployed in New York, UTC -5 hours.
# When user say: I want this race to be started in 10:00.
# Server received this request, and say: Okay man, I can do it!
# User expects to race that should be started in 10:00 (UTC +1hour) = 09:00 UTC+0
# Server will start the race in 10:00 (UTC -5 hour) = 15:00 UTC+0
#
# This module brings the workaround. All that you need is to make a preprocess for all incoming Time data from users.
# Check the methods descriptions for specific info.
#
# The Time formula is:
#                       UTC + offset = local_time
#                    or
#                       UTC = local_time - offset
#

module TimeUtil

# It returns the UTC+0 DateTime object, that computed from incoming parameter  "datetime_arg".
# The offset data in "datetime_arg" is ignored - it replaces with Time.zone offset.
# Time.zone offset initialized in ApplicationController::set_timezone before-filter method.
#
def self.override_offset datetime_arg      
  Time.zone.parse(datetime_arg.strftime("%D %T")).utc
end

ActiveRecord getter 也适应用户的时区。时间以“utc+0”格式存储在数据库(mysql)中,我们希望以当前用户的时区格式获取该时间:

class Race < ActiveRecord::Base
  def race_date
    date = self.attributes["race_date"]
    time = self.attributes["race_time"]
    datetime = Time.utc(date.year, date.month, date.day, time.hour, time.min, time.sec).in_time_zone
    datetime.to_date
  end

  def race_time
    date = self.attributes["race_date"]
    time = self.attributes["race_time"]
    datetime = Time.utc(date.year, date.month, date.day, time.hour, time.min, time.sec).in_time_zone
    datetime.to_time
  end

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:

class RacesController < ApplicationController
  def create
    @race = Race.new(params[:race])
    @race.correct_time_before_save   #can be before_save
    @race.save
  end

class Race < ActiveRecord::Base
  def correct_time_before_save
    date = self.attributes["race_date"]
    time = self.attributes["race_time"]
    datetime = Time.local(date.year, date.month, date.day, time.hour, time.min, time.sec)
    datetime_corrected = TimeUtil::override_offset(datetime)
    self.race_date = datetime_corrected.to_date
    self.race_time = datetime_corrected.to_time
  end

# TimeUtil is uses for time correction. It should be very clear, please read description before using.
# It's for time correction, when server's and user's time zones are different.
# Example: User lives in Madrid where UTC +1 hour, Server had deployed in New York, UTC -5 hours.
# When user say: I want this race to be started in 10:00.
# Server received this request, and say: Okay man, I can do it!
# User expects to race that should be started in 10:00 (UTC +1hour) = 09:00 UTC+0
# Server will start the race in 10:00 (UTC -5 hour) = 15:00 UTC+0
#
# This module brings the workaround. All that you need is to make a preprocess for all incoming Time data from users.
# Check the methods descriptions for specific info.
#
# The Time formula is:
#                       UTC + offset = local_time
#                    or
#                       UTC = local_time - offset
#

module TimeUtil

# It returns the UTC+0 DateTime object, that computed from incoming parameter  "datetime_arg".
# The offset data in "datetime_arg" is ignored - it replaces with Time.zone offset.
# Time.zone offset initialized in ApplicationController::set_timezone before-filter method.
#
def self.override_offset datetime_arg      
  Time.zone.parse(datetime_arg.strftime("%D %T")).utc
end

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:

class Race < ActiveRecord::Base
  def race_date
    date = self.attributes["race_date"]
    time = self.attributes["race_time"]
    datetime = Time.utc(date.year, date.month, date.day, time.hour, time.min, time.sec).in_time_zone
    datetime.to_date
  end

  def race_time
    date = self.attributes["race_date"]
    time = self.attributes["race_time"]
    datetime = Time.utc(date.year, date.month, date.day, time.hour, time.min, time.sec).in_time_zone
    datetime.to_time
  end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文