Handling time for projects used in many countries

Tram Ho

Hi guys, most project developers will probably work with respect to processing time (time), if your project is only used in only one country then it is less likely will be wrong about some query or function related to time, if your code is not wrong. But think about it, if we do a project where a function related to multinationals and our server can only configure 1 time zone for 1 single country, what would this be like ??? If you have not covered the code, you will definitely generate a bug in this function. And because of that, today I will show you how you can code and use it in many countries I know, let’s go

First we should learn:

Time zone

Explain simply: The world is divided into 24 time zones based on longitude, each time zone will correspond to a time zone. That means every 1 timezone will stretch with 15 degrees longitude. The zero mark is taken at the zero-degree longitude traverse through London, England.

In Ruby, there will be classes related to Time, such as:

  • Time
  • Date
  • DateTime And in Rails it will provide ActiveSupport :: TimeWithZone class to save time with time zone. When creating a new row with columns of type datetime, the DB will default to save under the time zone as UTC + 0. However, when taking that value out, the datetime value will automatically be calculated converted to the time zone corresponding to the setting in application.rb.

above because I am setting time_zone in Japan so when I Time.zone it will +9 hours at the time of data received when I query from DB

The problem will occur when I use it in many countries

I take for example: in the same time 2 guys want to see the admin data of the user table but because here I only config server is Japanese time, the data returned it is currently only right compared to the admin in Japan and the admin guy in Vietnam the data will not be right because it is now paying data of Japanese time

How to solve the problem

In the case set out above, in order to help the Admin in each of the different locations to get accurate statistics according to the corresponding time_zone, it is necessary to determine the time_zone for each user rather than using the default time_zone setting in the application. rb There are 2 ways to determine this:

  • Save time_zone by admin_user.
  • When admin_user access, use javascript on the client to get timezone information and send as a param to the server. The server will use the posted time_zone params and process the data based on this param. You can refer to how to get time_zone via javascript.
  • Bringing all queries to the same time zone (this is quite hard because I have to convert every query related to time zone)


For example, I add 1 case that even if you save the time zone in the user, it still cannot solve. ex: There is a function, people want to 00h every day or an hour every day, will automatically run all data and notify if that data is valid, then obviously because my server only config 1 time zone then auto at 00h in Japan, the auto in Japan is correct, but in VN it will run at 22h the previous day => wrong compared to other time zones config. My solution is that when I save the data, I will have to save the attribute I want that query in 00h and transfer all the UTC time to query, run the job 24 times a day.  This is my own way of handling, look at a banana but it is guaranteed to be true spec =))

If you have any good solutions, you can comment below for me to learn, thanks everyone.

Share the news now

Source : Viblo