Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Time.now doesn't respect config.time_zone

#2112

I am located in MST timezone but want my Rails app to run in UTC. I've set config.time_zone = 'UTC' which seems to work except that Time.now remains in MST, which produces unexpected results. Should Time.now be using the modified timezone?


# config/environment.rb
config.time_zone = 'UTC'

# console
$ script/runner "puts (Time.now + 1.day).hour, 1.day.from_now.hour"
14
21
$ script/runner "puts (Time.now + 1.day).zone, 1.day.from_now.zone"
MST
UTC

Reported by Jordan Brough · March 2nd, 2009 @ 09:55 PM

State: wontfix
Milestone: 2.x
Assigned to: Geoff Buesing Geoff Buesing
Importance: none

Activity

  1. Geoff Buesing
    Geoff Buesing
    • State changed from new to wontfix

    This is correct behavior: Time.now is not affected by config.time_zone/Time.zone.

    To get the current time in Time.zone, use Time.zone.now.

    March 2nd, 2009 @ 10:02 PM

  2. Jordan Brough
    Jordan Brough

    Thanks for the quick response. Is the reasoning for this documented anywhere? (I couldn't find it) Seems a bit unintuitive and verbose (not to mention error-prone when I forget the '.zone.').

    March 2nd, 2009 @ 10:12 PM

  3. Jordan Brough
    Jordan Brough

    The unintuitive bit being that 1.day.from_now uses config.time_zone while Time.now + 1.day doesn't.

    March 2nd, 2009 @ 10:13 PM