2017-01-03 3 views
0

現在のタイムゾーンで時刻を表示するフィールドを取得しようとしています。これは「バックオフィス」アプリなので、東部標準時間(米国の&カナダ)を正しいタイムゾーンとすることができます。私は別のテーブルのstart_dateを見て、それを私のbedsheet_linesテーブルの最初のレコードのstart_dateとして使用しています。ローカルタイムゾーンで表示する時間が得られません

[![]ここに画像の説明を入力し、[1] [1]

しかし、私は(東部)適切なタイムゾーンで表示する時間を得るように見えることはできません

start_dateのは2017として格納されています-01-03 13:26:00 end_dateは2017-01-03 18:30:00と保存されています。

私は前のend_timeまたはstart_timeを取得するために使用している方法が原因であると考えています。親レコードのフォームで

私の開始時刻と終了時刻のフィールドは

<td> <%= get_start_time.strftime("%I:%M %p") %> </td> 
    <td> <%= f.time_select :end_time, ampm: true %></td> 

あるget_start_timeヘルパーメソッドは、私がget_start_timeヘルパーで何かが現在のタイムゾーン情報を削除して保存されている疑いがある

# ------------------------------------- get start time ---------------------------------------------------- 
    # This will get the start time either from the last bedsheet line or from the start time in the master bedsheet record. 

    #helper_method :get_start_time 
    def get_start_time 

    last_time = BedsheetLine.where("slitter_bedsheet_id = ?", $current_bedsheet).order(:end_time).last  # Look for any bedsheet lines for the current bedsheet. 
    # Get the last one. 

    if last_time.present?          # This will tell us if any bedsheet lines exist for the current bedsheet 
     start_time = last_time.end_time       # if it does exist, set the start time (for the new bedsheet line) to the end time of the last bs line 
    else              # If a bedsheet line does not exist, grab the time from the slitter bedsheet record. 
     start_time = SlitterBedsheet.find($current_bedsheet) # find the current record 
     start_time = (start_time.date.to_s + " " + start_time.start_time.to_s).to_datetime 
    end 

    return start_time           # send this back - NOTE - it probably comes back in a datetime format. 
    # FIXME - I think this is returning UTC instead of our current timezone - Make sure the timezone is set in the environmental config. 

ですそれをUTCとして保存する必要がある場合は、東部時間を使用します。

要約 - 親レコードに記載されているstart_timeまたは前のレコードの終了時刻をチェックして、新しいレコードの開始時刻として使用する必要があります。ストレージはUTCでなければなりませんが、近い将来のすべてのディスプレイは東部時間になります。時間はUTCに格納されている場合

[1]: https://i.stack.imgur.com/NPeIe.jpg 

答えて

1

、なぜあなたはビューにそれを変換するために.in_time_zone("Eastern Time (US & Canada)")を使用していませんか?

と同様のもの、start_time.in_time_zone("Eastern Time (US & Canada)")

関連する問題