After deploying my rails app to a VPS I had some problems with 'time'. After some puzzling I found out that Rails is acting in a strange way when using the date/time conversion.

My server is set to timezone "Amsterdam" and in the Rails config I also have set timezome to "Amsterdam"

example:

I receive this string via my body content in a json/api

date = "13-03-2015 09:29"

when I convert this string like this:
date.to_datetime, the result is "Fri, 13 Mar 2015 09:29:00 +0000"

when I convert the string like this:
date.to_time, the result is "2015-03-13 09:29:00 +0100"

As you can see there is a difference in the way it is adding the time correction "+0000" vs "+0100"

So when I convert like this:
date.to_time.to_datetime, the result is "Fri, 13 Mar 2015 09:29:00 +0100"

And this is the result I needed, because my postgres database otherwise gave me back a wrong date (+1 hour)

My questions are:

  1. What is the explanation for the difference in behaviour of .to_date and and .to_datetime

  2. How does Postgres handle dates. When saving a datetime parameter like this "Fri, 13 Mar 2015 09:29:00 +0000" and I render this date in a json response it is 1 hour later.

Hope someone can explain this to me.
Martijn

2

Related questions 0 Strange output after date conversion in Ruby on Rails 1 Ruby on Rails 3: Why is the conversion from Date to seconds different from Time to seconds 5 Weird Ruby Behavior in DateTime to Time conversion Related questions 0 Strange output after date conversion in Ruby on Rails 1 Ruby on Rails 3: Why is the conversion from Date to seconds different from Time to seconds 5 Weird Ruby Behavior in DateTime to Time conversion 0 Why does to_date in my view output date in a different format than to_date in Rails console for the same object? 4 Time.parse and DateTime.parse returns different results 9 Ruby Time#to_date() method returns wrong date 14 Why are those two datetimes different? 0 Date conversion is not working as expected, time always remains after conversion 2 DateTime turned to string by two methods 0 Ruby inconsistency in time calculations between Date and Date.to_time operations Load 7 more related questions Show fewer related questions

Reset to default

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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.