Search code examples
ruby-on-railsrubytimezoneactivesupporttzinfo

Rails Time Zone: Setting Time.zone not Changing Time.current


In Rails 3.0.10 I'm using Time.zone to change how a user sees times of courses posted around the US. However changing the time zone doesn't seem to behave the way I would expect

Time.zone = TZInfo::Timezone.get('America/Los_Angeles')
Time.zone     # => (GMT-08:00) America/Los_Angeles
Time.current  # => 2011-10-30 14:00:00 -0700


Time.zone = TZInfo::Timezone.get('America/Chicago')
Time.zone     # => (GMT-06:00) America/Chicago
Time.current  # => 2011-10-30 14:00:00 -0700

I would expect time to change as well as the offset to accommodate for the change in Time.zone however it doesn't appear that it behaves that way.

I'm running into a problem where users in say LA, California are creating courses that end at 9pm california time, and users in different time zones are seeing the course go inactive and disappear at different times instead of at the same time. Can anyone shed some light into what setting Time.zone is actually doing and how I can get a course that ends at 9pm (PST) to disappear correctly at 11pm (CST)?


Solution

  • Replace

    Time.current
    

    With

    Time.zone.now
    

    This will give you the current time in the time zone that you've set.