Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get local timezone in django

Tags:

python

django

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" ?

like image 237
David542 Avatar asked Feb 02 '13 00:02

David542


People also ask

How do I get end user timezone in Django?

Django doesn't detect end user's timezone. It saves the objects in the database in UTC time. Then you can convert the UTC time to the client's time on the browser using JavaScript for displaying.

What timezone does Django use?

Python Django set timezone settings For saving information in the database, Django recommends using UTC. Even if your website is only accessible in a one-time zone, storing data in UTC in your database is still a good strategy. Daylight Saving Time is the primary reason for this.

What is Use_tz in Django?

When USE_TZ is False, this is the time zone in which Django will store all datetimes. When USE_TZ is True, this is the default time zone that Django will use to display datetimes in templates and to interpret datetimes entered in forms.

How does Django store current date and time?

First, open the views.py file of your Django application and import the datetime module. Next, use the datetime. now() method to get the current date and time value.


1 Answers

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) 
like image 153
Austin Phillips Avatar answered Sep 23 '22 07:09

Austin Phillips