0

I am trying to create recurring events with based on a gap in the timestamp and experiencing what seems to be odd behavior: if I continue to increase a timestamp by a weekly gap (604800 seconds), Rails seems to change the timezone on me.

t = Time.new(2015, 2, 22, 14, 0)
 => 2015-02-22 14:00:00 -0500 
gap = 60 * 60 * 24 * 7
 => 604800 
newt = t + gap
 => 2015-03-01 14:00:00 -0500 
newt + gap
 => 2015-03-08 15:00:00 -0400 

What am I doing wrong? Or what am I doing to cause this timezone issue?

Steph Rose
  • 2,126
  • 3
  • 23
  • 35

1 Answers1

1

It is because the time zone changes from standard time to daylight savings time on March 8th, 2015.

infused
  • 24,000
  • 13
  • 68
  • 78
  • One way would be to start with a time in UTC: `t = Time.new(2015, 2, 22, 14, 0).utc` – infused Feb 19 '15 at 03:42
  • The time I am starting with is in UTC, however, when I increase the timestamp by what would be 1 week, if we "gain or lose an hour" in our timezone, it messes up the time that it should be. – Steph Rose Feb 23 '15 at 22:47
  • I wonder if this is Rails version specific. Under Rails 4.2.0, as long as I start with a timezone that does not have DST then the calculation is correct even when passing through DST boundaries. – infused Feb 23 '15 at 23:12
  • I'll check my version of Rails and update you on this. – Steph Rose Feb 27 '15 at 15:28