0

Possible Duplicate:
UTC time resets to 2000-01-01 (ruby). How do I prevent the time from resetting?

In my application.rb, I have:

config.time_zone = "Eastern Time (US & Canada)"

If, in my application, I display:

<%= Time.now %>

everything looks as I'd expect ("2012-10-09 09:20:39 -0400").

I also have a Session model with an attributes 'start' and 'end' of type Time.

If, in my application code, I create a Session and set s.start=Time.now, the time shows up in the database as a UTC time, which is ok if translated for the application, but with a date of 2000-01-01, and, displays the same way in the application if I have something like:

<%= s.start %>

Also, if I then populate my 'end' attribute some time later with s.end = Time.now, I get a similar 2000-01-01 and UTC time value. At the same time, I set an elapsed time attribute as:

s.elapsed = s.end - s.start

In the database, elapsed shows up as an extremely large number (403056003) even if the difference between start and end is only a few seconds.

If I display s.elapsed and (s.end - s.start) in my application later, s.elapsed shows the big number in the database (obviously), but the calculation of s.end - s.start displays correctly!

Sorry if this is rambling. I'm clearly missing something fundamental, but can't get there.

Community
  • 1
  • 1
Greg Rogers
  • 1,001
  • 2
  • 8
  • 15
  • What is the data type of the time fields in the database? Looks like they could be "TIME" instead of "DATETIME" ? Check your migration and also the table using `desc tablename;` in the mysql console. When you say "of type Time", then maybe you did create a TIME field. The TIME data type stores only the time I believe, not the date. – Zabba Oct 09 '12 at 14:10
  • Also, http://stackoverflow.com/questions/5073917/why-doesnt-config-time-zone-seem-to-do-anything might help. – the Tin Man Oct 09 '12 at 14:12
  • Thanks all. I think what was confusing me was ruby/rails representation of the field before committing to the database. And then what I saw after. If I try a migration 'change_column :sessions, :start, :datetime', the migration complains that the column cannot be cast to type timestamp without time zone. Is there a way to make the TIME->DATETIME change (without dropping and readding the column)? Thanks!! – Greg Rogers Oct 09 '12 at 20:16

0 Answers0