>我正在尝试将 rails 日期时间转换为毫秒,但是当我调用.strftime
或.to_i
时,它会忽略分配给它的时区并读取系统的时区
system timezone: Europe/London # currently is +01:00 due to BST
# application.rb
config.time_zone = 'Europe/Athens' # +03:00
stored in database as GMT: 2019-04-19 10:00:00.000000
start_date: schedule.start_date,
to_datetime: schedule.start_date.to_datetime,
strftime: schedule.start_date.to_datetime.strftime('%Q').to_i,
start_date: "2019-04-19T13:00:00.000+03:00",
to_datetime: "2019-04-19T13:00:00.000+03:00",
strftime: 1555668000000, # 2019-04-19T11:00:00.000+01:00
它在视图中正确呈现
<%= schedule.start_date %>
# 13:00:00 19 Apr 2019
irb(main):004:0> Time.at(1555668000)
=> 2019-04-19 11:00:00 +0100
如果之前已经回答过这个问题,我深表歉意,但我只是找不到令人满意的解决方案。
我最终做了这样的事情
system timezone: UTC
# application.rb
config.time_zone = 'UTC'
然后我实现了一个类似于这个的时区切换器http://railscasts.com/episodes/106-time-zones-revised
dst = Time.zone.now.dst?
offset = Time.zone.utc_offset
offset += 3600 if dst
ts = (schedule.start_date.to_i + offset) * 1000