Quantize listing dates to "just now", like in JS.

This commit is contained in:
Max Goodman
2014-04-25 13:53:30 -07:00
parent 6a27c1bf78
commit f93b5c42ed
2 changed files with 20 additions and 16 deletions

View File

@@ -22,7 +22,7 @@
from r2.models import *
from filters import unsafe, websafe, _force_unicode, _force_utf8
from r2.lib.utils import UrlParser, timesince, is_subdomain
from r2.lib.utils import UrlParser, timeago, timesince, is_subdomain
from r2.lib import hooks
from r2.lib.static import static_mtime
@@ -281,16 +281,16 @@ def replace_render(listing, item, render_func):
if hasattr(item, "promoted") and item.promoted is not None:
from r2.lib import promote
# promoted links are special in their date handling
replacements['timesince'] = timesince(item._date -
promote.timezone_offset)
replacements['timesince'] = \
simplified_timesince(item._date - promote.timezone_offset)
else:
replacements['timesince'] = timesince(item._date)
replacements['timesince'] = simplified_timesince(item._date)
replacements['time_period'] = calc_time_period(item._date)
# compute the last edited time here so we don't end up caching it
if hasattr(item, "editted") and not isinstance(item.editted, bool):
replacements['lastedited'] = timesince(item.editted)
replacements['lastedited'] = simplified_timesince(item.editted)
# Set in front.py:GET_comments()
replacements['previous_visits_hex'] = c.previous_visits_hex
@@ -558,3 +558,14 @@ def html_datetime(date):
def js_timestamp(date):
return '%d' % (calendar.timegm(date.timetuple()) * 1000)
def simplified_timesince(date, include_tense=True):
if date > timeago("1 minute"):
return _("just now")
since = []
since.append(timesince(date))
if include_tense:
since.append(_("ago"))
return " ".join(since)

View File

@@ -24,8 +24,8 @@
import json
from r2.models import FakeSubreddit
from r2.lib.filters import spaceCompress, unsafe, safemarkdown
from r2.lib.template_helpers import add_sr, js_config, static, html_datetime
from r2.lib.utils import cols, long_datetime, timesince
from r2.lib.template_helpers import add_sr, js_config, static, html_datetime, simplified_timesince
from r2.lib.utils import cols, long_datetime
from r2.lib import tracking
from datetime import datetime
%>
@@ -550,17 +550,10 @@ ${unsafe(txt)}
## todo: use pubdate attribute once things are <article> tags.
## note: comment and link templates will pass a CachedVariable stub as since.
<%
now = date.now(g.tz)
timestamp_class = unsafe(' class="live-timestamp"') if live else ''
%>
<time title="${long_datetime(date)}" datetime="${html_datetime(date)}"${timestamp_class}>
${unsafe(since or timesince(date))}
%if include_tense and date < now:
${_("ago")}
%elif date > now:
${_("from now")}
%endif
${unsafe(since or simplified_timesince(date, include_tense))}
</time>
</%def>
@@ -594,7 +587,7 @@ ${unsafe(txt)}
<%def name="edited(thing, lastedited=None)">
%if isinstance(thing.editted, datetime):
<time class="edited-timestamp" title="${_('last edited')} ${unsafe(lastedited or timesince(thing.editted))} ${_('ago')}" datetime="${html_datetime(thing.editted)}">*</time>
<time class="edited-timestamp" title="${_('last edited')} ${unsafe(lastedited or simplified_timesince(thing.editted))}}" datetime="${html_datetime(thing.editted)}">*</time>
%elif thing.editted:
<em>*</em>
%endif