I'm aware there are similar questions like this (1), (2).
But I thing my approach is somewhat different.
I want to get the average times a user logs in the platform with it's timezone. This would be provided back to the user as some kind of stats, like:
You logged in an average of X times each day this week
As the timezone could mean the difference between the login being inside the week or not I've iterated through the problem with the following solutions with it's pros and cons.
My natural approach would be to use epoch time (ignoring timezones).
+-----------+--------------------+------------+
| log_in_id | user_id | epoch_time |
+-----------+--------------------+------------+
| 1 | 1 | 1513242884 |
| 2 | 1 | 1513243489 |
+-----------+--------------------+------------+
But this wouldn't give accurate results as someone with a timezone not near UTC would appear to have logged on different days. And store somewhere the default user timezone.
So the next iteration of the problem would be to store the 1st login timezone of the user on to a table.
+--------------------+---------------+
| user_id | timezone_id |
+--------------------+---------------+
| 1 | Europe/Madrid |
| 2 | America/Bahia |
+----------- --------+---------------+
This solves one problem, but makes another one appear. Users with changing timezones (let's say they travel a lot or live in a zone with daylight saving time) would still not give accurate results.
The only solution I could thing of is to store the difference of timezone with each login.
+-----------+--------------------+------------+-----------------+
| log_in_id | user_id | epoch_time | timezone_offset |
+-----------+--------------------+------------+-----------------+
| 1 | 1 | 1513242884 | -60 |
| 2 | 1 | 1513243489 | -120 |
+-----------+--------------------+------------+-----------------+
Now with epoch_time - time*60
would give the local time of each signin.
But this surfaces another problem. A user could change to another time zone in less time than the timezone difference. Ej. User foo logs in at 14:00 local time then travels to B (that has -1h in the timezone) (takes 30 minutes) and logs in again at 13:30 (B local time), but it's really 30 mins later. This would look like an user traveled time.
At this point I don't know how to proceed. I suppose the last solution is enough and the "best" way to approach it. But I suppose there has to be a better way.