2010-09-27 15:15:33 +00:00
|
|
|
import datetime
|
2023-04-14 04:10:31 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
import zoneinfo
|
|
|
|
except ImportError:
|
|
|
|
from backports import zoneinfo
|
2007-09-17 04:50:12 +00:00
|
|
|
|
2020-07-03 06:01:45 +00:00
|
|
|
from django.test import TestCase
|
2023-01-05 15:38:19 +00:00
|
|
|
from django.test.utils import override_settings, requires_tz_support
|
2019-10-28 10:17:29 +00:00
|
|
|
from django.utils import timezone, translation
|
2015-01-28 12:35:27 +00:00
|
|
|
from django.utils.timesince import timesince, timeuntil
|
2019-10-28 10:17:29 +00:00
|
|
|
from django.utils.translation import npgettext_lazy
|
2013-09-08 08:43:33 +00:00
|
|
|
|
2007-09-17 04:50:12 +00:00
|
|
|
|
2020-07-03 06:01:45 +00:00
|
|
|
class TimesinceTests(TestCase):
|
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)
|
2023-01-04 10:14:06 +00:00
|
|
|
self.onemonth = datetime.timedelta(days=31)
|
|
|
|
self.oneyear = datetime.timedelta(days=366)
|
2007-09-17 04:50:12 +00:00
|
|
|
|
2010-09-27 15:15:33 +00:00
|
|
|
def test_equal_datetimes(self):
|
|
|
|
"""equal datetimes."""
|
2013-05-18 11:58:45 +00:00
|
|
|
# NOTE: \xa0 avoids wrapping between value and unit
|
|
|
|
self.assertEqual(timesince(self.t, self.t), "0\xa0minutes")
|
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."""
|
2016-04-08 02:04:45 +00:00
|
|
|
self.assertEqual(
|
|
|
|
timesince(self.t, self.t + self.onemicrosecond), "0\xa0minutes"
|
|
|
|
)
|
|
|
|
self.assertEqual(timesince(self.t, self.t + self.onesecond), "0\xa0minutes")
|
2007-09-17 04:50:12 +00:00
|
|
|
|
2010-09-27 15:15:33 +00:00
|
|
|
def test_other_units(self):
|
|
|
|
"""Test other units."""
|
2016-04-08 02:04:45 +00:00
|
|
|
self.assertEqual(timesince(self.t, self.t + self.oneminute), "1\xa0minute")
|
2013-11-03 18:08:55 +00:00
|
|
|
self.assertEqual(timesince(self.t, self.t + self.onehour), "1\xa0hour")
|
|
|
|
self.assertEqual(timesince(self.t, self.t + self.oneday), "1\xa0day")
|
|
|
|
self.assertEqual(timesince(self.t, self.t + self.oneweek), "1\xa0week")
|
2016-04-08 02:04:45 +00:00
|
|
|
self.assertEqual(timesince(self.t, self.t + self.onemonth), "1\xa0month")
|
2013-11-03 18:08:55 +00:00
|
|
|
self.assertEqual(timesince(self.t, self.t + self.oneyear), "1\xa0year")
|
2007-09-17 04:50:12 +00:00
|
|
|
|
2010-09-27 15:15:33 +00:00
|
|
|
def test_multiple_units(self):
|
|
|
|
"""Test multiple units."""
|
2016-04-08 02:04:45 +00:00
|
|
|
self.assertEqual(
|
|
|
|
timesince(self.t, self.t + 2 * self.oneday + 6 * self.onehour),
|
|
|
|
"2\xa0days, 6\xa0hours",
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
timesince(self.t, self.t + 2 * self.oneweek + 2 * self.oneday),
|
|
|
|
"2\xa0weeks, 2\xa0days",
|
|
|
|
)
|
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.
|
|
|
|
"""
|
2016-04-08 02:04:45 +00:00
|
|
|
self.assertEqual(
|
|
|
|
timesince(
|
|
|
|
self.t,
|
|
|
|
self.t + 2 * self.oneweek + 3 * self.onehour + 4 * self.oneminute,
|
|
|
|
),
|
|
|
|
"2\xa0weeks",
|
2022-02-03 19:24:19 +00:00
|
|
|
)
|
2016-04-08 02:04:45 +00:00
|
|
|
self.assertEqual(
|
|
|
|
timesince(self.t, self.t + 4 * self.oneday + 5 * self.oneminute),
|
2022-02-03 19:24:19 +00:00
|
|
|
"4\xa0days",
|
2016-04-08 02:04:45 +00:00
|
|
|
)
|
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.
|
|
|
|
"""
|
2016-04-08 02:04:45 +00:00
|
|
|
self.assertEqual(
|
|
|
|
timesince(self.t, self.t - self.onemicrosecond), "0\xa0minutes"
|
2022-02-03 19:24:19 +00:00
|
|
|
)
|
2016-04-08 02:04:45 +00:00
|
|
|
self.assertEqual(timesince(self.t, self.t - self.onesecond), "0\xa0minutes")
|
|
|
|
self.assertEqual(timesince(self.t, self.t - self.oneminute), "0\xa0minutes")
|
|
|
|
self.assertEqual(timesince(self.t, self.t - self.onehour), "0\xa0minutes")
|
|
|
|
self.assertEqual(timesince(self.t, self.t - self.oneday), "0\xa0minutes")
|
|
|
|
self.assertEqual(timesince(self.t, self.t - self.oneweek), "0\xa0minutes")
|
|
|
|
self.assertEqual(timesince(self.t, self.t - self.onemonth), "0\xa0minutes")
|
|
|
|
self.assertEqual(timesince(self.t, self.t - self.oneyear), "0\xa0minutes")
|
2019-10-28 10:17:29 +00:00
|
|
|
self.assertEqual(
|
2016-04-08 02:04:45 +00:00
|
|
|
timesince(self.t, self.t - 2 * self.oneday - 6 * self.onehour),
|
|
|
|
"0\xa0minutes",
|
2022-02-03 19:24:19 +00:00
|
|
|
)
|
2019-10-28 10:17:29 +00:00
|
|
|
self.assertEqual(
|
2016-04-08 02:04:45 +00:00
|
|
|
timesince(self.t, self.t - 2 * self.oneweek - 2 * self.oneday),
|
2013-05-18 11:58:45 +00:00
|
|
|
"0\xa0minutes",
|
2022-02-03 19:24:19 +00:00
|
|
|
)
|
2019-10-28 10:17:29 +00:00
|
|
|
self.assertEqual(
|
|
|
|
timesince(
|
2022-02-03 19:24:19 +00:00
|
|
|
self.t,
|
2016-04-08 02:04:45 +00:00
|
|
|
self.t - 2 * self.oneweek - 3 * self.onehour - 4 * self.oneminute,
|
2022-02-03 19:24:19 +00:00
|
|
|
),
|
2013-05-18 11:58:45 +00:00
|
|
|
"0\xa0minutes",
|
2022-02-03 19:24:19 +00:00
|
|
|
)
|
2019-10-28 10:17:29 +00:00
|
|
|
self.assertEqual(
|
2016-04-08 02:04:45 +00:00
|
|
|
timesince(self.t, self.t - 4 * self.oneday - 5 * self.oneminute),
|
2013-05-18 11:58:45 +00:00
|
|
|
"0\xa0minutes",
|
2016-04-08 02:04:45 +00:00
|
|
|
)
|
2010-09-27 15:15:33 +00:00
|
|
|
|
2019-10-28 10:17:29 +00:00
|
|
|
def test_second_before_equal_first_humanize_time_strings(self):
|
|
|
|
time_strings = {
|
2021-01-18 18:03:37 +00:00
|
|
|
"minute": npgettext_lazy(
|
|
|
|
"naturaltime-future",
|
|
|
|
"%(num)d minute",
|
|
|
|
"%(num)d minutes",
|
|
|
|
"num",
|
|
|
|
),
|
2019-10-28 10:17:29 +00:00
|
|
|
}
|
|
|
|
with translation.override("cs"):
|
|
|
|
for now in [self.t, self.t - self.onemicrosecond, self.t - self.oneday]:
|
|
|
|
with self.subTest(now):
|
|
|
|
self.assertEqual(
|
|
|
|
timesince(self.t, now, time_strings=time_strings),
|
|
|
|
"0\xa0minut",
|
|
|
|
)
|
|
|
|
|
2013-10-04 19:55:16 +00:00
|
|
|
@requires_tz_support
|
2010-09-27 15:15:33 +00:00
|
|
|
def test_different_timezones(self):
|
|
|
|
"""When using two different timezones."""
|
|
|
|
now = datetime.datetime.now()
|
2013-09-08 08:43:33 +00:00
|
|
|
now_tz = timezone.make_aware(now, timezone.get_default_timezone())
|
|
|
|
now_tz_i = timezone.localtime(now_tz, timezone.get_fixed_timezone(195))
|
2010-09-27 15:15:33 +00:00
|
|
|
|
2013-05-18 11:58:45 +00:00
|
|
|
self.assertEqual(timesince(now), "0\xa0minutes")
|
|
|
|
self.assertEqual(timesince(now_tz), "0\xa0minutes")
|
2013-10-04 19:55:16 +00:00
|
|
|
self.assertEqual(timesince(now_tz_i), "0\xa0minutes")
|
|
|
|
self.assertEqual(timesince(now_tz, now_tz_i), "0\xa0minutes")
|
|
|
|
self.assertEqual(timeuntil(now), "0\xa0minutes")
|
|
|
|
self.assertEqual(timeuntil(now_tz), "0\xa0minutes")
|
|
|
|
self.assertEqual(timeuntil(now_tz_i), "0\xa0minutes")
|
2013-05-18 11:58:45 +00:00
|
|
|
self.assertEqual(timeuntil(now_tz, now_tz_i), "0\xa0minutes")
|
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()
|
2013-05-18 11:58:45 +00:00
|
|
|
self.assertEqual(timesince(today + self.oneday), "0\xa0minutes")
|
|
|
|
self.assertEqual(timeuntil(today - self.oneday), "0\xa0minutes")
|
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()
|
2013-05-18 11:58:45 +00:00
|
|
|
self.assertEqual(timeuntil(today + self.oneday, today), "1\xa0day")
|
|
|
|
self.assertEqual(timeuntil(today - self.oneday, today), "0\xa0minutes")
|
|
|
|
self.assertEqual(timeuntil(today + self.oneweek, today), "1\xa0week")
|
2011-11-18 13:01:06 +00:00
|
|
|
|
2016-12-27 14:29:11 +00:00
|
|
|
def test_leap_year(self):
|
|
|
|
start_date = datetime.date(2016, 12, 25)
|
|
|
|
self.assertEqual(timeuntil(start_date + self.oneweek, start_date), "1\xa0week")
|
|
|
|
self.assertEqual(timesince(start_date, start_date + self.oneweek), "1\xa0week")
|
|
|
|
|
2017-01-02 13:40:44 +00:00
|
|
|
def test_leap_year_new_years_eve(self):
|
|
|
|
t = datetime.date(2016, 12, 31)
|
|
|
|
now = datetime.datetime(2016, 12, 31, 18, 0, 0)
|
|
|
|
self.assertEqual(timesince(t + self.oneday, now), "0\xa0minutes")
|
|
|
|
self.assertEqual(timeuntil(t - self.oneday, now), "0\xa0minutes")
|
|
|
|
|
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
|
2022-02-03 19:24:19 +00:00
|
|
|
|
2011-11-18 13:01:06 +00:00
|
|
|
future = datetime.datetime(2080, 1, 1, tzinfo=naive())
|
2013-05-18 11:58:45 +00:00
|
|
|
self.assertEqual(timesince(future), "0\xa0minutes")
|
2011-11-18 13:01:06 +00:00
|
|
|
past = datetime.datetime(1980, 1, 1, tzinfo=naive())
|
2013-05-18 11:58:45 +00:00
|
|
|
self.assertEqual(timeuntil(past), "0\xa0minutes")
|
2015-06-04 22:46:52 +00:00
|
|
|
|
|
|
|
def test_thousand_years_ago(self):
|
2023-01-05 15:38:19 +00:00
|
|
|
t = self.t.replace(year=self.t.year - 1000)
|
2015-06-04 22:46:52 +00:00
|
|
|
self.assertEqual(timesince(t, self.t), "1000\xa0years")
|
2016-12-27 14:29:11 +00:00
|
|
|
self.assertEqual(timeuntil(self.t, t), "1000\xa0years")
|
2020-07-03 06:01:45 +00:00
|
|
|
|
|
|
|
def test_depth(self):
|
|
|
|
t = (
|
|
|
|
self.t
|
|
|
|
+ self.oneyear
|
|
|
|
+ self.onemonth
|
|
|
|
+ self.oneweek
|
|
|
|
+ self.oneday
|
|
|
|
+ self.onehour
|
2022-02-03 19:24:19 +00:00
|
|
|
)
|
2020-07-03 06:01:45 +00:00
|
|
|
tests = [
|
|
|
|
(t, 1, "1\xa0year"),
|
|
|
|
(t, 2, "1\xa0year, 1\xa0month"),
|
|
|
|
(t, 3, "1\xa0year, 1\xa0month, 1\xa0week"),
|
|
|
|
(t, 4, "1\xa0year, 1\xa0month, 1\xa0week, 1\xa0day"),
|
|
|
|
(t, 5, "1\xa0year, 1\xa0month, 1\xa0week, 1\xa0day, 1\xa0hour"),
|
|
|
|
(t, 6, "1\xa0year, 1\xa0month, 1\xa0week, 1\xa0day, 1\xa0hour"),
|
|
|
|
(self.t + self.onehour, 5, "1\xa0hour"),
|
|
|
|
(self.t + (4 * self.oneminute), 3, "4\xa0minutes"),
|
|
|
|
(self.t + self.onehour + self.oneminute, 1, "1\xa0hour"),
|
|
|
|
(self.t + self.oneday + self.onehour, 1, "1\xa0day"),
|
|
|
|
(self.t + self.oneweek + self.oneday, 1, "1\xa0week"),
|
|
|
|
(self.t + self.onemonth + self.oneweek, 1, "1\xa0month"),
|
|
|
|
(self.t + self.oneyear + self.onemonth, 1, "1\xa0year"),
|
|
|
|
(self.t + self.oneyear + self.oneweek + self.oneday, 3, "1\xa0year"),
|
|
|
|
]
|
|
|
|
for value, depth, expected in tests:
|
|
|
|
with self.subTest():
|
|
|
|
self.assertEqual(timesince(self.t, value, depth=depth), expected)
|
|
|
|
self.assertEqual(timeuntil(value, self.t, depth=depth), expected)
|
|
|
|
|
2023-01-04 10:14:06 +00:00
|
|
|
def test_months_edge(self):
|
|
|
|
t = datetime.datetime(2022, 1, 1)
|
|
|
|
tests = [
|
|
|
|
(datetime.datetime(2022, 1, 31), "4\xa0weeks, 2\xa0days"),
|
|
|
|
(datetime.datetime(2022, 2, 1), "1\xa0month"),
|
|
|
|
(datetime.datetime(2022, 2, 28), "1\xa0month, 3\xa0weeks"),
|
|
|
|
(datetime.datetime(2022, 3, 1), "2\xa0months"),
|
|
|
|
(datetime.datetime(2022, 3, 31), "2\xa0months, 4\xa0weeks"),
|
|
|
|
(datetime.datetime(2022, 4, 1), "3\xa0months"),
|
|
|
|
(datetime.datetime(2022, 4, 30), "3\xa0months, 4\xa0weeks"),
|
|
|
|
(datetime.datetime(2022, 5, 1), "4\xa0months"),
|
|
|
|
(datetime.datetime(2022, 5, 31), "4\xa0months, 4\xa0weeks"),
|
|
|
|
(datetime.datetime(2022, 6, 1), "5\xa0months"),
|
|
|
|
(datetime.datetime(2022, 6, 30), "5\xa0months, 4\xa0weeks"),
|
|
|
|
(datetime.datetime(2022, 7, 1), "6\xa0months"),
|
|
|
|
(datetime.datetime(2022, 7, 31), "6\xa0months, 4\xa0weeks"),
|
|
|
|
(datetime.datetime(2022, 8, 1), "7\xa0months"),
|
|
|
|
(datetime.datetime(2022, 8, 31), "7\xa0months, 4\xa0weeks"),
|
|
|
|
(datetime.datetime(2022, 9, 1), "8\xa0months"),
|
|
|
|
(datetime.datetime(2022, 9, 30), "8\xa0months, 4\xa0weeks"),
|
|
|
|
(datetime.datetime(2022, 10, 1), "9\xa0months"),
|
|
|
|
(datetime.datetime(2022, 10, 31), "9\xa0months, 4\xa0weeks"),
|
|
|
|
(datetime.datetime(2022, 11, 1), "10\xa0months"),
|
|
|
|
(datetime.datetime(2022, 11, 30), "10\xa0months, 4\xa0weeks"),
|
|
|
|
(datetime.datetime(2022, 12, 1), "11\xa0months"),
|
|
|
|
(datetime.datetime(2022, 12, 31), "11\xa0months, 4\xa0weeks"),
|
|
|
|
]
|
|
|
|
for value, expected in tests:
|
|
|
|
with self.subTest():
|
|
|
|
self.assertEqual(timesince(t, value), expected)
|
|
|
|
|
2020-07-03 06:01:45 +00:00
|
|
|
def test_depth_invalid(self):
|
|
|
|
msg = "depth must be greater than 0."
|
|
|
|
with self.assertRaisesMessage(ValueError, msg):
|
|
|
|
timesince(self.t, self.t, depth=0)
|
2023-01-05 15:38:19 +00:00
|
|
|
|
2023-04-13 16:16:33 +00:00
|
|
|
@requires_tz_support
|
|
|
|
def test_less_than_a_day_with_zoneinfo(self):
|
|
|
|
now_with_zoneinfo = timezone.now().astimezone(
|
|
|
|
zoneinfo.ZoneInfo(key="Asia/Kathmandu") # UTC+05:45
|
|
|
|
)
|
|
|
|
tests = [
|
|
|
|
(now_with_zoneinfo, "0\xa0minutes"),
|
|
|
|
(now_with_zoneinfo - self.onemicrosecond, "0\xa0minutes"),
|
|
|
|
(now_with_zoneinfo - self.onesecond, "0\xa0minutes"),
|
|
|
|
(now_with_zoneinfo - self.oneminute, "1\xa0minute"),
|
|
|
|
(now_with_zoneinfo - self.onehour, "1\xa0hour"),
|
|
|
|
]
|
|
|
|
for value, expected in tests:
|
|
|
|
with self.subTest(value):
|
|
|
|
self.assertEqual(timesince(value), expected)
|
|
|
|
|
2023-01-05 15:38:19 +00:00
|
|
|
|
|
|
|
@requires_tz_support
|
|
|
|
@override_settings(USE_TZ=True)
|
|
|
|
class TZAwareTimesinceTests(TimesinceTests):
|
|
|
|
def setUp(self):
|
|
|
|
super().setUp()
|
|
|
|
self.t = timezone.make_aware(self.t, timezone.get_default_timezone())
|