Skip to content

Instantly share code, notes, and snippets.

@danielthiel
Created March 1, 2014 18:20
Show Gist options
  • Save danielthiel/9294494 to your computer and use it in GitHub Desktop.
Save danielthiel/9294494 to your computer and use it in GitHub Desktop.
Returns a prettyfied date
def pretty_date(dt, default='gerade'):
"""
Returns string representing "time since" e.g.
3 days ago, 5 hours ago etc.
Ref: https://bitbucket.org/danjac/newsmeme/src/a281babb9ca3/newsmeme/
"""
now = datetime.utcnow()
diff = now - dt
periods = (
(diff.days / 365, 'Jahr', 'Jahren'),
(diff.days / 30, 'Monat', 'Monaten'),
(diff.days / 7, 'Woche', 'Wochen'),
(diff.days, 'Tag', 'Tagen'),
(diff.seconds / 3600, 'Stunde', 'Stunden'),
(diff.seconds / 60, 'Minute', 'Minuten'),
(diff.seconds, 'Sekunde', 'Sekunden'),
)
for period, singular, plural in periods:
if not period:
continue
if period == 1:
return u'vor %d %s' % (period, singular)
else:
return u'vor %d %s' % (period, plural)
return default
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment