E
E
eldar_web2015-09-29 13:51:56
Ruby on Rails
eldar_web, 2015-09-29 13:51:56

How to change the time zone in Ruby on Rails?

I wrote in locales/aplication.rb: config.time_zone = "Moscow" .
But in models, when I write Time.now, it does not save the current date (hours), but the early one by 3 hours.
How to solve the problem?
And yet, instead of writing Moscow, can't you do something else so that the system determines the current time zone?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
malroc, 2015-09-29
@eldar_web

But in models, when I write Time.now, it does not save the current date (hours), but the early one by 3 hours.

Instead of Time.now, it is more correct to use Time.zone.now (or Time.current, which is about the same). The database will still store everything in UTC, but this affects the interaction with the client. In your case, there will be no difference, but this is because you have the same server and client times.
config.time_zone is not the server time (it's just read from the system), it's the client's assumed timezone. That is, when the time comes from the client / is displayed to the client, it is converted from / to this zone.
Generally:
  • Time.now - system time, in the time zone that is registered in the system on the server
  • Time.zone.now - client time, defined via config.time_zone, or on the go via Time.zone =
  • Database - everything is stored in UTC + 0

J
Jeiwan, 2015-09-29
@Jeiwan

Rails save the date in UTC, and when data is loaded from the database, it is converted to the zone specified in config.time_zone.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question