34
votes

I have a mysql DATETIME value that is stored in system time, UTC. I need to convert that to my local timezone in django. Here is what I currently have:

# value in mysql
`timestamp`
2013-02-01 22:48:45

# settings.py
TIME_ZONE = 'America/Los_Angeles'

# views.py
last_updated = PathLastUpdated.objects.all()[0].timestamp
print last_updated
2013-02-01 22:48:45 <-- same as UTC

How would I get the last_updated value to be in my local timezone = "America/Los Angeles" ?

9
By "my local timezone" do you mean the timezone the server runs in or the timezone of the client which views the page? - Sergey
The timezone that I have set in settings.py = 'America/Los_Angeles' - David542

9 Answers

39
votes

The Django documentation for timezones documents all the necessary details for converting datetime objects to the appropriate time zone for display.

Your data is stored in UTC which is good. When you obtain a DateTime field object from the database it will be a naive datetime.datetime object. ie A date/time without a timezone attached. It's then up to you to do the conversion.

User of your webapp may be in different time zones so the conversion to an appropriate time zone must occur for each request. This is why there is an activate function to set the current time zone.

If you have pytz installed you should be able to do the following:

from django.utils.timezone import activate
activate(settings.TIME_ZONE)

All output of date field in the template engine will then automatically convert you naive date time objects to the correct time zone for display.

If you just have a single naive datetime.datetime instance that you want to set the time zone on, then just use the pytz module directly. It is not normal to do this in your views though, as it's a good idea to only convert the time zone at the point of presentation.

from pytz import timezone

settings_time_zone = timezone(settings.TIME_ZONE)
last_updated = last_updated.astimezone(settings_time_zone)
29
votes

After cry a lot, I could show the correct date for my country doing something like this:

>>> from django.utils.timezone import get_current_timezone
>>> from front.models import Training

>>> tz = get_current_timezone()
>>> stored_date = Training.objects.first().start_date
datetime.datetime(2015, 4, 25, 17, 0, tzinfo=<UTC>)

>>> desired_date = stored_date + tz.utcoffset(stored_date)
datetime.datetime(2015, 4, 25, 14, 0, tzinfo=<UTC>)

The tzinfo attribute is shows utc, but the date and time is correct to show.

UPDATE 30/10/2015 (Django 1.8)

I'm using another approach today, that is more django friendly

>>> from django.utils import timezone
>>> from trainings.models import Training
>>> value = Training.objects.first().date

>>> value
datetime.datetime(2015, 10, 23, 11, 32, 54, 633151, tzinfo=<UTC>)

>>> timezone.localtime(value)
datetime.datetime(2015, 10, 23, 9, 32, 54, 633151, tzinfo=<django.utils.timezone.LocalTimezone object at 0x7fa6129784a8>)
16
votes

localtime is a template filter, this may be helpful.

https://github.com/django/django/blob/1.8.4/django/utils/timezone.py#L298

Code sample:

from django.utils.timezone import localtime 

desired_datetime = localtime(stored_datetime)
7
votes

I've created a simple middleware to handle all of this stuff for you:

https://github.com/Miserlou/django-easy-timezones

Simply install it and follow the instructions and you're done!

  1. Install django-easy-timezones

    pip install django-easy-timezones pytz pygeoip
    
  2. Add "easy-timezones" to your INSTALLED_APPS setting like this:

    INSTALLED_APPS = (
    ...
    'easy-timezones',
    )
    
  3. Add EasyTimezoneMiddleware to your MIDDLEWARE_CLASSES

    MIDDLEWARE_CLASSES = (
    ...
    'easy-timezones.middleware.EasyTimezoneMiddleware',
    )
    
  4. Add a path to the MaxMind GeoIP database in your settings file:

    GEOIP_DATABASE = '/path/to/your/geoip/database/GeoIP.dat'
    
  5. Enable localtime in your templates.

    {% load tz %}
        The UTC time is {{ object.date }}
    {% localtime on %}
        The local time is {{ object.date }}
    {% endlocaltime %}
    
  6. Tada!

6
votes

I personally would advice against using a TIME_ZONE setting other than UTC. I remember having problems with this in the past, be it that the database was operating in a different timezone (saving values in a different timezone) than the Django backend was using. That meant a lot of hassle to compare the times, changing them forth and back depending on what you are doing.

A good practice is usually to use one timezone in the backend (lets say UTC) and convert the time in the frontend to the users timezone you are serving.

5
votes

I have came to the same problem. In my settings, I have set the TIME_ZONE, but the time in MySql still UTC time.

# settings.py
TIME_ZONE = 'Asia/Shanghai'

Then I found,in settings.py, USE_TZ has be set to False

USE_TZ = False

As pointed out by @MagicLAMP, it only works for single time zone site, please check time-zones if your site is international or if daylight savings happen where your site is used.

2
votes

It took me a while to find this, but this is how I solved it. I guess the user's timezone from IP address and then do this in Django 2 and beyond:

{% load tz %}
{% timezone "Europe/Paris" %}
    Paris time: {{ object.date }}
{% endtimezone %}
1
votes

1: Define the timezone in your view

from django.utils import timezone

variable = timezone.now()

2: Then use it in your template

{% load l10n %}

Teresina, {{variable |localize}}.