Révision ecc2c1c7
Ajouté par Benjamin Dauvergne il y a plus de 10 ans
entrouvert/djommon/humantime/utils.py | ||
---|---|---|
1 | 1 |
import datetime |
2 | 2 |
|
3 |
from django.utils.translation import npgettext, pgettext |
|
3 |
from django.utils.translation import pgettext |
|
4 |
from django.template.defaultfilters import date |
|
4 | 5 |
|
5 | 6 |
def datetime2human(dt, include_time=False, days_limit=7): |
6 | 7 |
'''Format a datetime object for human consumption''' |
... | ... | |
11 | 12 |
include_time = False |
12 | 13 |
today = datetime.date.today() |
13 | 14 |
yesterday = today - datetime.timedelta(days=1) |
14 |
date = dt.date() |
|
15 |
if date == today: |
|
15 |
subdate = dt.date()
|
|
16 |
if subdate == today:
|
|
16 | 17 |
if include_time: |
17 | 18 |
return pgettext('humantime', 'today at {0}').format(time) |
18 | 19 |
else: |
19 | 20 |
return pgettext('humantime', 'today') |
20 |
elif date == yesterday: |
|
21 |
elif subdate == yesterday:
|
|
21 | 22 |
if include_time: |
22 | 23 |
return pgettext('humantime', 'yesterday at {0}').format(time) |
23 | 24 |
else: |
24 | 25 |
return pgettext('humantime', 'yesterday') |
25 | 26 |
else: |
26 |
delta = (today - date).days |
|
27 |
if delta <= days_limit: |
|
28 |
return npgettext('humantime', '{0} day ago', '{0} days ago', |
|
29 |
delta).format(delta) |
|
27 |
if include_time: |
|
28 |
return date(dt, 'SHORT_DATETIME_FORMAT') |
|
30 | 29 |
else: |
31 |
return npgettext('humantime', 'more than {0} day ago', 'more than {0} days ago', |
|
32 |
days_limit).format(days_limit) |
|
30 |
return date(dt, 'SHORT_DATE_FORMAT') |
Formats disponibles : Unified diff
humantime: remove x days ago formulation, use short date time format instead