Privacy Policy
Snippets index

  Manage timezones in Django

file "settings.py"

import pytz

#TIME_ZONE = 'UTC'
TIME_ZONE = 'Europe/Rome'

CONSTANCE_CONFIG = {
    'LAST_CACHE_UPDATE_DT': (datetime.datetime(2000, 1, 1, 0, 0, 0, 0, pytz.timezone(TIME_ZONE)), u'Keeps track of last cache successfull update'),
}

Assign TZ aware current time:

from django.utils import timezone

#config.LAST_CACHE_UPDATE_DT = timezone.make_aware(datetime.datetime.now(),
#    timezone.get_default_timezone())
config.LAST_CACHE_UPDATE_DT = timezone.now()

Print current TZ (settings.TIME_ZONE):

from django.utils import timezone
print timezone.get_default_timezone_name()

When creating arbitrary dates in your code, you should always work with aware datetimes in UTC:

from django.utils import timezone
dt = datetime(y, m, d, tzinfo=timezone.utc)