2011-10-20 16 views
1

私の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です。私は時間の任意の要求は、ユーザーのタイムゾーンを取得する必要があることを願っていますが、このコードは値を期待できない戻り値:

render :text => Time.zone.to_s + "<br/>" + 
       Time.now.to_s + "<br/>" + 
       Time.now.in_time_zone.to_s 

結果:

0300から来オフセットん
(GMT+05:00) Tashkent 
Thu Oct 20 19:41:11 +0300 2011 
2011-10-20 21:41:11 +0500 

答えて

1

は、あなたが使用することができ、現在設定されたタイムゾーンの現在時刻を取得するには

Time.zone.now 

お使いのサーバのタイムゾーンは3と

Time.now.to_s is returning this 
+0

、bilash.saha – Vitalyp

1

SAHAです!申し訳ありませんが、私はあなたにレベルアップを与えるための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ゲッターもユーザーのタイムゾーンに適合しています。時間は「UTC + 0」の形式でデータベース(MySQLの)に保存されている、と私たちは、現在のユーザーのタイムゾーン形式で、この時間を取得したいされていますTime.zone.nowはクールだ

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 
+0

@ありがとうあ、ごめんなさい !!!私はそれに気づいていなかった。幸運のベスト –

関連する問題