1

I have a query that hits my database and it returns a column of dates. the output of this query looks something like this in psql:

Query:

SELECT (by_event.event_date AT TIME ZONE 'UTC')::DATE as event_date from events as by_event;

Output:

event_date   
------------
 2012-11-15 
 2012-11-16
 2012-11-17

I run my SQL query in Rails with this:

results = ActiveRecord::Base.connection.execute(sql) 

results.each do |result|
  puts result.inspect
end

This unfortunately prints out:

[{\"event_date\"=>\"2012-11-16\"}, {...}]

All of the dates are one day ahead of what they should be!

Obviously this is timezone related, but I am not sure how to fix it. How do I get Rails to stop bumping up the date?

2
  • config/application.rb check what config.time_zone is set to. If its not set, set it to your desired time zone and things should be fine.
    – Anand Shah
    Oct 29, 2013 at 17:21
  • I have tried calling Time.zone = "EST" and Time.zone = "UTC" with no change in results. just before running the query.
    – iTake
    Oct 29, 2013 at 17:40

1 Answer 1

1

For future people, here is what I did to fix this:

SELECT  ((by_event.event_date) AT TIME ZONE 'America/New_York')::DATE as event_date,

Ugly, but it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.