0

I am having alot of trouble getting Django to render a datetime field in my local timzeone.

Settings.py has:

TIME_ZONE = 'UTC'
USE_TZ = True
USE_L10N = True

In my model I have:

class ExportRecord(models.Model):
    [...]
    created = models.DateTimeField(auto_now_add=True)

    def save(self, *args, **kwargs):
        [...]
        self.created = timezone.now()

Created gets stored as a UTC time object in MySQL.

If I have "2016-11-08 01:25:15" in the database after the created field is populated, when I render my template I expect it to be translated into the local time of the client (I am in eastern time, so I expected it to be "2016-11-07 20:25:15".

However, no matter what tags I use (for example {{ date_obg | localtime }}), the date will not be rendered as my local time.

I installed tzlocal and when I run get_localzone() in my view it shows 'UTC' as the output.

Furthermore, if I try this (converting my created field from UTC to my local timezone variable):

lctz = get_localzone()
self.created.replace(tzinfo=pytz.utc).astimezone('lctz')
>>>>2016-11-08 01:25:15

The created date stays the same as it is in the DB (which is in UTC).

Is this because the local timezone of my Google App Engine instance is in UTC? How do I get my applications templates to render in my user/client timezones?

coler-j
  • 1,791
  • 1
  • 27
  • 57
  • I think it may have something to do with mySQL... – coler-j Nov 08 '16 at 03:51
  • You've set `TIME_ZONE = 'UTC'` in the settings. Doesn't this tell Django that your local time is UTC? – Antonis Christofides Nov 08 '16 at 07:35
  • 1
    There's some middlewares used to get client timezone, see this for more help http://stackoverflow.com/questions/10235956/django-1-4-how-to-automatically-get-users-timezone-from-client but my advice is set `TIME_ZONE` to the client timezone or most of clients, this is the best solution if your clients from the same country – Shehab ElDin Nov 08 '16 at 09:04
  • 1
    From the [docs](https://docs.djangoproject.com/en/1.10/topics/i18n/timezones/#selecting-the-current-time-zone): "You should set the current time zone to the end user’s actual time zone with `activate()`. Otherwise, the default time zone is used" – Kevin Christopher Henry Nov 08 '16 at 21:17
  • @ShehabElDin is right. I was able to use middleware to set the local timezone. Then as Kevin said, you active the local timezone within this middleware. I have hard coded the timezone through this method. I will post code once I have also retrieved the users actual timezone automatically as well. – coler-j Nov 11 '16 at 13:13

1 Answers1

0

My middleware to activate timezone looks like this:

import pytz
from django.utils import timezone

class get_user_timezone(object):
    def process_request(self, request):
        if request.user.is_authenticated():
            user_timezone = 
                 pytz.timezone(request.user.userprofile.iana_timezone)

            if user_timezone:
                 timezone.activate(user_timezone)
            else:
                 timezone.deactivate()
coler-j
  • 1,791
  • 1
  • 27
  • 57