0

Using Rails 3 and PostgreSQL 8, I have a fuel_date column with data type "timestamp without time zone". A user selects a date and time from dropdown menu in their local time and when it's submitted to server, it should be saved as UTC time:

  def create_fuel_service
    attributes = {}
    fuel_service_hash = params[:fuel_service]
    datetime = DateTime.new(fuel_service_hash["fuel_date(1i)"].to_i, fuel_service_hash["fuel_date(2i)"].to_i, 
                            fuel_service_hash["fuel_date(3i)"].to_i, fuel_service_hash["fuel_date(4i)"].to_i,
                            fuel_service_hash["fuel_date(5i)"].to_i).in_time_zone(current_user.time_zone).utc
    attributes[:fuel_date] = datetime
    fuel_service = FuelService.new(attributes)
    if fuel_service.save
       ...

However, the record is saved as localtime, not UTC time, even though the created_at and updated_at fields are saved as UTC time:

http://s22.postimg.org/caho7v6c1/utctime_issue.png

How can I convert the fuel_date field to UTC time before saving? Using the UTC time method of DateTime does nothing.

frlan
  • 6,950
  • 3
  • 31
  • 72
JohnMerlino
  • 3,900
  • 4
  • 57
  • 89
  • In Postgres you would use the `AT TIME ZONE` construct, but you probably want to adjust the value in the client? Either way, [this related answer should be of help.](http://stackoverflow.com/questions/9571392/ignoring-timezones-altogether-in-rails-and-postgresql/9576170#9576170) – Erwin Brandstetter Apr 25 '14 at 18:49

1 Answers1

0

What ended up working was using Time.new rather than DateTime.new:

datetime = Time.new(fuel_service_hash["fuel_date(1i)"].to_i, fuel_service_hash["fuel_date(2i)"].to_i, 
                        fuel_service_hash["fuel_date(3i)"].to_i, fuel_service_hash["fuel_date(4i)"].to_i,
                        fuel_service_hash["fuel_date(5i)"].to_i)
JohnMerlino
  • 3,900
  • 4
  • 57
  • 89