I have set the time zone in /config/application.rb, and I expect all times generated in my app to be in this time zone by default, yet when I create a new DateTime object (using .new), it creates it in GMT. How can I get it to be in my app’s time zone?
config/application.rb
config.time_zone = 'Pacific Time (US & Canada)'
irb
DateTime.now
# => Wed, 11 Jul 2012 19:04:56 -0700
mydate = DateTime.new(2012, 07, 11, 20, 10, 0)
# => Wed, 11 Jul 2012 20:10:00 +0000 # GMT, but I want PDT
Using in_time_zone doesn’t work because that just converts the GMT time to PDT time, which is the wrong time:
mydate.in_time_zone('Pacific Time (US & Canada)')
# => Wed, 11 Jul 2012 13:10:00 PDT -07:00 # wrong time (I want 20:10)
You can use ActiveSupport’s TimeWithZone (
Time.zone) object to create and parse dates in the time zone of your application: