Ticket #9672: timeuntil.date.diff

File timeuntil.date.diff, 1.6 KB (added by rossp, 6 years ago)

Moved fix into timesince block so any use of timesince() will apply it, and added unit tests. Against Django r9739.

  • django/utils/timesince.py

     
    3434            now = datetime.datetime.now(LocalTimezone(d))
    3535        else:
    3636            now = datetime.datetime.now()
     37
     38    # Convert datetime.date to datetime.datetime for comparison
     39    if now.__class__ is not datetime.datetime:
     40        now = datetime.datetime(now.year, now.month, now.day)
    3741
    3842    # ignore microsecond part of 'd' since we removed it from 'now'
    3943    delta = now - (d - datetime.timedelta(0, 0, d.microsecond))
     
    6468            now = datetime.datetime.now(LocalTimezone(d))
    6569        else:
    6670            now = datetime.datetime.now()
     71
    6772    return timesince(now, d)
  • tests/regressiontests/utils/timesince.py

     
    11"""
    2 >>> from datetime import datetime, timedelta
     2>>> from datetime import datetime, timedelta, date
    33>>> from django.utils.timesince import timesince, timeuntil
    44>>> from django.utils.tzinfo import LocalTimezone, FixedOffset
    55
     
    8686u'0 minutes'
    8787>>> timeuntil(now_tz, now_tz_i)
    8888u'0 minutes'
     89
     90# Timeuntil should work with both date and datetime objects
     91>>> today = date.today()
     92>>> timeuntil(today+oneday, today)
     93u'1 day'
     94>>> timeuntil(today-oneday, today)
     95u'0 minutes'
     96>>> timeuntil(today+oneweek, today)
     97u'1 week'
    8998"""
Back to Top