Permalink
Browse files

Fixed bug with Django 1.4 with image/file popups caused by naive/awar…

…e datetime mismatch

This makes the friendly_datetime function work in the context of Django 1.4
and USE_TZ==True.  It retains compatibility for older versions of Django.
  • Loading branch information...
1 parent 6a79625 commit 88801ae46d9db786f9137eacfbaddb8c062ba0d2 @spookylukey committed Jan 17, 2012
Showing with 10 additions and 3 deletions.
  1. +10 −3 fiber/utils/date.py
View
@@ -1,14 +1,21 @@
from django.utils.translation import ugettext_lazy as _
+from datetime import datetime
+
+try:
+ # For Django 1.4, django.utils.timezone exists, and we need to handle
+ # timezone aware datetimes being passed in to friendly_datetime
+ from django.utils import timezone
+ tz_now = timezone.now
+except ImportError:
+ tz_now = datetime.now
def friendly_datetime(date_time):
"""
Given a datetime object or an int() Unix timestamp, return a friendly
string like 'an hour ago', 'yesterday', '3 months ago', 'just now', etc.
"""
- from datetime import datetime
-
- now = datetime.now()
+ now = tz_now()
if type(date_time) is datetime:
diff = now - date_time
elif type(date_time) is int:

0 comments on commit 88801ae

Please sign in to comment.