views:

19

answers:

1
+2  Q: 

Getting local time

I've got this time stored as a string:

2010-07-25 04:16:25

This is the GMT time for some action I took.

Since I live in the Jerusalem time zone, I would like to show it at Jerusalem time, i.e. 07:16 in the morning, not the GMT time of 04:16:25 which is 3 hours before.

How do I properly convert it programmatically with Ruby on Rails? I seem to get lost with the multitude of timezone functions and considerations I need to take when serving users from different locations.

I tried: Time.parse("2010-07-25 04:16:25") and it gave me: "Sun Jul 25 04:16:25 +0300 2010".

I suppose the "+0300" is the difference to where I'm at?

Some light on this, or even a link to a good article that doesn't assume you know much, would help.

+1  A: 

You can define your timezone in environment.rb file (if you are using Rails 2.3.*) or application.rb (if you're using Rails 3).

Just look for section about time zones and everything is explained in comment. It will say something like this (this is from Rails 3):

# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
# config.time_zone = 'Central Time (US & Canada)'

Just uncomment that last line and you should be fine.

Slobodan Kovacevic
Hey Slobodan,Thanks for the answer, I didn't get it on my email, just on the website here. I already have the Jerusalem Time Zone set in environment.rb (and yes, I've restarted the app). I need more than that though. As I said, when I display this time, I get "Sun Jul 25 04:16:25 +0300 2010" and not my Jerusalem time. Perhaps I should move to England? :-) How would I go about displaying this obviously GMT time in my own time zone?
mjnissim
Actually, now that I look at it closer that's not UTC/GMT time. It's UTC+3, which is Jerusalem time. Think about it. When you do: Time.parse("2010-07-25 04:16:25") it gave you: "Sun Jul 25 04:16:25 +0300 2010" - which is the time you entered and Rails presumed that parsed time is in Jerusalem timezone and it returned appropriate Time object. You can tell Time.parse that you have different timezone simply by adding GMT to the end, e.g.: Time.parse("2010-07-25 04:16:25 GMT") => Sun Jul 25 07:16:25 +0300 2010
Slobodan Kovacevic