Ruby on Rails and Active Record standalone scripts disagree on database values for :timestamps

I posted a question earlier today when I'd not zeroed in quite so far on the problem. I'll be able to be more concise here.

I'm running RoR 2.1.2, on Windows, with MySQL. The SQL server's native time zone is UTC. My local timezone is Pacific (-0800)

I have a model with a :timestamp type column which I can do things like this with:

record = Record.find(:first)
record.the_time =

When I do a "select * from records" in the database, the time shown is eight hours in advance of my local time, which is correct, given that the DB is on UTC. (I have verified that it is 'thinking in utc' with a simple 'select now()' and 'select utc_timestamp()')

This is where the trouble begins. If I display the time in a view:

<%= h record.the_time %>

...then I get back the correct time, displayed in UTC format. If I wrote to the database at 16:40:00 local time, the database showed 00:40:00.

HOWEVER, if I am running a standalone script:

record = Record.find(:first)
puts record.the_time

...then I get back the UTC time that I stored in the database (00:40:00,) but with the local timezone:

Wed Nov 26 00:40:00 (-0800) 2008 eight-hour time warp. Why is it that storing the time translates it correctly, but retrieving it does not? If I compare a stored time from the recent past in the DB and compare it to the current time, the current time is less - telling me this isn't just a string conversion issue.

Any ideas?


There is a setting in config/environment.rb that sets a time_zone. Possibly this is not set the same in your script:

# Make default to the specified zone, and make Active Record store time values
# in the database in UTC, and return them converted to the specified local zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Uncomment to use default local time.
config.time_zone = 'UTC'

You can also try explicitly specifying the TZ and format:

require 'active_support/core_ext/date/conversions'

(or cheat and grab the code fragment from there if you're not using active_support in your standalone script)

Try requiring active_support

Need Your Help

Assign string to zmq::message_t without copying

string move zeromq zero-copy

I need to do some high performance c++ stuff and that is why I need to avoid copying data whenever possible.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.