OLD | NEW |
(Empty) | |
| 1 from __future__ import unicode_literals |
| 2 |
| 3 import calendar |
| 4 import datetime |
| 5 |
| 6 from django.utils.html import avoid_wrapping |
| 7 from django.utils.timezone import is_aware, utc |
| 8 from django.utils.translation import ugettext, ungettext_lazy |
| 9 |
| 10 TIMESINCE_CHUNKS = ( |
| 11 (60 * 60 * 24 * 365, ungettext_lazy('%d year', '%d years')), |
| 12 (60 * 60 * 24 * 30, ungettext_lazy('%d month', '%d months')), |
| 13 (60 * 60 * 24 * 7, ungettext_lazy('%d week', '%d weeks')), |
| 14 (60 * 60 * 24, ungettext_lazy('%d day', '%d days')), |
| 15 (60 * 60, ungettext_lazy('%d hour', '%d hours')), |
| 16 (60, ungettext_lazy('%d minute', '%d minutes')) |
| 17 ) |
| 18 |
| 19 |
| 20 def timesince(d, now=None, reversed=False): |
| 21 """ |
| 22 Takes two datetime objects and returns the time between d and now |
| 23 as a nicely formatted string, e.g. "10 minutes". If d occurs after now, |
| 24 then "0 minutes" is returned. |
| 25 |
| 26 Units used are years, months, weeks, days, hours, and minutes. |
| 27 Seconds and microseconds are ignored. Up to two adjacent units will be |
| 28 displayed. For example, "2 weeks, 3 days" and "1 year, 3 months" are |
| 29 possible outputs, but "2 weeks, 3 hours" and "1 year, 5 days" are not. |
| 30 |
| 31 Adapted from |
| 32 http://web.archive.org/web/20060617175230/http://blog.natbat.co.uk/archive/2
003/Jun/14/time_since |
| 33 """ |
| 34 # Convert datetime.date to datetime.datetime for comparison. |
| 35 if not isinstance(d, datetime.datetime): |
| 36 d = datetime.datetime(d.year, d.month, d.day) |
| 37 if now and not isinstance(now, datetime.datetime): |
| 38 now = datetime.datetime(now.year, now.month, now.day) |
| 39 |
| 40 if not now: |
| 41 now = datetime.datetime.now(utc if is_aware(d) else None) |
| 42 |
| 43 if reversed: |
| 44 d, now = now, d |
| 45 delta = now - d |
| 46 |
| 47 # Deal with leapyears by subtracing the number of leapdays |
| 48 leapdays = calendar.leapdays(d.year, now.year) |
| 49 if leapdays != 0: |
| 50 if calendar.isleap(d.year): |
| 51 leapdays -= 1 |
| 52 elif calendar.isleap(now.year): |
| 53 leapdays += 1 |
| 54 delta -= datetime.timedelta(leapdays) |
| 55 |
| 56 # ignore microseconds |
| 57 since = delta.days * 24 * 60 * 60 + delta.seconds |
| 58 if since <= 0: |
| 59 # d is in the future compared to now, stop processing. |
| 60 return avoid_wrapping(ugettext('0 minutes')) |
| 61 for i, (seconds, name) in enumerate(TIMESINCE_CHUNKS): |
| 62 count = since // seconds |
| 63 if count != 0: |
| 64 break |
| 65 result = avoid_wrapping(name % count) |
| 66 if i + 1 < len(TIMESINCE_CHUNKS): |
| 67 # Now get the second item |
| 68 seconds2, name2 = TIMESINCE_CHUNKS[i + 1] |
| 69 count2 = (since - (seconds * count)) // seconds2 |
| 70 if count2 != 0: |
| 71 result += ugettext(', ') + avoid_wrapping(name2 % count2) |
| 72 return result |
| 73 |
| 74 |
| 75 def timeuntil(d, now=None): |
| 76 """ |
| 77 Like timesince, but returns a string measuring the time until |
| 78 the given time. |
| 79 """ |
| 80 return timesince(d, now, reversed=True) |
OLD | NEW |