Show year in time_since when ambiguous

This commit is contained in:
Mouse Reeve 2020-12-12 20:02:14 -08:00
parent b61a7cccff
commit 47981dc15e
2 changed files with 36 additions and 1 deletions

View file

@ -132,6 +132,8 @@ def time_since(date):
delta = now - date delta = now - date
if date < (now - relativedelta(weeks=1)): if date < (now - relativedelta(weeks=1)):
if date.year != now.year:
return date.strftime('%b %-d %Y')
return date.strftime('%b %-d') return date.strftime('%b %-d')
delta = relativedelta(now, date) delta = relativedelta(now, date)
if delta.days: if delta.days:

View file

@ -3,6 +3,7 @@ import re
from unittest.mock import patch from unittest.mock import patch
from dateutil.parser import parse from dateutil.parser import parse
from dateutil.relativedelta import relativedelta
from django.test import TestCase from django.test import TestCase
from django.utils import timezone from django.utils import timezone
@ -205,5 +206,37 @@ class TemplateTags(TestCase):
def test_get_uuid(self): def test_get_uuid(self):
''' uuid functionality ''' ''' uuid functionality '''
uuid = bookwyrm_tags.get_uuid('hi') uuid = bookwyrm_tags.get_uuid('hi')
print(uuid)
self.assertTrue(re.match(r'hi[A-Za-z0-9\-]', uuid)) self.assertTrue(re.match(r'hi[A-Za-z0-9\-]', uuid))
def test_time_since(self):
''' ultraconcise timestamps '''
self.assertEqual(bookwyrm_tags.time_since('bleh'), '')
now = timezone.now()
self.assertEqual(bookwyrm_tags.time_since(now), '0s')
seconds_ago = now - relativedelta(seconds=4)
self.assertEqual(bookwyrm_tags.time_since(seconds_ago), '4s')
minutes_ago = now - relativedelta(minutes=8)
self.assertEqual(bookwyrm_tags.time_since(minutes_ago), '8m')
hours_ago = now - relativedelta(hours=9)
self.assertEqual(bookwyrm_tags.time_since(hours_ago), '9h')
days_ago = now - relativedelta(days=3)
self.assertEqual(bookwyrm_tags.time_since(days_ago), '3d')
# I am not going to figure out how to mock dates tonight.
months_ago = now - relativedelta(months=5)
self.assertTrue(re.match(
r'[A-Z][a-z]{2} \d?\d',
bookwyrm_tags.time_since(months_ago)
))
years_ago = now - relativedelta(years=10)
self.assertTrue(re.match(
r'[A-Z][a-z]{2} \d?\d \d{4}',
bookwyrm_tags.time_since(years_ago)
))