Why does Rails store time in UTC?

Normally, Rails stores all the time in a database in UTC. If you set your time zone as something else, it is automatically converted between this zone and UTC when you save it in the database or retrieve it from it.

What are some of the benefits of this approach? Are there any disadvantages? Is there a way to use Rails in a different time zone?

I think some of the benefits may be:

  • UTC eliminates the ambiguity of seasonal time changes.
  • You can present different time zones for different users by storing compatible things in the database.

The only drawback I can think of is that for an internal application where all users are in the same time zone, this difference complicates the execution of raw SQL queries based on local time.

+4
source share
1 answer

This question has a little religious feeling for him, but I'm going to answer it based on my personal experience.

Always store dates in unambiguous form. Storing dates in UTC is pretty much the standard in this regard.

Benefits:

  • You can do simple math by date-time in the database without the need to convert them.
  • You can customize the presentation of dates at the presentation level.
  • Web applications can use a bit of javascript to display local time

Disadvantages:

  • You need to convert all the time to some kind of "local" time on the display
  • Local time ↔ UTC conversions incur a small processing fee.

Can you get the rails to do something else? Perhaps, but I never tried, since it was too much work to fight with the fact that IMHO was a reasonable design.

Does it make sense to use UTC in terms of "just use my time zone"? Yes. Your server may be located in California, your users in New York, and who decides what the local time is in this case. Server? Users? Mike, who just ended up on a business trip in London for a week? In this case, what time zone are you using?

+7
source

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


All Articles