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:
parent
3732cc30f2
commit
50e8637b71
|
@ -26,12 +26,14 @@ Utilities
|
||||||
|
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
import datetime
|
||||||
|
|
||||||
import pytz
|
import pytz
|
||||||
import humanize
|
import humanize
|
||||||
|
|
||||||
from webhelpers.html import HTML
|
from webhelpers.html import HTML
|
||||||
|
|
||||||
from rattail.time import timezone
|
from rattail.time import timezone, make_utc
|
||||||
|
|
||||||
|
|
||||||
def pretty_datetime(config, value):
|
def pretty_datetime(config, value):
|
||||||
|
@ -52,11 +54,13 @@ def pretty_datetime(config, value):
|
||||||
if not value.tzinfo:
|
if not value.tzinfo:
|
||||||
value = pytz.utc.localize(value)
|
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)
|
local = timezone(config)
|
||||||
value = local.normalize(value.astimezone(local))
|
value = local.normalize(value.astimezone(local))
|
||||||
naive_value = value.replace(tzinfo=None)
|
|
||||||
|
|
||||||
return HTML.tag('span',
|
return HTML.tag('span',
|
||||||
title=value.strftime('%Y-%m-%d %H:%M:%S %Z%z'),
|
title=value.strftime('%Y-%m-%d %H:%M:%S %Z%z'),
|
||||||
c=humanize.naturaltime(naive_value))
|
c=humanize.naturaltime(time_ago))
|
||||||
|
|
Loading…
Reference in a new issue