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.
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.
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.
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.
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With