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.

4
  • What kind of environment are you talking about? What if users ssh from all over the world? Aug 27, 2012 at 16:37
  • 1
    would this be better accomplished with js since that runs client side? Aug 27, 2012 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, 2012 at 18:11
  • unrelated: to find out the server timezone, you could use tzlocal.get_localzone().
    – jfs
    May 5, 2014 at 7:48

2 Answers 2

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

1
  • not it has the opposite sign to what is normally called utc offset (local time = utc + offset).
    – jfs
    May 5, 2014 at 7: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.

2
  • Yes, indeed. I'm not that familiar with js. One learns something new here everyday. :-) Aug 27, 2012 at 16:59
  • if you want to get timezone from a location; you can.
    – jfs
    May 5, 2014 at 7:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.