Skip to content

Instantly share code, notes, and snippets.

@hlegius
Last active December 27, 2015 04:29
Show Gist options
  • Save hlegius/7266840 to your computer and use it in GitHub Desktop.
Save hlegius/7266840 to your computer and use it in GitHub Desktop.
This is the price to pay when you "love" Black Magic frameworks. As you can see, Rails' ActiveRecord will override timezone for all time/datetime attributes up to your "rails configuration". There is nothing to do about it. This is the Rails way. Nice, huh?
2.0.0p247 :001 > e = Event.new(start_date_time: DateTime.now, date_offset: 'GMT-4')
=> #<Event id: nil, start_date_time: "2013-11-01 13:03:31", date_offset: "GMT-4", created_at: nil, updated_at: nil>
2.0.0p247 :002 > e.start_date_time
=> Fri, 01 Nov 2013 11:03:31 BRST -02:00
2.0.0p247 :003 > d = DateTime.now
=> Fri, 01 Nov 2013 13:04:02 -0200
2.0.0p247 :004 > DateTime.new(d.year, d.month, d.day, d.hour, d.minute, d.second, e.date_offset)
=> Fri, 01 Nov 2013 13:04:02 -0400
@hlegius
Copy link
Author

hlegius commented Nov 1, 2013

To Railers: I am NOT trying to change the default timezone. I must define date and timezone for my own. Each Event will have a datetime and a timezone.

My target here was save the date with timezone in the start_date_time attribute, instead of saving wrong timezone and do conversions on the fly everytime.

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