Time. Night time sensitivity

I have daily analytics-style entries that track usage on my site, and they work as follows:

  • When an event occurs, the site searches for the entry created in Time.now.midnight .
  • If such an entry is not found, a new one is created, and created_at is Time.now.midnight .

Here's the question - is Time.now.midnight recorded differently depending on the client’s time zone? If so, will I correct it, believing that the above very simple system will be destroyed?

How can I fix this so that all analytics records, regardless of the time zone of the user who caused their creation, are bound to the same time?

Note. I believe setting this created_at field to Date instead of Datetime might be better in this scenario. Suppose we are stuck with a given time for this issue.

+4
source share
1 answer

Time.now does not register differently depending on the time zone of clients.

Time.now returns a new time using the system time zone (aka server)

To use a specific time zone for a client, you must select a user for your zone and use Time.current or Time.zone.now (which does the same)

created_at is usually attached to UTC, so you shouldn't have a problem either. (to change it you need to change Rails.root / config / application.rb)

 config.time_zone = "whatever you want the ActiveRecord default to be" 
+2
source

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


All Articles