Skip to content

Commit

Permalink
Web app: make humanize_naturaltime resilient to 0001-01-01.
Browse files Browse the repository at this point in the history
  • Loading branch information
lemon24 committed Nov 5, 2023
1 parent c8606cb commit a9952db
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/reader/_app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import typing
from dataclasses import dataclass
from datetime import datetime
from datetime import timedelta
from datetime import timezone

import flask.signals
Expand Down Expand Up @@ -47,8 +48,14 @@
def humanize_naturaltime(dt):
when = None
if dt.tzinfo:
when = datetime.utcnow().replace(tzinfo=timezone.utc)
return humanize.naturaltime(dt, when=when)
when = datetime.now(tz=timezone.utc)
try:
return humanize.naturaltime(dt, when=when)
except ValueError as e:
# can happen for 0001-01-01
if 'year 0 is out of range' not in str(e):
raise
return humanize.naturaltime(dt + timedelta(days=1), when=when)


@blueprint.app_template_filter()
Expand Down

0 comments on commit a9952db

Please sign in to comment.