Rails时区也不覆盖



我有一个嘈杂的问题与UTC在我的Rails项目。

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

from +0300的偏移量从哪里来??

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

Time.zone.now

您的服务器的时区是+3和

Time.now.to_s   is returning this 

啊哈!抱歉,但我没有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

相关内容

  • 没有找到相关文章