Ruby: combine Date and Time objects into a DateTime

后端 未结 6 1655
伪装坚强ぢ
伪装坚强ぢ 2020-12-08 13:18

Simple question, but I can\'t find a good or definitive answer. What is the best and most efficient way to combine Ruby Date and Time objects (objects, not

6条回答
  •  广开言路
    2020-12-08 14:19

    When using seconds_since_midnight, changes in daylight savings time can lead to unexpected results.

    Time.zone = 'America/Chicago'
    t  = Time.zone.parse('07:00').seconds_since_midnight.seconds
    d1 = Time.zone.parse('2016-11-06').to_date # Fall back
    d2 = Time.zone.parse('2016-11-07').to_date # Normal day
    d3 = Time.zone.parse('2017-03-12').to_date # Spring forward
    
    d1 + t
    #=> Sun, 06 Nov 2016 06:00:00 CST -06:00
    d2 + t
    #=> Mon, 07 Nov 2016 07:00:00 CST -06:00
    d3 + t
    #=> Sun, 12 Mar 2017 08:00:00 CDT -05:00
    

    Here's an alternative, similar to @selva-raj's answer above, using string interpolation, strftime, and parse. %F is equal to %Y-%m-%d and %T is equal to %H:%M:%S.

    Time.zone = 'America/Chicago'
    t = Time.zone.parse('07:00')
    d1 = Time.zone.parse('2016-11-06').to_date # Fall back
    d2 = Time.zone.parse('2016-11-07').to_date # Normal day
    d3 = Time.zone.parse('2017-03-12').to_date # Spring forward
    
    Time.zone.parse("#{d1.strftime('%F')} #{t.strftime('%T')}")
    #=> Sun, 06 Nov 2016 07:00:00 CST -06:00
    Time.zone.parse("#{d2.strftime('%F')} #{t.strftime('%T')}")
    #=> Sun, 07 Nov 2016 07:00:00 CST -06:00
    Time.zone.parse("#{d3.strftime('%F')} #{t.strftime('%T')}")
    #=> Sun, 12 Mar 2017 07:00:00 CDT -05:00
    

提交回复
热议问题