nonopolarity
nonopolarity

Reputation: 151046

How to use exact time with Time zone with MySQL and Sqlite3?

How can this be done in MySQL and Sqlite3?

select * from foos where created_time > "2011-04-03 18:01:02 UTC"

The key is that the time zone needs to be there.

That's because using with Ruby on Rails (3.0.5), if there is a Time.zone = "..." in the config file, then

foo = Foo.all(:condition => "created_at > '#{bar.created_at.strftime('%Y-%m-%d')}'")

then the strftime will give the date of 2011-04-04 in that special time zone, while MySQL or Sqlite will use a date that is probably the local time zone, so there is discrepancy sometimes.

Upvotes: 0

Views: 281

Answers (1)

MattiasB
MattiasB

Reputation: 135

It is not self explanatory but I found out that timestamps only show correct values if you set these params in your application.rb in the config folder.

config.time_zone = 'Your closest timezone related city'
config.active_record.default_timezone = :local

Upvotes: 2

Related Questions