2012-06-07 16:08:47 +00:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2010-09-27 15:15:33 +00:00
|
|
|
import datetime
|
2010-09-28 08:18:12 +00:00
|
|
|
import unittest
|
2007-09-17 04:50:12 +00:00
|
|
|
|
2010-09-27 15:15:33 +00:00
|
|
|
from django.utils.timesince import timesince, timeuntil
|
|
|
|
from django.utils.tzinfo import LocalTimezone, FixedOffset
|
2007-09-17 04:50:12 +00:00
|
|
|
|
2010-09-28 08:18:12 +00:00
|
|
|
class TimesinceTests(unittest.TestCase):
|
2007-09-17 04:50:12 +00:00
|
|
|
|
2010-09-27 15:15:33 +00:00
|
|
|
def setUp(self):
|
|
|
|
self.t = datetime.datetime(2007, 8, 14, 13, 46, 0)
|
|
|
|
self.onemicrosecond = datetime.timedelta(microseconds=1)
|
|
|
|
self.onesecond = datetime.timedelta(seconds=1)
|
|
|
|
self.oneminute = datetime.timedelta(minutes=1)
|
|
|
|
self.onehour = datetime.timedelta(hours=1)
|
|
|
|
self.oneday = datetime.timedelta(days=1)
|
|
|
|
self.oneweek = datetime.timedelta(days=7)
|
|
|
|
self.onemonth = datetime.timedelta(days=30)
|
|
|
|
self.oneyear = datetime.timedelta(days=365)
|
2007-09-17 04:50:12 +00:00
|
|
|
|
2010-09-27 15:15:33 +00:00
|
|
|
def test_equal_datetimes(self):
|
|
|
|
""" equal datetimes. """
|
2012-06-07 16:08:47 +00:00
|
|
|
self.assertEqual(timesince(self.t, self.t), '0 minutes')
|
2007-09-17 04:50:12 +00:00
|
|
|
|
2010-09-27 15:15:33 +00:00
|
|
|
def test_ignore_microseconds_and_seconds(self):
|
|
|
|
""" Microseconds and seconds are ignored. """
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(timesince(self.t, self.t+self.onemicrosecond),
|
2012-06-07 16:08:47 +00:00
|
|
|
'0 minutes')
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(timesince(self.t, self.t+self.onesecond),
|
2012-06-07 16:08:47 +00:00
|
|
|
'0 minutes')
|
2007-09-17 04:50:12 +00:00
|
|
|
|
2010-09-27 15:15:33 +00:00
|
|
|
def test_other_units(self):
|
|
|
|
""" Test other units. """
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(timesince(self.t, self.t+self.oneminute),
|
2012-06-07 16:08:47 +00:00
|
|
|
'1 minute')
|
|
|
|
self.assertEqual(timesince(self.t, self.t+self.onehour), '1 hour')
|
|
|
|
self.assertEqual(timesince(self.t, self.t+self.oneday), '1 day')
|
|
|
|
self.assertEqual(timesince(self.t, self.t+self.oneweek), '1 week')
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(timesince(self.t, self.t+self.onemonth),
|
2012-06-07 16:08:47 +00:00
|
|
|
'1 month')
|
|
|
|
self.assertEqual(timesince(self.t, self.t+self.oneyear), '1 year')
|
2007-09-17 04:50:12 +00:00
|
|
|
|
2010-09-27 15:15:33 +00:00
|
|
|
def test_multiple_units(self):
|
|
|
|
""" Test multiple units. """
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(timesince(self.t,
|
2012-06-07 16:08:47 +00:00
|
|
|
self.t+2*self.oneday+6*self.onehour), '2 days, 6 hours')
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(timesince(self.t,
|
2012-06-07 16:08:47 +00:00
|
|
|
self.t+2*self.oneweek+2*self.oneday), '2 weeks, 2 days')
|
2008-08-26 08:08:55 +00:00
|
|
|
|
2010-09-27 15:15:33 +00:00
|
|
|
def test_display_first_unit(self):
|
|
|
|
"""
|
|
|
|
If the two differing units aren't adjacent, only the first unit is
|
|
|
|
displayed.
|
|
|
|
"""
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(timesince(self.t,
|
2010-09-27 15:15:33 +00:00
|
|
|
self.t+2*self.oneweek+3*self.onehour+4*self.oneminute),
|
2012-06-07 16:08:47 +00:00
|
|
|
'2 weeks')
|
2009-04-07 22:12:53 +00:00
|
|
|
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(timesince(self.t,
|
2012-06-07 16:08:47 +00:00
|
|
|
self.t+4*self.oneday+5*self.oneminute), '4 days')
|
2010-09-27 15:15:33 +00:00
|
|
|
|
|
|
|
def test_display_second_before_first(self):
|
|
|
|
"""
|
|
|
|
When the second date occurs before the first, we should always
|
|
|
|
get 0 minutes.
|
|
|
|
"""
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(timesince(self.t, self.t-self.onemicrosecond),
|
2012-06-07 16:08:47 +00:00
|
|
|
'0 minutes')
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(timesince(self.t, self.t-self.onesecond),
|
2012-06-07 16:08:47 +00:00
|
|
|
'0 minutes')
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(timesince(self.t, self.t-self.oneminute),
|
2012-06-07 16:08:47 +00:00
|
|
|
'0 minutes')
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(timesince(self.t, self.t-self.onehour),
|
2012-06-07 16:08:47 +00:00
|
|
|
'0 minutes')
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(timesince(self.t, self.t-self.oneday),
|
2012-06-07 16:08:47 +00:00
|
|
|
'0 minutes')
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(timesince(self.t, self.t-self.oneweek),
|
2012-06-07 16:08:47 +00:00
|
|
|
'0 minutes')
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(timesince(self.t, self.t-self.onemonth),
|
2012-06-07 16:08:47 +00:00
|
|
|
'0 minutes')
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(timesince(self.t, self.t-self.oneyear),
|
2012-06-07 16:08:47 +00:00
|
|
|
'0 minutes')
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(timesince(self.t,
|
2012-06-07 16:08:47 +00:00
|
|
|
self.t-2*self.oneday-6*self.onehour), '0 minutes')
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(timesince(self.t,
|
2012-06-07 16:08:47 +00:00
|
|
|
self.t-2*self.oneweek-2*self.oneday), '0 minutes')
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(timesince(self.t,
|
2010-09-27 15:15:33 +00:00
|
|
|
self.t-2*self.oneweek-3*self.onehour-4*self.oneminute),
|
2012-06-07 16:08:47 +00:00
|
|
|
'0 minutes')
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(timesince(self.t,
|
2012-06-07 16:08:47 +00:00
|
|
|
self.t-4*self.oneday-5*self.oneminute), '0 minutes')
|
2010-09-27 15:15:33 +00:00
|
|
|
|
|
|
|
def test_different_timezones(self):
|
|
|
|
""" When using two different timezones. """
|
|
|
|
now = datetime.datetime.now()
|
|
|
|
now_tz = datetime.datetime.now(LocalTimezone(now))
|
|
|
|
now_tz_i = datetime.datetime.now(FixedOffset((3 * 60) + 15))
|
|
|
|
|
2012-06-07 16:08:47 +00:00
|
|
|
self.assertEqual(timesince(now), '0 minutes')
|
|
|
|
self.assertEqual(timesince(now_tz), '0 minutes')
|
|
|
|
self.assertEqual(timeuntil(now_tz, now_tz_i), '0 minutes')
|
2010-09-27 15:15:33 +00:00
|
|
|
|
2012-03-22 07:29:39 +00:00
|
|
|
def test_date_objects(self):
|
|
|
|
""" Both timesince and timeuntil should work on date objects (#17937). """
|
|
|
|
today = datetime.date.today()
|
2012-06-07 16:08:47 +00:00
|
|
|
self.assertEqual(timesince(today + self.oneday), '0 minutes')
|
|
|
|
self.assertEqual(timeuntil(today - self.oneday), '0 minutes')
|
2012-03-22 07:29:39 +00:00
|
|
|
|
2010-09-27 15:15:33 +00:00
|
|
|
def test_both_date_objects(self):
|
|
|
|
""" Timesince should work with both date objects (#9672) """
|
|
|
|
today = datetime.date.today()
|
2012-06-07 16:08:47 +00:00
|
|
|
self.assertEqual(timeuntil(today + self.oneday, today), '1 day')
|
|
|
|
self.assertEqual(timeuntil(today - self.oneday, today), '0 minutes')
|
|
|
|
self.assertEqual(timeuntil(today + self.oneweek, today), '1 week')
|
2011-11-18 13:01:06 +00:00
|
|
|
|
|
|
|
def test_naive_datetime_with_tzinfo_attribute(self):
|
|
|
|
class naive(datetime.tzinfo):
|
|
|
|
def utcoffset(self, dt):
|
|
|
|
return None
|
|
|
|
future = datetime.datetime(2080, 1, 1, tzinfo=naive())
|
2012-06-07 16:08:47 +00:00
|
|
|
self.assertEqual(timesince(future), '0 minutes')
|
2011-11-18 13:01:06 +00:00
|
|
|
past = datetime.datetime(1980, 1, 1, tzinfo=naive())
|
2012-06-07 16:08:47 +00:00
|
|
|
self.assertEqual(timeuntil(past), '0 minutes')
|