2011-07-21 35 views
2

我正在爲我和我的朋友創建一個賽車網站。當用戶創建他們的賬戶時,從我選擇的時區中選擇他們的時區,我已經填入了ActiveSupport::TimeZoneRails 3 - 計算用戶時間timezone

當用戶創建比賽時,他們可以選擇比賽正在進行的時間和時區。例如:

用戶已選擇他們的時區作爲距離UTC10小時的莫斯科。然後,另一個用戶創建一場比賽,並選擇布達佩斯時區下午3點的時間,距UTC的時間爲+3小時。

我一直試圖找出如何計算的比賽時間將是Budapests時區是什麼,但我掙扎,我失去了一個簡單的方法來做到這一點?

希望你明白我的意思。

EEF

回答

3

商店所有次UTC - 轉換爲使用的ActiveSupport :: Timezone.local_to_utc。

當顯示時間,轉換回本地時間用的ActiveSupport :: Timzezone.utc_to_local用戶。

= ActiveSupport::TimeZone.utc_to_local 

(from gem activesupport-3.0.8) 
------------------------------------------------------------------------------ 
    utc_to_local(time) 

------------------------------------------------------------------------------ 

Adjust the given time to the simultaneous time in the time zone represented by 
self. Returns a Time.utc() instance -- if you want an 
ActiveSupport::TimeWithZone instance, use Time#in_time_zone() instead. 
+1

乾杯幹活了! – RailsSon

+0

沒問題....很高興聽到它的工作! – Andy