6

I have verified that dateutils.tz.tzlocal() does not work on heroku and even if it did, wouldn't it just get the tz from the OS of the computer its on, not necessarly the users?

Short of storing a users timezone, is there any way to determine where a request is coming from? (I'm using flask)

Twitter does have a setting to adjust your timezone but I'm wondering how they determine what the default should be and how that would work when a user is not logged in.

chrickso
  • 2,994
  • 5
  • 30
  • 53
  • What kind of environment are you talking about? What if users ssh from all over the world? – Eero Aaltonen Aug 27 '12 at 16:37
  • 1
    would this be better accomplished with js since that runs client side? – Joran Beasley Aug 27 '12 at 16:42
  • All of the web-apps I've worked with store the user timezone preference. Its a small sample, but a sample nonetheless. Its relatively inelegant, but its simple and it works well with minimal failures due to strange clients/OSs :) – Oren Mazor Aug 27 '12 at 18:11
  • unrelated: to find out the *server* timezone, you could use `tzlocal.get_localzone()`. – jfs May 05 '14 at 07:48

2 Answers2

9

You could use Javascript and set the client's time zone in a cookie. You could even use an AJAX request and then send the offset to the server and save in the client's session.

var offset = new Date().getTimezoneOffset();

Description

The time-zone offset is the difference, in minutes, between UTC and local time. Note that>this means that the offset is positive if the local timezone is behind UTC and negative if it is ahead. For example, if your time zone is UTC+10 (Australian Eastern Standard Time), -600 will be returned. Daylight savings time prevents this value from being a constant even for a given locale

Mozilla Javascript Reference: getTimezoneOffset

brian buck
  • 3,284
  • 3
  • 23
  • 24
  • not it has the opposite sign to what is normally called utc offset (local time = utc + offset). – jfs May 05 '14 at 07:50
1

If you can easily get the users' IP address from within flask (maybe from the request object?), you could use geolocation. E.g. via http://www.hostip.info/.

It is probably not 100% foolproof, though. For instance if someone uses a VPN or another kind of proxy you wouldn't see his "real" IP address.

Roland Smith
  • 42,427
  • 3
  • 64
  • 94