views:

753

answers:

4

I've been pulling my hair out trying to work with Time in Rails. Basically I need to set all time output (core as well as ActiveSupport) to the server's local time -- no GMT, no UTC, etc. I've seen various posts relating to Time, but they usually involve someone's need to set it for each user. Mine isn't nearly as complex, I simply want consistency when I use any Time object. (I'd also appreciate not receiving errors every 3 seconds telling me that I can't convert a Fixnum (or some other type) to string -- it's Ruby, just do it!)

I also seem to be getting drastically different times for Time.new vs the ActiveSupport 1.second.ago. Anyway, does anyone have any quality suggestions as regards working with Time in Rails?

A: 

If you just want Time objects to be consistent, then why not stick with UTC? I just tried Time.new and 1.second.ago using script/console and I get the same output (give or take a second for typing the command). How are you doing it?

John Topley
I apologize. You're correct. The *information* returned is the same. However, I am experiencing errors when it comes to printing the values out to strings. Conversions "to_i" and back again get jumbled and miscalculated. As I said, I don't want UTC and I don't want to have to perpetually convert (as I use it in a lot of places). I simply want all Times returned as local server time.Thanks
humble_coder
+1  A: 

Somewhere in your initializers, define the format(s) that you want to use.

ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.merge!(:default => '%m/%d/%Y %H:%M')
ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.merge!(:my_special_format => '%H:%M %p')

Then when you want to print a Time object, it works like the following example. Notice that the Time object in my console is already aware of my time zone. I'm not performing any magical transformations here.

>> t = Time.now
=> Wed Jul 15 18:47:33 -0500 2009
>> t.to_s
=> "07/15/2009 18:47"
>> t.to_s(:my_special_format)
=> "18:47 PM"

Calling Time#to_s uses the :default format, or you can pass in the name of the format you'd rather use like I did with :my_special_format.

You can see the various options for formatting a Time object here.

jdl
A: 

If u don't want to store each user time setting, the only solution is to use javascript time system because it work on user client time. For example i have an application that each time user try it, the app will create some example data with each data have a initial date value "today". At first time, it confuse me a lot because my host server is in australia and lot of user is on western part, so sometime the initial date value is not "today", it said "yesterday" because of different time region.

After a couple day of headache i finally take decision to JUST use javascript time system and include it in the link, so when user click the "try now" link it will also include today date value.

<% javascript_tag do -%>
  var today = new Date();
  $("trynow").href = "<%= new_invitation_path %>?today=" + today.toLocaleString();
<% end -%>
gkrdvl
Thanks for that. However, my concern isn't with keeping time synced with people. Mine is an issue of the Time objects returning similarly formatted values for display/print. I don't care what time zone people are in. I'm dealing with data that is being logged on the same server as the application so I only need the time to be consistent across the server -- in harmony with its own local data. That said, my issue was with ActiveSupport returning UTC formatted time, and Time returning locally formatted time. I need all server-local all the time. Thanks again.
humble_coder
A: 

Add the following to config/environment.rb to handle time correctly and consistently all the time within the context of Rails. It's important to know that it will store your times to the database in UTC -- but this is what you want -- all the conversion is done automatically.

config.time_zone = 'Pacific Time (US & Canada)'

You can run rake time:zones:local from your Rails root directory to get a list of valid time zone strings in your area.

bensie