dimanche 17 avril 2016

Ruby on Rails 3 convert time to DateTime in specific timezone

I have column which stores only time. I want to convert to it into datetime and then to s specific timezone. When I do it on console using only the attribute, it shows correct conversion value. But when I use it in query the query shows the current time in UTC. Suppose I have time "05:15" stored in Central time. Now when I want to fetch records for a interval of 30 minutes plus and minus of this time, I do following,

day  = Time.now
# departure_time_from _db below is the name of column in table which store time
departure = departure_time_from _db.change(:day => date.day, :month => date.month, :year => date.year)
departure = departure.in_time_zone("Central Time (US & Canada)")

When I execute above code on console and see the value, it shows correct converted value. But when I use this variable in below query, current time gets used instead of the departure time.

Model.where("column_name > ? and "column_name < ?, departure - 30.minutes, departure + 30.minutes)

The time used in above query should be the time I want i.e Central time zone, but in query output, it shows UTC being used.

Please let me know what i am missing.

Aucun commentaire:

Enregistrer un commentaire