2015-01-28 12:35:27 +00:00
|
|
|
from datetime import date, datetime
|
2010-09-28 08:18:12 +00:00
|
|
|
|
2015-04-17 21:38:20 +00:00
|
|
|
from django.test import SimpleTestCase, override_settings
|
2014-01-12 08:41:09 +00:00
|
|
|
from django.test.utils import TZ_SUPPORT, requires_tz_support
|
2015-01-28 12:35:27 +00:00
|
|
|
from django.utils import dateformat, translation
|
2009-11-01 04:53:51 +00:00
|
|
|
from django.utils.dateformat import format
|
2015-01-28 12:35:27 +00:00
|
|
|
from django.utils.timezone import (
|
|
|
|
get_default_timezone, get_fixed_timezone, make_aware, utc,
|
|
|
|
)
|
2009-05-08 13:39:37 +00:00
|
|
|
|
2010-10-11 12:55:17 +00:00
|
|
|
|
2013-09-08 08:43:33 +00:00
|
|
|
@override_settings(TIME_ZONE='Europe/Copenhagen')
|
2015-04-17 21:38:20 +00:00
|
|
|
class DateFormatTests(SimpleTestCase):
|
2013-09-08 08:43:33 +00:00
|
|
|
|
2010-09-28 08:19:42 +00:00
|
|
|
def setUp(self):
|
2013-02-27 21:48:17 +00:00
|
|
|
self._orig_lang = translation.get_language()
|
2010-09-28 08:19:42 +00:00
|
|
|
translation.activate('en-us')
|
|
|
|
|
|
|
|
def tearDown(self):
|
2013-02-27 21:48:17 +00:00
|
|
|
translation.activate(self._orig_lang)
|
2010-09-28 08:19:42 +00:00
|
|
|
|
2009-11-01 04:53:51 +00:00
|
|
|
def test_date(self):
|
|
|
|
d = date(2009, 5, 16)
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(date.fromtimestamp(int(format(d, 'U'))), d)
|
2009-05-08 13:39:37 +00:00
|
|
|
|
2009-11-01 04:53:51 +00:00
|
|
|
def test_naive_datetime(self):
|
|
|
|
dt = datetime(2009, 5, 16, 5, 30, 30)
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(datetime.fromtimestamp(int(format(dt, 'U'))), dt)
|
2009-05-08 13:39:37 +00:00
|
|
|
|
2015-11-07 15:12:13 +00:00
|
|
|
def test_naive_ambiguous_datetime(self):
|
2016-10-08 01:06:49 +00:00
|
|
|
# dt is ambiguous in Europe/Copenhagen. pytz raises an exception for
|
|
|
|
# the ambiguity, which results in an empty string.
|
2015-11-07 15:12:13 +00:00
|
|
|
dt = datetime(2015, 10, 25, 2, 30, 0)
|
|
|
|
|
|
|
|
# Try all formatters that involve self.timezone.
|
2016-10-08 01:06:49 +00:00
|
|
|
self.assertEqual(format(dt, 'I'), '')
|
|
|
|
self.assertEqual(format(dt, 'O'), '')
|
|
|
|
self.assertEqual(format(dt, 'T'), '')
|
|
|
|
self.assertEqual(format(dt, 'Z'), '')
|
2015-11-07 15:12:13 +00:00
|
|
|
|
2014-01-12 08:41:09 +00:00
|
|
|
@requires_tz_support
|
2009-11-01 04:53:51 +00:00
|
|
|
def test_datetime_with_local_tzinfo(self):
|
2013-09-08 08:43:33 +00:00
|
|
|
ltz = get_default_timezone()
|
2013-09-10 16:06:43 +00:00
|
|
|
dt = make_aware(datetime(2009, 5, 16, 5, 30, 30), ltz)
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(datetime.fromtimestamp(int(format(dt, 'U')), ltz), dt)
|
|
|
|
self.assertEqual(datetime.fromtimestamp(int(format(dt, 'U'))), dt.replace(tzinfo=None))
|
2009-05-08 13:39:37 +00:00
|
|
|
|
2014-01-12 08:41:09 +00:00
|
|
|
@requires_tz_support
|
2009-11-01 04:53:51 +00:00
|
|
|
def test_datetime_with_tzinfo(self):
|
2013-09-08 08:43:33 +00:00
|
|
|
tz = get_fixed_timezone(-510)
|
|
|
|
ltz = get_default_timezone()
|
2013-09-10 16:06:43 +00:00
|
|
|
dt = make_aware(datetime(2009, 5, 16, 5, 30, 30), ltz)
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(datetime.fromtimestamp(int(format(dt, 'U')), tz), dt)
|
|
|
|
self.assertEqual(datetime.fromtimestamp(int(format(dt, 'U')), ltz), dt)
|
2013-09-10 16:06:43 +00:00
|
|
|
# astimezone() is safe here because the target timezone doesn't have DST
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(datetime.fromtimestamp(int(format(dt, 'U'))), dt.astimezone(ltz).replace(tzinfo=None))
|
|
|
|
self.assertEqual(datetime.fromtimestamp(int(format(dt, 'U')), tz).utctimetuple(), dt.utctimetuple())
|
|
|
|
self.assertEqual(datetime.fromtimestamp(int(format(dt, 'U')), ltz).utctimetuple(), dt.utctimetuple())
|
2009-11-01 04:53:51 +00:00
|
|
|
|
|
|
|
def test_epoch(self):
|
|
|
|
udt = datetime(1970, 1, 1, tzinfo=utc)
|
2012-06-07 16:08:47 +00:00
|
|
|
self.assertEqual(format(udt, 'U'), '0')
|
2010-09-28 08:19:42 +00:00
|
|
|
|
|
|
|
def test_empty_format(self):
|
|
|
|
my_birthday = datetime(1979, 7, 8, 22, 00)
|
|
|
|
|
2012-06-07 16:08:47 +00:00
|
|
|
self.assertEqual(dateformat.format(my_birthday, ''), '')
|
2010-09-28 08:19:42 +00:00
|
|
|
|
|
|
|
def test_am_pm(self):
|
|
|
|
my_birthday = datetime(1979, 7, 8, 22, 00)
|
|
|
|
|
2012-06-07 16:08:47 +00:00
|
|
|
self.assertEqual(dateformat.format(my_birthday, 'a'), 'p.m.')
|
2010-09-28 08:19:42 +00:00
|
|
|
|
2012-09-22 10:02:21 +00: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 08:19:42 +00:00
|
|
|
def test_date_formats(self):
|
|
|
|
my_birthday = datetime(1979, 7, 8, 22, 00)
|
|
|
|
timestamp = datetime(2008, 5, 19, 11, 45, 23, 123456)
|
|
|
|
|
2012-06-07 16:08:47 +00:00
|
|
|
self.assertEqual(dateformat.format(my_birthday, 'A'), 'PM')
|
|
|
|
self.assertEqual(dateformat.format(timestamp, 'c'), '2008-05-19T11:45:23.123456')
|
|
|
|
self.assertEqual(dateformat.format(my_birthday, 'd'), '08')
|
|
|
|
self.assertEqual(dateformat.format(my_birthday, 'j'), '8')
|
|
|
|
self.assertEqual(dateformat.format(my_birthday, 'l'), 'Sunday')
|
|
|
|
self.assertEqual(dateformat.format(my_birthday, 'L'), 'False')
|
|
|
|
self.assertEqual(dateformat.format(my_birthday, 'm'), '07')
|
|
|
|
self.assertEqual(dateformat.format(my_birthday, 'M'), 'Jul')
|
|
|
|
self.assertEqual(dateformat.format(my_birthday, 'b'), 'jul')
|
|
|
|
self.assertEqual(dateformat.format(my_birthday, 'n'), '7')
|
|
|
|
self.assertEqual(dateformat.format(my_birthday, 'N'), 'July')
|
2010-09-28 08:19:42 +00:00
|
|
|
|
|
|
|
def test_time_formats(self):
|
|
|
|
my_birthday = datetime(1979, 7, 8, 22, 00)
|
|
|
|
|
2012-06-07 16:08:47 +00:00
|
|
|
self.assertEqual(dateformat.format(my_birthday, 'P'), '10 p.m.')
|
|
|
|
self.assertEqual(dateformat.format(my_birthday, 's'), '00')
|
|
|
|
self.assertEqual(dateformat.format(my_birthday, 'S'), 'th')
|
|
|
|
self.assertEqual(dateformat.format(my_birthday, 't'), '31')
|
|
|
|
self.assertEqual(dateformat.format(my_birthday, 'w'), '0')
|
|
|
|
self.assertEqual(dateformat.format(my_birthday, 'W'), '27')
|
|
|
|
self.assertEqual(dateformat.format(my_birthday, 'y'), '79')
|
|
|
|
self.assertEqual(dateformat.format(my_birthday, 'Y'), '1979')
|
|
|
|
self.assertEqual(dateformat.format(my_birthday, 'z'), '189')
|
2010-09-28 08:19:42 +00:00
|
|
|
|
|
|
|
def test_dateformat(self):
|
|
|
|
my_birthday = datetime(1979, 7, 8, 22, 00)
|
|
|
|
|
2012-06-07 16:08:47 +00:00
|
|
|
self.assertEqual(dateformat.format(my_birthday, r'Y z \C\E\T'), '1979 189 CET')
|
2010-09-28 08:19:42 +00:00
|
|
|
|
2012-06-07 16:08:47 +00:00
|
|
|
self.assertEqual(dateformat.format(my_birthday, r'jS \o\f F'), '8th of July')
|
2010-09-28 08:19:42 +00:00
|
|
|
|
|
|
|
def test_futuredates(self):
|
|
|
|
the_future = datetime(2100, 10, 25, 0, 00)
|
2012-06-07 16:08:47 +00:00
|
|
|
self.assertEqual(dateformat.format(the_future, r'Y'), '2100')
|
2010-09-28 08:19:42 +00:00
|
|
|
|
2019-11-18 10:03:54 +00:00
|
|
|
def test_day_of_year_leap(self):
|
|
|
|
self.assertEqual(dateformat.format(datetime(2000, 12, 31), 'z'), '366')
|
|
|
|
|
2010-09-28 08:19:42 +00: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)
|
|
|
|
timestamp = datetime(2008, 5, 19, 11, 45, 23, 123456)
|
|
|
|
|
2013-07-02 22:19:56 +00:00
|
|
|
# 3h30m to the west of UTC
|
2013-09-08 08:43:33 +00:00
|
|
|
tz = get_fixed_timezone(-210)
|
2013-07-02 22:19:56 +00:00
|
|
|
aware_dt = datetime(2009, 5, 16, 5, 30, 30, tzinfo=tz)
|
|
|
|
|
2014-01-12 08:41:09 +00:00
|
|
|
if TZ_SUPPORT:
|
2012-06-07 16:08:47 +00:00
|
|
|
self.assertEqual(dateformat.format(my_birthday, 'O'), '+0100')
|
2019-11-21 19:34:58 +00:00
|
|
|
self.assertEqual(dateformat.format(my_birthday, 'r'), 'Sun, 08 Jul 1979 22:00:00 +0100')
|
2012-06-07 16:08:47 +00:00
|
|
|
self.assertEqual(dateformat.format(my_birthday, 'T'), 'CET')
|
2013-07-02 22:19:56 +00:00
|
|
|
self.assertEqual(dateformat.format(my_birthday, 'e'), '')
|
|
|
|
self.assertEqual(dateformat.format(aware_dt, 'e'), '-0330')
|
2012-06-07 16:08:47 +00:00
|
|
|
self.assertEqual(dateformat.format(my_birthday, 'U'), '300315600')
|
|
|
|
self.assertEqual(dateformat.format(timestamp, 'u'), '123456')
|
|
|
|
self.assertEqual(dateformat.format(my_birthday, 'Z'), '3600')
|
|
|
|
self.assertEqual(dateformat.format(summertime, 'I'), '1')
|
|
|
|
self.assertEqual(dateformat.format(summertime, 'O'), '+0200')
|
|
|
|
self.assertEqual(dateformat.format(wintertime, 'I'), '0')
|
|
|
|
self.assertEqual(dateformat.format(wintertime, 'O'), '+0100')
|
2011-09-25 17:08:31 +00:00
|
|
|
|
|
|
|
# Ticket #16924 -- We don't need timezone support to test this
|
2013-07-02 22:19:56 +00:00
|
|
|
self.assertEqual(dateformat.format(aware_dt, 'O'), '-0330')
|
2016-04-03 17:31:35 +00:00
|
|
|
|
|
|
|
def test_invalid_time_format_specifiers(self):
|
|
|
|
my_birthday = date(1984, 8, 7)
|
|
|
|
|
2019-11-22 08:40:49 +00:00
|
|
|
for specifier in ['a', 'A', 'f', 'g', 'G', 'h', 'H', 'i', 'P', 'r', 's', 'u']:
|
2016-04-03 17:31:35 +00:00
|
|
|
msg = (
|
|
|
|
"The format for date objects may not contain time-related "
|
|
|
|
"format specifiers (found '%s')." % specifier
|
|
|
|
)
|
|
|
|
with self.assertRaisesMessage(TypeError, msg):
|
|
|
|
dateformat.format(my_birthday, specifier)
|
2019-11-21 19:34:58 +00: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',
|
|
|
|
)
|