6

I'm trying to make my users Time zone be the current Time zone of my application so everything they interact will be by it. I run into an ArgumentError for my method inside of my ApplicationController though.

application_controller.rb

before_filter :set_user_time_zone

private

def set_user_time_zone
  if signed_in?
   Time.zone = Time.now.in_time_zone(current_user.time_zone)
  end
end

Notes: current_user is a Devise Helper and my User model as a :time_zone column.

Than the error:

invalid argument to TimeZone[]: Mon, 20 Aug 2012 13:16:20 JST +09:00

I don't know where to go from here. Any ideas on how to correct this?

Thanks.

UPDATE

class Price
  attr_accessible :date
  def self.today
    where(:date => Date.today)
  end
end

If my method does:

def set_user_time_zone
  if signed_in?
    Time.zone = current_user.time_zone
  end
end

The problem I have my times are like this:

 Time.now = US EAST- 2012-08-22 21:17:03 -0400 
 Time.zone = TOKYO - (GMT+09:00) Tokyo 
 Time.zone.now 2012-08-23 10:17:03 +0900

Which means all of my Date methods go by

Time.now = US EAST- 2012-08-22 21:17:03 -0400

when it should be

Time.zone.now 2012-08-23 10:17:03 +0900

How can I get it to the latter?

LearningRoR
  • 26,582
  • 22
  • 85
  • 150

2 Answers2

3

Time#zone= method accepts only these params:

  • A Rails TimeZone object.

  • An identifier for a Rails TimeZone object (e.g., “Eastern Time (US & Canada)”, -5.hours).

  • A TZInfo::Timezone object.

  • An identifier for a TZInfo::Timezone object (e.g., “America/New_York”).

So you should pass something from this list

Community
  • 1
  • 1
Vasiliy Ermolovich
  • 24,459
  • 5
  • 79
  • 77
3

Time.now.in_time_zone(current_user.time_zone) returns instance of TimeWithZone class, but Time#zone= expects to get something that can be converted to TimeZone.

Assuming you store TimeZone identifiers in your :time_zone column (“Eastern Time (US & Canada)”, "Hawaii", etc.) you can simply do

Time.zone = current_user.time_zone
tomgi
  • 1,422
  • 11
  • 20
  • OK so I do that but if I write a method using `Date.today` it won't go by the current user's time zone but the default one `("Eastern Time (US & Canada)"`. My code above was an attempt at trying to correct this behavior. I'll show my code of a method I have called `today` that does what I described. – LearningRoR Aug 23 '12 at 01:12
  • I also explained everything else. – LearningRoR Aug 23 '12 at 01:22
  • 1
    Ok, now I see what you mean and basicly ... you can't do this (or at least shouldn't). Take a look at [this](http://stackoverflow.com/a/5074193/1155892) answer I would recommend you use UTC everywhere on server side and user zone only in views and helpers. – tomgi Aug 23 '12 at 09:01
  • User zone only in views and helpers? – LearningRoR Aug 23 '12 at 20:00
  • 1
    Yes, keep time in database in UTC - this is rails default and times saved via ActiveRecord will be automatically converted from user zone to UTC. In views convert it back to user's zone so he will see his local time. – tomgi Aug 23 '12 at 20:26