2022-03-23 19:15:36 +08:00
|
|
|
from datetime import date, datetime, time, timezone, tzinfo
|
2010-09-28 16:18:12 +08:00
|
|
|
|
2015-04-18 05:38:20 +08:00
|
|
|
from django.test import SimpleTestCase, override_settings
|
2014-01-12 16:41:09 +08:00
|
|
|
from django.test.utils import TZ_SUPPORT, requires_tz_support
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.utils import dateformat, translation
|
2009-11-01 12:53:51 +08:00
|
|
|
from django.utils.dateformat import format
|
2022-03-23 19:15:36 +08:00
|
|
|
from django.utils.timezone import get_default_timezone, get_fixed_timezone, make_aware
|
2009-05-08 21:39:37 +08:00
|
|
|
|
2010-10-11 20:55:17 +08:00
|
|
|
|
2013-09-08 16:43:33 +08:00
|
|
|
@override_settings(TIME_ZONE="Europe/Copenhagen")
|
2015-04-18 05:38:20 +08:00
|
|
|
class DateFormatTests(SimpleTestCase):
|
2010-09-28 16:19:42 +08:00
|
|
|
def setUp(self):
|
2013-02-28 05:48:17 +08:00
|
|
|
self._orig_lang = translation.get_language()
|
2010-09-28 16:19:42 +08:00
|
|
|
translation.activate("en-us")
|
|
|
|
|
|
|
|
def tearDown(self):
|
2013-02-28 05:48:17 +08:00
|
|
|
translation.activate(self._orig_lang)
|
2010-09-28 16:19:42 +08:00
|
|
|
|
2009-11-01 12:53:51 +08:00
|
|
|
def test_date(self):
|
|
|
|
d = date(2009, 5, 16)
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(date.fromtimestamp(int(format(d, "U"))), d)
|
2009-05-08 21:39:37 +08:00
|
|
|
|
2009-11-01 12:53:51 +08:00
|
|
|
def test_naive_datetime(self):
|
|
|
|
dt = datetime(2009, 5, 16, 5, 30, 30)
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(datetime.fromtimestamp(int(format(dt, "U"))), dt)
|
2009-05-08 21:39:37 +08:00
|
|
|
|
2015-11-07 23:12:13 +08:00
|
|
|
def test_naive_ambiguous_datetime(self):
|
2016-10-08 09:06:49 +08:00
|
|
|
# dt is ambiguous in Europe/Copenhagen. pytz raises an exception for
|
|
|
|
# the ambiguity, which results in an empty string.
|
2015-11-07 23:12:13 +08:00
|
|
|
dt = datetime(2015, 10, 25, 2, 30, 0)
|
|
|
|
|
|
|
|
# Try all formatters that involve self.timezone.
|
2016-10-08 09:06:49 +08:00
|
|
|
self.assertEqual(format(dt, "I"), "")
|
|
|
|
self.assertEqual(format(dt, "O"), "")
|
|
|
|
self.assertEqual(format(dt, "T"), "")
|
|
|
|
self.assertEqual(format(dt, "Z"), "")
|
2015-11-07 23:12:13 +08:00
|
|
|
|
2014-01-12 16:41:09 +08:00
|
|
|
@requires_tz_support
|
2009-11-01 12:53:51 +08:00
|
|
|
def test_datetime_with_local_tzinfo(self):
|
2013-09-08 16:43:33 +08:00
|
|
|
ltz = get_default_timezone()
|
2013-09-11 00:06:43 +08:00
|
|
|
dt = make_aware(datetime(2009, 5, 16, 5, 30, 30), ltz)
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(datetime.fromtimestamp(int(format(dt, "U")), ltz), dt)
|
|
|
|
self.assertEqual(
|
|
|
|
datetime.fromtimestamp(int(format(dt, "U"))), dt.replace(tzinfo=None)
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2009-05-08 21:39:37 +08:00
|
|
|
|
2014-01-12 16:41:09 +08:00
|
|
|
@requires_tz_support
|
2009-11-01 12:53:51 +08:00
|
|
|
def test_datetime_with_tzinfo(self):
|
2013-09-08 16:43:33 +08:00
|
|
|
tz = get_fixed_timezone(-510)
|
|
|
|
ltz = get_default_timezone()
|
2013-09-11 00:06:43 +08:00
|
|
|
dt = make_aware(datetime(2009, 5, 16, 5, 30, 30), ltz)
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(datetime.fromtimestamp(int(format(dt, "U")), tz), dt)
|
|
|
|
self.assertEqual(datetime.fromtimestamp(int(format(dt, "U")), ltz), dt)
|
2013-09-11 00:06:43 +08:00
|
|
|
# astimezone() is safe here because the target timezone doesn't have DST
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(
|
|
|
|
datetime.fromtimestamp(int(format(dt, "U"))),
|
|
|
|
dt.astimezone(ltz).replace(tzinfo=None),
|
|
|
|
)
|
2021-05-07 17:42:59 +08:00
|
|
|
self.assertEqual(
|
|
|
|
datetime.fromtimestamp(int(format(dt, "U")), tz).timetuple(),
|
|
|
|
dt.astimezone(tz).timetuple(),
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
datetime.fromtimestamp(int(format(dt, "U")), ltz).timetuple(),
|
|
|
|
dt.astimezone(ltz).timetuple(),
|
|
|
|
)
|
2009-11-01 12:53:51 +08:00
|
|
|
|
|
|
|
def test_epoch(self):
|
2022-03-23 19:15:36 +08:00
|
|
|
udt = datetime(1970, 1, 1, tzinfo=timezone.utc)
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(format(udt, "U"), "0")
|
2010-09-28 16:19:42 +08:00
|
|
|
|
|
|
|
def test_empty_format(self):
|
|
|
|
my_birthday = datetime(1979, 7, 8, 22, 00)
|
|
|
|
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(dateformat.format(my_birthday, ""), "")
|
2010-09-28 16:19:42 +08:00
|
|
|
|
|
|
|
def test_am_pm(self):
|
2021-07-13 19:19:52 +08:00
|
|
|
morning = time(7, 00)
|
|
|
|
evening = time(19, 00)
|
|
|
|
self.assertEqual(dateformat.format(morning, "a"), "a.m.")
|
|
|
|
self.assertEqual(dateformat.format(evening, "a"), "p.m.")
|
|
|
|
self.assertEqual(dateformat.format(morning, "A"), "AM")
|
|
|
|
self.assertEqual(dateformat.format(evening, "A"), "PM")
|
2010-09-28 16:19:42 +08:00
|
|
|
|
2012-09-22 18:02:21 +08:00
|
|
|
def test_microsecond(self):
|
|
|
|
# Regression test for #18951
|
|
|
|
dt = datetime(2009, 5, 16, microsecond=123)
|
|
|
|
self.assertEqual(dateformat.format(dt, "u"), "000123")
|
|
|
|
|
2010-09-28 16:19:42 +08:00
|
|
|
def test_date_formats(self):
|
2021-07-13 19:19:52 +08:00
|
|
|
# Specifiers 'I', 'r', and 'U' are covered in test_timezones().
|
2010-09-28 16:19:42 +08:00
|
|
|
my_birthday = datetime(1979, 7, 8, 22, 00)
|
2021-05-19 17:53:40 +08:00
|
|
|
for specifier, expected in [
|
|
|
|
("b", "jul"),
|
|
|
|
("d", "08"),
|
2021-07-13 19:19:52 +08:00
|
|
|
("D", "Sun"),
|
|
|
|
("E", "July"),
|
|
|
|
("F", "July"),
|
2021-05-19 17:53:40 +08:00
|
|
|
("j", "8"),
|
|
|
|
("l", "Sunday"),
|
|
|
|
("L", "False"),
|
|
|
|
("m", "07"),
|
|
|
|
("M", "Jul"),
|
|
|
|
("n", "7"),
|
|
|
|
("N", "July"),
|
2021-07-13 19:19:52 +08:00
|
|
|
("o", "1979"),
|
2021-05-19 17:53:40 +08:00
|
|
|
("S", "th"),
|
|
|
|
("t", "31"),
|
|
|
|
("w", "0"),
|
|
|
|
("W", "27"),
|
|
|
|
("y", "79"),
|
|
|
|
("Y", "1979"),
|
|
|
|
("z", "189"),
|
|
|
|
]:
|
|
|
|
with self.subTest(specifier=specifier):
|
|
|
|
self.assertEqual(dateformat.format(my_birthday, specifier), expected)
|
|
|
|
|
|
|
|
def test_date_formats_c_format(self):
|
2010-09-28 16:19:42 +08:00
|
|
|
timestamp = datetime(2008, 5, 19, 11, 45, 23, 123456)
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(
|
|
|
|
dateformat.format(timestamp, "c"), "2008-05-19T11:45:23.123456"
|
|
|
|
)
|
2010-09-28 16:19:42 +08:00
|
|
|
|
|
|
|
def test_time_formats(self):
|
2021-07-13 19:19:52 +08:00
|
|
|
# Specifiers 'I', 'r', and 'U' are covered in test_timezones().
|
2010-09-28 16:19:42 +08:00
|
|
|
my_birthday = datetime(1979, 7, 8, 22, 00)
|
2021-05-19 17:53:40 +08:00
|
|
|
for specifier, expected in [
|
|
|
|
("a", "p.m."),
|
|
|
|
("A", "PM"),
|
2021-07-13 19:19:52 +08:00
|
|
|
("f", "10"),
|
|
|
|
("g", "10"),
|
|
|
|
("G", "22"),
|
|
|
|
("h", "10"),
|
|
|
|
("H", "22"),
|
|
|
|
("i", "00"),
|
2021-05-19 17:53:40 +08:00
|
|
|
("P", "10 p.m."),
|
|
|
|
("s", "00"),
|
|
|
|
("u", "000000"),
|
|
|
|
]:
|
|
|
|
with self.subTest(specifier=specifier):
|
|
|
|
self.assertEqual(dateformat.format(my_birthday, specifier), expected)
|
2010-09-28 16:19:42 +08:00
|
|
|
|
|
|
|
def test_dateformat(self):
|
|
|
|
my_birthday = datetime(1979, 7, 8, 22, 00)
|
|
|
|
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(dateformat.format(my_birthday, r"Y z \C\E\T"), "1979 189 CET")
|
2010-09-28 16:19:42 +08:00
|
|
|
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(dateformat.format(my_birthday, r"jS \o\f F"), "8th of July")
|
2010-09-28 16:19:42 +08:00
|
|
|
|
|
|
|
def test_futuredates(self):
|
|
|
|
the_future = datetime(2100, 10, 25, 0, 00)
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(dateformat.format(the_future, r"Y"), "2100")
|
2010-09-28 16:19:42 +08:00
|
|
|
|
2019-11-18 18:03:54 +08:00
|
|
|
def test_day_of_year_leap(self):
|
|
|
|
self.assertEqual(dateformat.format(datetime(2000, 12, 31), "z"), "366")
|
|
|
|
|
2010-09-28 16:19:42 +08:00
|
|
|
def test_timezones(self):
|
|
|
|
my_birthday = datetime(1979, 7, 8, 22, 00)
|
|
|
|
summertime = datetime(2005, 10, 30, 1, 00)
|
|
|
|
wintertime = datetime(2005, 10, 30, 4, 00)
|
2021-07-13 19:19:52 +08:00
|
|
|
noon = time(12, 0, 0)
|
2010-09-28 16:19:42 +08:00
|
|
|
|
2013-07-03 06:19:56 +08:00
|
|
|
# 3h30m to the west of UTC
|
2013-09-08 16:43:33 +08:00
|
|
|
tz = get_fixed_timezone(-210)
|
2013-07-03 06:19:56 +08:00
|
|
|
aware_dt = datetime(2009, 5, 16, 5, 30, 30, tzinfo=tz)
|
|
|
|
|
2014-01-12 16:41:09 +08:00
|
|
|
if TZ_SUPPORT:
|
2021-05-19 17:53:40 +08:00
|
|
|
for specifier, expected in [
|
|
|
|
("e", ""),
|
|
|
|
("O", "+0100"),
|
|
|
|
("r", "Sun, 08 Jul 1979 22:00:00 +0100"),
|
|
|
|
("T", "CET"),
|
|
|
|
("U", "300315600"),
|
|
|
|
("Z", "3600"),
|
|
|
|
]:
|
|
|
|
with self.subTest(specifier=specifier):
|
|
|
|
self.assertEqual(
|
|
|
|
dateformat.format(my_birthday, specifier), expected
|
|
|
|
)
|
|
|
|
|
2013-07-03 06:19:56 +08:00
|
|
|
self.assertEqual(dateformat.format(aware_dt, "e"), "-0330")
|
2021-07-13 19:19:52 +08:00
|
|
|
self.assertEqual(
|
|
|
|
dateformat.format(aware_dt, "r"),
|
|
|
|
"Sat, 16 May 2009 05:30:30 -0330",
|
|
|
|
)
|
|
|
|
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(dateformat.format(summertime, "I"), "1")
|
|
|
|
self.assertEqual(dateformat.format(summertime, "O"), "+0200")
|
2021-07-13 19:19:52 +08:00
|
|
|
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(dateformat.format(wintertime, "I"), "0")
|
|
|
|
self.assertEqual(dateformat.format(wintertime, "O"), "+0100")
|
2011-09-26 01:08:31 +08:00
|
|
|
|
2021-07-13 19:19:52 +08:00
|
|
|
for specifier in ["e", "O", "T", "Z"]:
|
|
|
|
with self.subTest(specifier=specifier):
|
|
|
|
self.assertEqual(dateformat.time_format(noon, specifier), "")
|
|
|
|
|
2011-09-26 01:08:31 +08:00
|
|
|
# Ticket #16924 -- We don't need timezone support to test this
|
2013-07-03 06:19:56 +08:00
|
|
|
self.assertEqual(dateformat.format(aware_dt, "O"), "-0330")
|
2016-04-04 01:31:35 +08:00
|
|
|
|
|
|
|
def test_invalid_time_format_specifiers(self):
|
|
|
|
my_birthday = date(1984, 8, 7)
|
|
|
|
|
2019-11-22 16:40:49 +08:00
|
|
|
for specifier in ["a", "A", "f", "g", "G", "h", "H", "i", "P", "r", "s", "u"]:
|
2021-05-19 17:53:40 +08:00
|
|
|
with self.subTest(specifier=specifier):
|
|
|
|
msg = (
|
|
|
|
"The format for date objects may not contain time-related "
|
|
|
|
f"format specifiers (found {specifier!r})."
|
|
|
|
)
|
|
|
|
with self.assertRaisesMessage(TypeError, msg):
|
|
|
|
dateformat.format(my_birthday, specifier)
|
2019-11-22 03:34:58 +08:00
|
|
|
|
2021-07-13 19:19:52 +08:00
|
|
|
@requires_tz_support
|
|
|
|
def test_e_format_with_named_time_zone(self):
|
2022-03-23 19:15:36 +08:00
|
|
|
dt = datetime(1970, 1, 1, tzinfo=timezone.utc)
|
2021-07-13 19:19:52 +08:00
|
|
|
self.assertEqual(dateformat.format(dt, "e"), "UTC")
|
|
|
|
|
|
|
|
@requires_tz_support
|
|
|
|
def test_e_format_with_time_zone_with_unimplemented_tzname(self):
|
|
|
|
class NoNameTZ(tzinfo):
|
|
|
|
"""Time zone without .tzname() defined."""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2021-07-13 19:19:52 +08:00
|
|
|
def utcoffset(self, dt):
|
|
|
|
return None
|
|
|
|
|
|
|
|
dt = datetime(1970, 1, 1, tzinfo=NoNameTZ())
|
|
|
|
self.assertEqual(dateformat.format(dt, "e"), "")
|
|
|
|
|
|
|
|
def test_P_format(self):
|
|
|
|
for expected, t in [
|
|
|
|
("midnight", time(0)),
|
|
|
|
("noon", time(12)),
|
|
|
|
("4 a.m.", time(4)),
|
|
|
|
("8:30 a.m.", time(8, 30)),
|
|
|
|
("4 p.m.", time(16)),
|
|
|
|
("8:30 p.m.", time(20, 30)),
|
|
|
|
]:
|
|
|
|
with self.subTest(time=t):
|
|
|
|
self.assertEqual(dateformat.time_format(t, "P"), expected)
|
|
|
|
|
2019-11-22 03:34:58 +08:00
|
|
|
def test_r_format_with_non_en_locale(self):
|
|
|
|
# Changing the locale doesn't change the "r" format.
|
|
|
|
dt = datetime(1979, 7, 8, 22, 00)
|
|
|
|
with translation.override("fr"):
|
|
|
|
self.assertEqual(
|
|
|
|
dateformat.format(dt, "r"),
|
|
|
|
"Sun, 08 Jul 1979 22:00:00 +0100",
|
|
|
|
)
|
2020-10-27 12:12:14 +08:00
|
|
|
|
2021-07-13 19:19:52 +08:00
|
|
|
def test_S_format(self):
|
|
|
|
for expected, days in [
|
|
|
|
("st", [1, 21, 31]),
|
|
|
|
("nd", [2, 22]),
|
|
|
|
("rd", [3, 23]),
|
|
|
|
("th", (n for n in range(4, 31) if n not in [21, 22, 23])),
|
|
|
|
]:
|
|
|
|
for day in days:
|
|
|
|
dt = date(1970, 1, day)
|
|
|
|
with self.subTest(day=day):
|
|
|
|
self.assertEqual(dateformat.format(dt, "S"), expected)
|
|
|
|
|
2021-05-10 22:33:48 +08:00
|
|
|
def test_y_format_year_before_1000(self):
|
2020-10-27 12:12:14 +08:00
|
|
|
tests = [
|
|
|
|
(476, "76"),
|
|
|
|
(42, "42"),
|
|
|
|
(4, "04"),
|
|
|
|
]
|
|
|
|
for year, expected_date in tests:
|
|
|
|
with self.subTest(year=year):
|
|
|
|
self.assertEqual(
|
|
|
|
dateformat.format(datetime(year, 9, 8, 5, 0), "y"),
|
|
|
|
expected_date,
|
|
|
|
)
|
2020-11-12 22:19:17 +08:00
|
|
|
|
2021-05-10 22:33:48 +08:00
|
|
|
def test_Y_format_year_before_1000(self):
|
|
|
|
self.assertEqual(dateformat.format(datetime(1, 1, 1), "Y"), "0001")
|
|
|
|
self.assertEqual(dateformat.format(datetime(999, 1, 1), "Y"), "0999")
|
|
|
|
|
2020-11-12 22:19:17 +08:00
|
|
|
def test_twelve_hour_format(self):
|
|
|
|
tests = [
|
2021-07-13 19:19:52 +08:00
|
|
|
(0, "12", "12"),
|
|
|
|
(1, "1", "01"),
|
|
|
|
(11, "11", "11"),
|
|
|
|
(12, "12", "12"),
|
|
|
|
(13, "1", "01"),
|
|
|
|
(23, "11", "11"),
|
2020-11-12 22:19:17 +08:00
|
|
|
]
|
2021-07-13 19:19:52 +08:00
|
|
|
for hour, g_expected, h_expected in tests:
|
|
|
|
dt = datetime(2000, 1, 1, hour)
|
2020-11-12 22:19:17 +08:00
|
|
|
with self.subTest(hour=hour):
|
2021-07-13 19:19:52 +08:00
|
|
|
self.assertEqual(dateformat.format(dt, "g"), g_expected)
|
|
|
|
self.assertEqual(dateformat.format(dt, "h"), h_expected)
|