需要合并两组数据以创建新的DateTime对象
def mergeDateTime(date_to_merge, time_to_merge)
DateTime.new(date_to_merge.year, date_to_merge.month,date_to_merge.day, time_to_merge.hour, time_to_merge.min, time_to_merge.sec)
end
查询@signature = Signature.where('playtime_id = ?', 514).first
返回
Signature id: 834,[...], created_at: "2017-06-27 05:16:52"
并查询@interruption = Interruption.where('playtime_id = ?', 514).first
返回
Interruption id: 190, [...], pause: "2017-06-27 06:46:19"
但正在运行
mergeDateTime(@signature.created_at, @interruption.pause)
返回
Tue, 27 Jun 2017 08:46:19 +0000
这是错误的,因为它解释了GMT +2处的数据并且应该生成
Tue, 27 Jun 2017 06:46:19 +0000
这种时区调整假设如何被绝育? 和会根据date_to_merge日期考虑时区的偏移量吗?
答案 0 :(得分:0)
Specify the timezone与in_time_zone
:
def mergeDateTime(date_to_merge, time_to_merge)
date_to_merge = date_to_merge.in_time_zone('UTC')
time_to_merge = time_to_merge.in_time_zone('UTC')
DateTime.new(
date_to_merge.year,
date_to_merge.month,date_to_merge.day,
time_to_merge.hour,
time_to_merge.min,
time_to_merge.sec
)
end
答案 1 :(得分:0)
虽然其他答案指向正确的方向,但转换为UTC的时刻很重要
ts_pause_raw = @interruption.pause.in_time_zone('UTC')
ts_pause = Time.parse(ts_pause_raw.to_s[11,8])
然后可以调用def mergeDateTime(date_to_merge, time_to_merge)
而无需进一步调用in_time_zone。
发生之后,为时已晚。