0

私はrail4アプリに取り組んでいます。UTC ですべての mysql テーブルに日付を保存したい場所。ただし、ユーザーのタイムゾーンを という特定のテーブルに保存しますusers。ユーザーがログインすると、ユーザーのタイムゾーン フォームuserテーブルを取得し、セッションに保存します。

デフォルト値はconfig.time_zoneactiverecordsとactivemodelsのUTCであるため、すべてのテーブルの日付をUTCで保存できます。しかし、表示中に、ユーザーのタイムゾーンで日付を表示したいと思います。同様に、ユーザーが任意の HTML フォームで日付/時刻を入力すると、同等の UTC 形式で保存したいと考えています。

これを達成するための最良の方法は何ですか?

4

2 に答える 2

1

時刻を UTC で保存し、タイムゾーンを個別に保存できます。タイムゾーンは通常、秒単位の UTC オフセットとして格納されます (秒は SI 時間単位です)。

次に、次のように表示できます。

utime = Time.now.utc.to_i  # this value can be any format that Time.at can understand. In this example I'll use a unix timestamp in UTC. Chances are any time format you store in your DB will work.
 => 1375944780 

time = Time.at(utime)  # parses the time value (by default, the local timezone is set, e.g. UTC+08:00)
 => 2013-08-08 14:53:00 +0800 
time_in_brisbane = time.in_time_zone(ActiveSupport::TimeZone[36000])  # sets the timezone, in this case to UTC+10:00 (see http://stackoverflow.com/a/942865/72176)
 => Thu, 08 Aug 2013 16:53:00 EST +10:00 
time_brisbane.strftime("%d %b %Y, %l:%M %p %z")  # format with strftime however you like!
 => "08 Aug 2013,  4:53 PM +1000" 
于 2013-08-08T06:57:45.257 に答える