converting Date object to TimeWithZone

后端 未结 5 746
清歌不尽
清歌不尽 2020-12-31 18:24

I need to convert a Date object into a TimeWithZone object representing the beginning of that day in a given time zone.

The following approach works, but seems too c

5条回答
  •  孤独总比滥情好
    2020-12-31 19:02

    Subtract utc_offset:

    d = Date.today
    Time.zone.class.all.map(&:name).map { |tz| dt = d.to_datetime.in_time_zone(tz); dt -= dt.utc_offset }
    

    Using ActiveSupport::TimeZone[tz] doesn't take daylight savings time into account.

    Time.zone.class.all.map(&:name).map { |tz| o = d.to_datetime.in_time_zone(tz).utc_offset - ActiveSupport::TimeZone[tz].utc_offset }
    

提交回复
热议问题