Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using python, what is the most accurate way to auto determine a users current timezone

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.

like image 388
chrickso Avatar asked Aug 27 '12 16:08

chrickso


People also ask

How do I get the current timezone in Python?

You can get the current time in a particular timezone by using the datetime module with another module called pytz . You can then check for all available timezones with the snippet below: from datetime import datetime import pytz zones = pytz. all_timezones print(zones) # Output: all timezones of the world.

How do I fetch the timezone of a user?

The client's timezone offset could be detected by using the Date object's getTimezoneOffset() method. The getTimezoneOffset() method returns the time difference between UTC time and local time, that is the time offset, in minutes.

How does Python handle time zones?

So in order to work with the timezone smoothly, it is recommended to use the UTC as your base timezone. To get the Universal Time Coordinated i.e. UTC time we just pass in the parameter to now() function. To get the UTC time we can directly use the 'pytz. utc' as a parameter to now() function as 'now(pytz.

What is timezone in Python?

TimeZones. Python provides the datetime. tzinfo abstract base class which provides methods to handle timezone. But this class is an abstract base class and should not be instantiated directly. We need to define a subclass of tzinfo to capture information about a particular time zone.


2 Answers

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

like image 59
brian buck Avatar answered Oct 26 '22 20:10

brian buck


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.

like image 26
Roland Smith Avatar answered Oct 26 '22 18:10

Roland Smith