Why Rails always displays date and time as 1/1/2000 - with the correct time

This one turns me on! Data is saved in db (sqlite3) correctly. However, when I show the date from the record, Rails seems to force it to 1/1/2000 - with the correct time. In other words, if I specify this as the time of the date: December 31, 2009, 18:00, sqllite3 will actually show 2009-12-31 18:00:00. But .... Rails will display the value from January 1, 2000, 18:00 (observing the correct time).

I created virtual attributes to handle date formatting (which work correctly). And I set my time zone:

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

I have to believe that this is something simple ... It completely turns me on nuts!

Thanks!

John

+4
source share
3 answers

I had the same problem using postgres and thanks to user145110 for suggesting using the DateTime class instead of Time. It worked for me. I would add that although I had to change the porting to use DateTime, I could still use Time and its methods in tests that were compared with DateTime values. In other words, with this initial migration:

 t.time :start_time 

... and fetching using Time:

 start_time = Time.now.beginning_of_day 

... it was preserved normally, but, having read it later, the year was changed to 2000. Perhaps this is an old Y2K bug. Lol Instead, I just changed the migration:

 t.datetime :start_time 

.. and all my code worked. Even tasks like this job:

 start_time = Time.zone.now.beginning_of_year.beginning_of_day 

I cannot help but feel that I missed something. Others use Time to manage dates?

+5
source

Well ... I found a problem. As it turned out, when the rails created the table, she did it using the Time data type. Although the part of the date of the date and time value will be stored in the time field, it would seem that when reading the time field, the rails take into account only the time part.

Correction .. In desperation, I changed the datetime column. This fixed it.

John

+3
source

I also had this problem. To add some information about the problem: - In the debug tag (<% = debug object%>), the time and date were correct, but when used without debugging (<% = object.year%>), the year was 2000.

You can expect one of two things:

  • : time has only time (hours, minutes, seconds, etc.) and expects to see this information in the database (without date (year, month and day)).

Xor

  • : time has time and date, and this information goes to the database (including casting on the object).

In fact, rails take two options. IMHO, this is a mistake. I'm right? or i dont see any point of rail design?

0
source

All Articles