Created
March 1, 2014 18:20
-
-
Save danielthiel/9294494 to your computer and use it in GitHub Desktop.
Returns a prettyfied date
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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