Search code examples
ruby-on-railspostgresqldatetimeecto

Using datetime from Rails in Phoenix with Ecto


I have a database created in a Rails application and now I want use this same database in a Phoenix application (using Ecto). The major problem at this time is with datetime values.

The Rails application show me things like this:

Tue, 06 Feb 2018 00:13:36 -02 -02:00

And the Ecto query on the same table show:

[{{2018, 2, 6}, {2, 13, 36, 22082}}]

Note the timezone lost.

The DB is schemaless on phoenix application and this is necessary.

So, I want keep the timezone information and convert the data to a more useful format for calculations.

Is there a way to do this?

Ecto ~ 3.0

Phoenix ~ 1.2.1

Rails ~ 5


Solution

  • Rails does not store time zone data in the database. All datetime values are stored in UTC. When you query a datetime field on an ActiveRecord object, it converts the output to the time zone set in your application.rb file using config.time_zone =.