24👍
Neither django nor python provide a set of timezones for you to use.
For that, you will need an additional module like pytz
. You can get a list of all timezones like this:
>>> import pytz
>>> pytz.all_timezones ['Africa/Abidjan', 'Africa/Accra', 'Africa/Addis_Ababa', 'Africa/Algiers', 'Africa/Asmara',
'Africa/Asmera'....
You can their store the timezone name in a CharField
.
By the way, choosing a timezone by “GMT +6:00” is not a good idea. For example, EST is usually 5 hours behind GMT, but for 2 weeks around daylight savings time changes, the offset is different. Also, at some times of year someone in Queensland and someone in New South Wales both have the same GMT offset, but because NSW has DST and Queensland doesn’t, for half the year their GMT offsets are different. The only safe way to list timezones is to list the actual geographic timezones.
39👍
I think the above answers are all correct, but I leave mine here as an simple example…
class UserProfile(models.Model):
import pytz
TIMEZONES = tuple(zip(pytz.all_timezones, pytz.all_timezones))
# ...
timezone = models.CharField(max_length=32, choices=TIMEZONES,
default='UTC')
# ...
- [Django]-More than 1 foreign key
- [Django]-How do I start up remote debugging with PyCharm?
- [Django]-Django – present current date and time in template
- [Django]-Django dynamic forms – on-the-fly field population?
- [Django]-How to submit form without refreshing page using Django, Ajax, jQuery?
- [Django]-Django: guidelines for speeding up template rendering performance
7👍
The way I do this is using pytz valid timezone names. I adjusted my list to reflect only the ones I need, i.e.
TIMEZONES = (
'Canada/Atlantic',
'Canada/Central',
'Canada/Eastern',
'Canada/Mountain',
'Canada/Pacific',
)
I then have a location class which sets the timezone as a Char Field as such:
class Location(models.Model):
....
time_zone = models.CharField(max_length=100, blank=True, null=True, choices=TIMEZONES) # 64 min
....
Notice I set blank & null to True to make the field optional. Take a look at django-timezone-field fields.py for further ideas.
To use this in my code with pytz, I import timezone:
from pytz import timezone
import datetime
from locations.models import Location # my object that has the time_zone field
loc = Location.objects.get(pk=1) #get existing location or your object that has time_zone field
utc = pytz.utc
some_utc_date = datetime.datetime(2002, 10, 27, 6, 0, 0).replace(tzinfo=utc) #tz aware
some_date.astimezone(timezone(loc.time_zone))
Replace datetime.datetime(2002, 10, 27, 6, 0, 0) with the datetime field that corresponds to your location or specific object that has the time_zone field. In my case I store all my date fields in UTC format in a MongoDB collection. When I retrieve the data and want to create human readable output, I use the above method to show the date in the output. You can also create a custom tag to handle this in templates. See pytz doc for more details.
- [Django]-What the difference between using Django redirect and HttpResponseRedirect?
- [Django]-Django Cannot set values on a ManyToManyField which specifies an intermediary model. Use Manager instead
- [Django]-Filter on prefetch_related in Django
2👍
If you are migrating from pytz
to zoneinfo
:
try:
import zoneinfo
except ImportError:
from backports import zoneinfo
class UserProfile(models.Model):
TIMEZONE_CHOICES = ((x, x) for x in sorted(zoneinfo.available_timezones(), key=str.lower))
timezone = models.CharField("Timezone", choices=TIMEZONE_CHOICES, max_length=250, default='Etc/GMT+2')
- [Django]-Django Rest Framework and JSONField
- [Django]-Django: How can I call a view function from template?
- [Django]-Changing a project name in django