Skip to content

Instantly share code, notes, and snippets.

@ramhoj
Created March 23, 2012 13:04
Show Gist options
  • Select an option

  • Save ramhoj/2170462 to your computer and use it in GitHub Desktop.

Select an option

Save ramhoj/2170462 to your computer and use it in GitHub Desktop.
Time.zone.parse bug
# Use
DateTime.parse("2012-03-25 03:29").in_time_zone(Time.zone)
# Instead of
Time.zone.parse("2012-03-25 03:29")
# Until the pull request https://github.com/jarkko/rails/commit/bb4a1d68f6db8bf99d2b6e21eee72a19d494dee0 has been pulled into Rails.
@jarkko
Copy link
Copy Markdown

jarkko commented Mar 23, 2012

Actually this is how to work around it:

ActiveSupport::TimeWithZone.new(nil, Time.zone, DateTime.parse("2012-03-25 03:29"))

Reason:

1.9.3p125 :099 > Time.zone
 => (GMT-08:00) Pacific Time (US & Canada) 
1.9.3p125 :100 > DateTime.parse("2012-03-25 03:29").in_time_zone(Time.zone)
 => Sat, 24 Mar 2012 20:29:00 PDT -07:00 
1.9.3p125 :101 > ActiveSupport::TimeWithZone.new(nil, Time.zone, DateTime.parse("2012-03-24 03:29"))
 => Sat, 24 Mar 2012 03:29:00 PDT -07:00 

@ramhoj
Copy link
Copy Markdown
Author

ramhoj commented Mar 23, 2012

facepalm Thanks again Jarkko!

Are you okay with me adding your findings to my blog post?

Bug in Time.zone.parse

Jarkko Laine (@jarkko) pointed out that there's currently a bug in Rails that can have Time.zone.parse lose an hour when your system time is in DST (daylight saving time) and your configured time zone isn't. Jarkko has posted an issue on Rails' issue tracker and written a patch to correct the bug. Until the patch has been accepted or if you're running with older versions of Rails the only safe way to avoid this bug is to either monkey patch Rails in your app with Jarkko's fix or use:

# use
ActiveSupport::TimeWithZone.new(nil, Time.zone, DateTime.parse("2012-03-25 03:29"))
# => Sun, 25 Mar 2012 03:29:00 PDT -07:00

# or if possible pass the time zone in the string
Time.zone.parse("2012-03-25 03:29 PDT")
# => Sun, 25 Mar 2012 04:29:00 PDT -07:00

# instead of
Time.zone.parse("2012-03-25 03:29")
# => Sun, 25 Mar 2012 04:29:00 PDT -07:00

It should however be mentioned that it's pretty rare that this bug surfaces and when it does it can only lose you one hour. If you can live with that you probably do best by just waiting for the patch to be accepted.

@jarkko
Copy link
Copy Markdown

jarkko commented Mar 23, 2012

Looks dandy. Note though that the second example shouldn't push the time up by an hour. The problem of course is that if you'd use PST instead (i.e. are not sure whether it is or is not DST at that point), it would break:

1.9.3p125 :104 > Time.zone.parse("2012-03-25 03:29 PDT")
 => Sun, 25 Mar 2012 03:29:00 PDT -07:00 
1.9.3p125 :105 > Time.zone.parse("2012-03-25 03:29 PST")
 => Sun, 25 Mar 2012 04:29:00 PDT -07:00 

@jarkko
Copy link
Copy Markdown

jarkko commented Mar 23, 2012

So yeah, go ahead :-)

@ramhoj
Copy link
Copy Markdown
Author

ramhoj commented Mar 23, 2012

Damn you copy-pate! :).

Thanks for catching that. The blog post is now updated at http://elabs.se/blog/36-working-with-time-zones-in-ruby-on-rails#bug_in_timezoneparse

I really appreciate you going down to the bottom of this issue. I know that I've seen it briefly before but it went a way and I didn't had the time to understand what was really going on. Great to have it all figured out now!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment