Skip to content

Commit

Permalink
Fixed bug with Django 1.4 with image/file popups caused by naive/awar…
Browse files Browse the repository at this point in the history
…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
spookylukey committed Jan 17, 2012
1 parent 6a79625 commit 88801ae
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions fiber/utils/date.py
@@ -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:
Expand Down

0 comments on commit 88801ae

Please sign in to comment.