Topic: Rails creating wrong dates

When I have an object with datetime fields and try to save it to the database the time is 5 hours ahead of my computer's time if I leave the datetime fields blank.  I have no idea why.

thanks,
mike

Re: Rails creating wrong dates

Is your application running on your localhost? Or on a server?

Remember to edit your topic title and add "[SOLVED]" if your question has been answered!

Follow me!

Re: Rails creating wrong dates

The default time zone for Rails is UTC. You can change this in your app/config/application.rb file. If you look in that file, you'll see this in there:

    # 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)'

You can uncomment the last line to change it.

Re: Rails creating wrong dates

Hi,

if your application runs on a server, the problem could be because of server time.

check it out.

life is not short however you think to learn Rails

Re: Rails creating wrong dates

I'm using a localhost.  So Brian71's answer worked for me.  I had to specify the timezone in app/config/application.rb file. 

Thanks to everyone.

mike

Re: Rails creating wrong dates

"Solved" in post title sure helps a lot smile Glad you got everything working!

Remember to edit your topic title and add "[SOLVED]" if your question has been answered!

Follow me!

Re: Rails creating wrong dates

Ok, I thought my problem was solved.  But now I'm using a tutorial from a 4 year old book and it's telling me to enter data into the DB using the rails console.  I changed my app/config/application.rb file to use the config.time_zone = 'Central Time (US & Canada)' setting and then tried reload! in the console and also tried exiting and restarting the console but still my default dates that are placed into the database are again 5 hours ahead of my local time.  Any ideas my fellow Railists?

mike

Re: Rails creating wrong dates

oh, one more thing.  When I type Time.now in the rails console it returns the correct, current time.

Re: Rails creating wrong dates

What database are you using? You may want to find out if the database has a default timezone setting that it's using.

If you're using postgresql, check out this website:
http://fugoconsulting.wordpress.com/tag … -database/

Re: Rails creating wrong dates

I'm using MySQL.  When I try to insert a datetime stamp into a datetime field using the NOW() function at the mysql> prompt it inserts the correct time.