Browse Source

Reduce datetime precision to 1 in topic listings

I don't think the extra precision is very meaningful on topics in
listings (especially now that it changes to absolute dates after a
week), so we can reduce that and it will make things look a bit less
cluttered.
merge-requests/55/head
Deimos 6 years ago
parent
commit
e00ac89dec
  1. 13
      tildes/tildes/lib/datetime.py
  2. 6
      tildes/tildes/templates/macros/datetime.jinja2
  3. 2
      tildes/tildes/templates/macros/topics.jinja2

13
tildes/tildes/lib/datetime.py

@ -5,7 +5,7 @@
from datetime import datetime, timedelta, timezone
import re
from typing import Any
from typing import Any, Optional
from ago import human
@ -84,7 +84,9 @@ def utc_from_timestamp(timestamp: int) -> datetime:
return datetime.fromtimestamp(timestamp, timezone.utc)
def descriptive_timedelta(target: datetime, abbreviate: bool = False) -> str:
def descriptive_timedelta(
target: datetime, abbreviate: bool = False, precision: Optional[int] = None
) -> str:
"""Return a descriptive string for how long ago a datetime was.
The returned string will be of a format like "4 hours ago" or "3 hours, 21 minutes
@ -105,6 +107,7 @@ def descriptive_timedelta(target: datetime, abbreviate: bool = False) -> str:
if seconds_ago < 1:
return "a moment ago"
if not precision:
# determine whether one or two precision levels is appropriate
if seconds_ago < 3600:
# if it's less than an hour, we always want only one precision level
@ -132,13 +135,15 @@ def descriptive_timedelta(target: datetime, abbreviate: bool = False) -> str:
return result
def adaptive_date(target: datetime, abbreviate: bool = False) -> str:
def adaptive_date(
target: datetime, abbreviate: bool = False, precision: Optional[int] = None
) -> str:
"""Return a date string that switches from relative to absolute past a threshold."""
threshold = timedelta(days=7)
# if the date is more recent than threshold, return the relative "ago"-style string
if utc_now() - target < threshold:
return descriptive_timedelta(target, abbreviate)
return descriptive_timedelta(target, abbreviate, precision)
# if abbreviating, use the short version of month name ("Dec" instead of "December")
if abbreviate:

6
tildes/tildes/templates/macros/datetime.jinja2

@ -5,11 +5,11 @@
<time datetime="{{ datetime.strftime('%Y-%m-%dT%H:%M:%SZ') }}" title="{{ datetime.strftime('%Y-%m-%d %H:%M:%S UTC') }}">{{ datetime|ago(abbreviate) }}</time>
{%- endmacro %}
{% macro adaptive_date_responsive(datetime, class_=None) -%}
{% macro adaptive_date_responsive(datetime, class_=None, precision=None) -%}
<time
class="time-responsive{{ ' '+class_ if class_ else '' }}"
datetime="{{ datetime.strftime('%Y-%m-%dT%H:%M:%SZ') }}"
title="{{ datetime.strftime('%Y-%m-%d %H:%M:%S UTC') }}"
data-abbreviated="{{ datetime|adaptive_date(abbreviate=True) }}"
><span class="time-responsive-full">{{ datetime|adaptive_date(abbreviate=False) }}</span></time>
data-abbreviated="{{ datetime|adaptive_date(abbreviate=True, precision=precision) }}"
><span class="time-responsive-full">{{ datetime|adaptive_date(abbreviate=False, precision=precision) }}</span></time>
{%- endmacro %}

2
tildes/tildes/templates/macros/topics.jinja2

@ -85,7 +85,7 @@
<div aria-label="Link domain">{{ topic.link_domain }}</div>
{% endif %}
<div aria-label="Post time">{{ adaptive_date_responsive(topic.created_time) }}</div>
<div aria-label="Post time">{{ adaptive_date_responsive(topic.created_time, precision=1) }}</div>
</footer>
{{ topic_voting(topic) }}

Loading…
Cancel
Save