1

Ok, so I am reading a ton of posts about how it is a utc time and use pytz to convert the time so it is useful. I can do all that, but it seems to be built into Django as on the admin page, if I create an entry with a datetimefield, it shows it as my time, saves it as utc and if I open it back up to edit, it shows it as my time again. So my question is, how is the Django admin doing this? I would rather do it that way than use another module.

Dave_750
  • 1,225
  • 1
  • 13
  • 28
  • Do you want to know the settings for the same or the code for how to do that? – karthikr Jun 14 '13 at 17:19
  • I found the answer I needed here. [http://stackoverflow.com/questions/5452555/converting-timezone-aware-datetime-to-local-time-in-python][1] Exactly what I was looking for [1]: http://stackoverflow.com/questions/5452555/converting-timezone-aware-datetime-to-local-time-in-python – Dave_750 Jun 14 '13 at 19:37

1 Answers1

1

Django has a utc class that loads pytz.utc if pytz is installed and a custom UTC class when it is not. You can use it for the tzinfo argument in a datetime object as shown below.

import datetime
from django.utils.timezone import utc

datetime.datetime(2013, 2, 20, 0, 0, tzinfo=utc)

I also prefer not to add additional dependencies, but I've learned that pytz is one of those useful libraries that you will need sooner or later. Now I just include it on on all my Django projects by default.

garnertb
  • 9,454
  • 36
  • 38
  • Thanks, I guess one more question, how would I convert the datetime object in utc returned by the model to say Central Time US? Nothing I can find has been working for me. – Dave_750 Jun 14 '13 at 19:33
  • Does this work https://docs.djangoproject.com/en/dev/topics/i18n/timezones/#timezone? – garnertb Jun 14 '13 at 19:41
  • Yes, I found the localtime method, I thought it answered here but SO made it into a comment above, sorry. Much appreciated though. This is my first stab at Django. – Dave_750 Jun 14 '13 at 19:49