3

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

Community
  • 1
  • 1
MDekker
  • 443
  • 1
  • 4
  • 19
  • `date.to_datetime` directly returns the date and `date.to_time.to_datetime` method internally convert date to `UTC` format `YYYY-MM-DDThh:mm:ssTZD` – Sonalkumar sute Mar 13 '15 at 09:07
  • And when you save the date in Postgres it save it as `2015-03-13 09:29:00 UTC+5:30` and when you retrieve it it gives you GMT time `2015-03-13 14:59:00`, both the dates are same `2015-03-13 09:29:00 UTC+5:30` = `2015-03-13 14:59:00` – Sonalkumar sute Mar 13 '15 at 09:13

0 Answers0