From 90e08b4863dfaecafee5b151478bda4513b12e85 Mon Sep 17 00:00:00 2001 From: Dan McGee Date: Fri, 23 Mar 2012 19:29:40 -0500 Subject: Make all datetime objects fully timezone aware This is most of the transition to Django 1.4 `USE_TZ = True`. We need to ensure we don't mix aware and non-aware datetime objects when dealing with datetimes in the code. Add a utc_now() helper method that we can use most places, and ensure there is always a timezone attached when necessary. Signed-off-by: Dan McGee --- main/utils.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'main/utils.py') diff --git a/main/utils.py b/main/utils.py index 8143ea6..e7e47c5 100644 --- a/main/utils.py +++ b/main/utils.py @@ -5,6 +5,7 @@ except ImportError: from datetime import datetime import hashlib +from pytz import utc from django.core.cache import cache @@ -91,12 +92,17 @@ def retrieve_latest(sender): return None +def utc_now(): + '''Returns a timezone-aware UTC date representing now.''' + return datetime.utcnow().replace(tzinfo=utc) + + def set_created_field(sender, **kwargs): - '''This will set the 'created' field on any object to datetime.utcnow() if - it is unset. For use as a pre_save signal handler.''' + '''This will set the 'created' field on any object to the current UTC time + if it is unset. For use as a pre_save signal handler.''' obj = kwargs['instance'] if hasattr(obj, 'created') and not obj.created: - obj.created = datetime.utcnow() + obj.created = utc_now() def groupby_preserve_order(iterable, keyfunc): -- cgit v1.2.3-24-g4f1b