Setting time zones for Django users using geography

I am working on a Django application that needs to report the local time relative to the user. I would rather not ask the user to enter the timezone directly because I have their address stored in the database. I am only considering American users. Since most of the states in the United States are in the same time zone, in most cases, you can calculate the time zone from the status information. I want to give the function a name of state / geographic information and return it the time offset from the UTC of that state, taking into account the daytime savings.

Is there a library that can do this for python?

+2


source to share


3 answers


I'm not sure if such a library exists. Every state in the United States does not have only one time zone.

Look here: List of US States by Time Zone



Many states have more than one.

I think you can still use this list and select the time zone that is used by most of the state and then allow users to customize them if different.

+3


source


I'm not sure how reliable it is, but the HTTP request encapsulated in Django in an object request

has a header TZ

that shows the client's timezone.



>>> request.META['TZ']
'Europe/London'

      

0


source


As Andre Miller noted, time zones are not tied to a state. You are probably better off using zipcode because multiple cities cross timelines.

There are several databases that you can purchase to have these zip codes displayed in time zones. Here I found that it also includes UTC offset and daylight saving time:

USA with 5-digit Zipcode with time zone information

I haven't done timezone before, but I used this database for lat / log information. Data does change, so plan to update your database several times a year.

By the way, if anyone knows about a free database like this, please write: I think it would be very convenient for the community.

0


source







All Articles