Does the django.TIME_ZONE setting affect datetime.datetime.now ()?

The documentation says:

http://docs.djangoproject.com/en/dev/ref/settings/#time-zone

Note that this is the time zone for which Django converts all dates / times - not necessarily the time zone of the server. For example, one server can serve multiple Sites on Django, each with a separate time zone setting. Normally, Django sets the os.environ ['TZ'] variable to the time zone specified in the TIME_ZONE setting. Thus, all your views and models will automatically work in the correct time zone.

I read this several times, and it’s not clear to me what happens with the TIME_ZONE setting.

Do I have to control UTC offsets if I want models with date and time stamps displayed in the user's local zone?

For example, to save, use datetime.datetime.utcnow () instead of datetime.datetime.now (), and in the view do something like:

display_datetime = model.date_time + datetime.timedelta(USER_UTC_OFFSET) 
+4
source share
2 answers

To my great surprise, this is similar.

 web81:~/webapps/dominicrodger2/dominicrodger$ python2.5 manage.py shell Python 2.5.4 (r254:67916, Aug 5 2009, 12:42:40) [GCC 4.1.2 20080704 (Red Hat 4.1.2-44)] on linux2 Type "help", "copyright", "credits" or "license" for more information. (InteractiveConsole) >>> import settings >>> settings.TIME_ZONE 'Europe/London' >>> from datetime import datetime >>> datetime.now() datetime.datetime(2009, 10, 15, 6, 29, 58, 85662) >>> exit() web81:~/webapps/dominicrodger2/dominicrodger$ date Thu Oct 15 00:31:10 CDT 2009 

And yes, I really got distracted by writing this answer :-)

I use the TIME_ZONE parameter so that automatically added timestamps when creating an object (using auto_now_add , which, it seems to me, will soon become obsolete) show the creation time in the time zone you set.

If you want to convert these times into time zones for visitors to your site, you will need to do a little more work, as shown in the example that you gave. If you want to make a lot of time to display the time on the clock of your time on the website, I highly recommend that you set your TIME_ZONE settings for storing time in UTC, because it will simplify your life in the long run (you can simply use UTC offsets instead of worrying about summer savings).

If you're interested, I believe the time zone is set from the TIME_ZONE parameter here .

Change, for your comment, that it does not work on Windows, this is because of the following in the Django source:

 if hasattr(time, 'tzset'): # Move the time zone info into os.environ. See ticket #2315 for why # we don't do this unconditionally (breaks Windows). os.environ['TZ'] = self.TIME_ZONE time.tzset() 

Window:

 C:\Documents and Settings\drodger>python ActivePython 2.6.1.1 (ActiveState Software Inc.) based on Python 2.6.1 (r261:67515, Dec 5 2008, 13:58:38) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import time >>> hasattr(time, 'tzset') False 

Linux:

 web81:~$ python2.5 Python 2.5.4 (r254:67916, Aug 5 2009, 12:42:40) [GCC 4.1.2 20080704 (Red Hat 4.1.2-44)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import time >>> hasattr(time, 'tzset') True 
+4
source

With TIME_ZONE both UTC, utcnow () and now () are the same. This is probably what you want. Then you can record the time like now / utcnow, and functions like timesince will work fine for each user. To display absolute times for specific users, you can use the utc offsets as you suggest.

+1
source

All Articles