adamcharnock/django-tz-detect

getTimezoneOffset() produces incorrect value if TZ is wrong

Opened this issue · 1 comments

Hi, thank you for a great module!

Right now django-tz-detect uses (new Date()).getTimezoneOffset()) to get current user offset and save to session. While in most cases it't quite accurate, there is a weird case, when user doesn't change timezone itself, but just sets up it's local time.

getTimezoneOffset() can't detect such cases and returns offset, based on actual browser timezone, which leads to incorrect user time offset. It's seems this behaviour is quite common especially for Windows users.

I guess, the following approach may give more accurate results:

  1. When user logs in, send it's local time to server instead of offset.
  2. In middleware, compare it with UTC (as Django stores datetime in UTC) and calculate actual offset, not based on user offset and store into session.
  3. Use this value to shift user time according to it's real offset.

Any ideas? Thank you.

Using this instead:

console.log(Intl.DateTimeFormat().resolvedOptions().timeZone)

https://stackoverflow.com/a/34602679/6396981