Timezone Rails Postgres

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Timezone Rails Postgres



How to select datetime column with timezone using ActiveRecord::Base.connection.execute()?



e.g



User.first.joined_at => Tue, 31 Jul 2018 05:00:34 MSK +03:00


User.first.joined_at => Tue, 31 Jul 2018 05:00:34 MSK +03:00



but


ActiveRecord::Base.connection.execute("SELECT joined_at FROM users") => "joined_at"=>"2018-07-31 02:00:34.684659"




1 Answer
1



ActiveRecord::Base.connection.execute doesn't understand the database's types that well so you have to parse the strings yourself.


ActiveRecord::Base.connection.execute



That timestamp should be in UTC as that's the Rails standard inside the database. You could go through Time.zone:


Time.zone


Time.zone = 'UTC' # Unless your application is already in UTC
t = Time.zone.parse('2018-07-31 02:00:34.684659').in_time_zone('Moscow')
# Tue, 31 Jul 2018 05:00:34 MSK +03:00



Or you could go through DateTime (which assumes UTC if the string doesn't have a timezone embedded in it):


DateTime


DateTime.parse('2018-07-31 02:00:34.684659').in_time_zone('Moscow')
# Tue, 31 Jul 2018 05:00:34 MSK +03:00






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

make 2 or more post in bootsrap

Store custom data using WC_Cart add_to_cart() method in Woocommerce 3

Firebase Auth - with Email and Password - Check user already registered