Getting local time

I remembered this time as a string:

2010-07-25 04:16:25

This is GMT time for some of the actions I took.

Since I live in the time zone in Jerusalem, I would like to show it during Jerusalem, that is, in the morning at 07:16, and not at GMT 04:16:25, which is 3 hours before.

How to properly convert it using Ruby on Rails? I seem to be lost with a lot of time zone features and considerations that I need to take when serving users from different places.

I tried: Time.parse ("2010-07-25 04:16:25") and it gave me: "Sun Jul 25 04:16:25 +0300 2010".

I believe that “+0300” is the difference with where I am?

Some light on this, or even a link to a good article that does not suggest that you know a lot, will help.

+3
source share
2 answers

You can define your time zone in the environment.rb file (if you use Rails 2.3. *) Or application.rb (if you use Rails 3).

Just find the time zone section and everything is explained in the comment. He will say something like this (this is from Rails 3):

# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
# config.time_zone = 'Central Time (US & Canada)'

Just uncomment this last line and you will be fine.

+4
source

To easily configure it on the local zone of your system, you can add this to your application.rb

config.time_zone = Time.now.zone

and you can use something like this to get local time

Post.created_at.localtime
0

Source: https://habr.com/ru/post/1756899/


All Articles