Natural/Relative days in Python
Asked Answered
U

5

41

I'd like a way to show natural times for dated items in Python. Similar to how Twitter will show a message from "a moment ago", "a few minutes ago", "two hours ago", "three days ago", etc.

Django 1.0 has a "humanize" method in django.contrib. I'm not using the Django framework, and even if I were, it's more limited than what I'd like.

Please let me (and generations of future searchers) know if there is a good working solution already. Since this is a common enough task, I imagine there must be something.

Unprepared answered 4/1, 2009 at 1:20 Comment(1)
I found this: code.google.com/p/parsedatetime But this seems to be for parsing many different forms of dates, whereas I'm simply interested in formatting a datetime object.Unprepared
U
21

While not useful to you at this very moment, it may be so for future searchers: The babel module, which deals with all sorts of locale stuff, has a function for doing more or less what you want. Currently it's only in their trunk though, not in the latest public release (version 0.9.4). Once the functionality lands in a release, you could do something like:

from datetime import timedelta
from babel.dates import format_timedelta
delta = timedelta(days=6)
format_timedelta(delta, locale='en_US')
u'1 week'

This is taken straight from the babel documentation on time delta formatting. This will at least get you parts of the way. It wont do fuzziness down to the level of "moments ago" and such, but it will do "n minutes" etc. correctly pluralized.

For what it's worth, the babel module also contains functions for formatting dates and times according to locale, Which might be useful when the time delta is large.

Unstop answered 4/1, 2009 at 4:52 Comment(1)
How to do same in Python 3 if python3-babel isn't available in the distro repositories? Thanks.Bret
N
42

Twitter dates in specific are interesting because they are relative only for the first day. After 24 hours they just show the month and day. After a year they start showing the last two digits of the year. Here's a sample function that does something more akin to Twitter relative dates, though it always shows the year too after 24 hours. It's US locale only, but you can always alter it as needed.

# tested in Python 2.7
import datetime
def prettydate(d):
    diff = datetime.datetime.utcnow() - d
    s = diff.seconds
    if diff.days > 7 or diff.days < 0:
        return d.strftime('%d %b %y')
    elif diff.days == 1:
        return '1 day ago'
    elif diff.days > 1:
        return '{} days ago'.format(diff.days)
    elif s <= 1:
        return 'just now'
    elif s < 60:
        return '{} seconds ago'.format(s)
    elif s < 120:
        return '1 minute ago'
    elif s < 3600:
        return '{} minutes ago'.format(s/60)
    elif s < 7200:
        return '1 hour ago'
    else:
        return '{} hours ago'.format(s/3600)
Nocturnal answered 2/3, 2011 at 6:9 Comment(1)
Your solution is by far the smallest and most elegant one. It just implies that we have to translate all the strings. But I'll take that one, thank you!Suppressive
U
21

While not useful to you at this very moment, it may be so for future searchers: The babel module, which deals with all sorts of locale stuff, has a function for doing more or less what you want. Currently it's only in their trunk though, not in the latest public release (version 0.9.4). Once the functionality lands in a release, you could do something like:

from datetime import timedelta
from babel.dates import format_timedelta
delta = timedelta(days=6)
format_timedelta(delta, locale='en_US')
u'1 week'

This is taken straight from the babel documentation on time delta formatting. This will at least get you parts of the way. It wont do fuzziness down to the level of "moments ago" and such, but it will do "n minutes" etc. correctly pluralized.

For what it's worth, the babel module also contains functions for formatting dates and times according to locale, Which might be useful when the time delta is large.

Unstop answered 4/1, 2009 at 4:52 Comment(1)
How to do same in Python 3 if python3-babel isn't available in the distro repositories? Thanks.Bret
C
13

There is the humanize package:

>>> import humanize
>>> import datetime
>>> humanize.naturalday(datetime.datetime.now())
'today'
>>> humanize.naturalday(datetime.datetime.now() - datetime.timedelta(days=1))
'yesterday'
>>> humanize.naturalday(datetime.date(2007, 6, 5))
'Jun 05'
>>> humanize.naturaldate(datetime.date(2007, 6, 5))
'Jun 05 2007'
>>> humanize.naturaltime(datetime.datetime.now() - datetime.timedelta(seconds=1))
'a second ago'
>>> humanize.naturaltime(datetime.datetime.now() - datetime.timedelta(seconds=3600))
'an hour ago'

Examples for your use case:

>>> humanize.naturaltime(datetime.datetime.now() - datetime.timedelta(seconds=36000))
'10 hours ago'
>>> humanize.naturaltime(datetime.datetime.now() - datetime.timedelta(seconds=360000))
'4 days ago'
>>> humanize.naturaltime(datetime.datetime.now() - datetime.timedelta(seconds=3600000))
'a month ago'

Further (see link above) it also supports humanization of:

  • integers
  • file sizes
  • floats (to fractional numbers)
Customhouse answered 5/5, 2015 at 9:17 Comment(0)
B
8

Or you could easily adapt timesince.py from Django which only has 2 other dependencies to itself: one for translation (which you might not need) and one for timezones (which can be easily adapted).

By the way, Django has a BSD license which is pretty flexible, you'll be able to use it in whatever project you are currently using.

Busty answered 4/1, 2009 at 3:2 Comment(0)
T
3

Found out that obe can use maya like this

pip install maya
>>> maya.when("3 week ago").datetime().strftime("%Y-%m-%d")
'2020-04-27'
Tiresias answered 18/5, 2020 at 16:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.