Fix timezone issues with util.pretty_datetime() function.

Seems we should just calculate the "time ago" value instead of just
providing a "then" timestamp and expecting the humanize library to
understand exactly what we meant.
This commit is contained in:
Lance Edgar 2015-07-20 09:52:24 -05:00
parent 3732cc30f2
commit 50e8637b71

View file

@ -26,12 +26,14 @@ Utilities
from __future__ import unicode_literals
import datetime
import pytz
import humanize
from webhelpers.html import HTML
from rattail.time import timezone
from rattail.time import timezone, make_utc
def pretty_datetime(config, value):
@ -52,11 +54,13 @@ def pretty_datetime(config, value):
if not value.tzinfo:
value = pytz.utc.localize(value)
# Convert value to local timezone, and make a naive copy.
# Calculate time diff using UTC.
time_ago = datetime.datetime.utcnow() - make_utc(value)
# Convert value to local timezone.
local = timezone(config)
value = local.normalize(value.astimezone(local))
naive_value = value.replace(tzinfo=None)
return HTML.tag('span',
title=value.strftime('%Y-%m-%d %H:%M:%S %Z%z'),
c=humanize.naturaltime(naive_value))
c=humanize.naturaltime(time_ago))