2
0

test_dateformat.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. from datetime import date, datetime, time, timezone, tzinfo
  2. from django.test import SimpleTestCase, override_settings
  3. from django.test.utils import TZ_SUPPORT, requires_tz_support
  4. from django.utils import dateformat, translation
  5. from django.utils.dateformat import format
  6. from django.utils.timezone import get_default_timezone, get_fixed_timezone, make_aware
  7. @override_settings(TIME_ZONE="Europe/Copenhagen")
  8. class DateFormatTests(SimpleTestCase):
  9. def setUp(self):
  10. self._orig_lang = translation.get_language()
  11. translation.activate("en-us")
  12. def tearDown(self):
  13. translation.activate(self._orig_lang)
  14. def test_date(self):
  15. d = date(2009, 5, 16)
  16. self.assertEqual(date.fromtimestamp(int(format(d, "U"))), d)
  17. def test_naive_datetime(self):
  18. dt = datetime(2009, 5, 16, 5, 30, 30)
  19. self.assertEqual(datetime.fromtimestamp(int(format(dt, "U"))), dt)
  20. def test_naive_ambiguous_datetime(self):
  21. # dt is ambiguous in Europe/Copenhagen. pytz raises an exception for
  22. # the ambiguity, which results in an empty string.
  23. dt = datetime(2015, 10, 25, 2, 30, 0)
  24. # Try all formatters that involve self.timezone.
  25. self.assertEqual(format(dt, "I"), "")
  26. self.assertEqual(format(dt, "O"), "")
  27. self.assertEqual(format(dt, "T"), "")
  28. self.assertEqual(format(dt, "Z"), "")
  29. @requires_tz_support
  30. def test_datetime_with_local_tzinfo(self):
  31. ltz = get_default_timezone()
  32. dt = make_aware(datetime(2009, 5, 16, 5, 30, 30), ltz)
  33. self.assertEqual(datetime.fromtimestamp(int(format(dt, "U")), ltz), dt)
  34. self.assertEqual(
  35. datetime.fromtimestamp(int(format(dt, "U"))), dt.replace(tzinfo=None)
  36. )
  37. @requires_tz_support
  38. def test_datetime_with_tzinfo(self):
  39. tz = get_fixed_timezone(-510)
  40. ltz = get_default_timezone()
  41. dt = make_aware(datetime(2009, 5, 16, 5, 30, 30), ltz)
  42. self.assertEqual(datetime.fromtimestamp(int(format(dt, "U")), tz), dt)
  43. self.assertEqual(datetime.fromtimestamp(int(format(dt, "U")), ltz), dt)
  44. # astimezone() is safe here because the target timezone doesn't have DST
  45. self.assertEqual(
  46. datetime.fromtimestamp(int(format(dt, "U"))),
  47. dt.astimezone(ltz).replace(tzinfo=None),
  48. )
  49. self.assertEqual(
  50. datetime.fromtimestamp(int(format(dt, "U")), tz).timetuple(),
  51. dt.astimezone(tz).timetuple(),
  52. )
  53. self.assertEqual(
  54. datetime.fromtimestamp(int(format(dt, "U")), ltz).timetuple(),
  55. dt.astimezone(ltz).timetuple(),
  56. )
  57. def test_epoch(self):
  58. udt = datetime(1970, 1, 1, tzinfo=timezone.utc)
  59. self.assertEqual(format(udt, "U"), "0")
  60. def test_empty_format(self):
  61. my_birthday = datetime(1979, 7, 8, 22, 00)
  62. self.assertEqual(dateformat.format(my_birthday, ""), "")
  63. def test_am_pm(self):
  64. morning = time(7, 00)
  65. evening = time(19, 00)
  66. self.assertEqual(dateformat.format(morning, "a"), "a.m.")
  67. self.assertEqual(dateformat.format(evening, "a"), "p.m.")
  68. self.assertEqual(dateformat.format(morning, "A"), "AM")
  69. self.assertEqual(dateformat.format(evening, "A"), "PM")
  70. def test_microsecond(self):
  71. # Regression test for #18951
  72. dt = datetime(2009, 5, 16, microsecond=123)
  73. self.assertEqual(dateformat.format(dt, "u"), "000123")
  74. def test_date_formats(self):
  75. # Specifiers 'I', 'r', and 'U' are covered in test_timezones().
  76. my_birthday = datetime(1979, 7, 8, 22, 00)
  77. for specifier, expected in [
  78. ("b", "jul"),
  79. ("d", "08"),
  80. ("D", "Sun"),
  81. ("E", "July"),
  82. ("F", "July"),
  83. ("j", "8"),
  84. ("l", "Sunday"),
  85. ("L", "False"),
  86. ("m", "07"),
  87. ("M", "Jul"),
  88. ("n", "7"),
  89. ("N", "July"),
  90. ("o", "1979"),
  91. ("S", "th"),
  92. ("t", "31"),
  93. ("w", "0"),
  94. ("W", "27"),
  95. ("y", "79"),
  96. ("Y", "1979"),
  97. ("z", "189"),
  98. ]:
  99. with self.subTest(specifier=specifier):
  100. self.assertEqual(dateformat.format(my_birthday, specifier), expected)
  101. def test_date_formats_c_format(self):
  102. timestamp = datetime(2008, 5, 19, 11, 45, 23, 123456)
  103. self.assertEqual(
  104. dateformat.format(timestamp, "c"), "2008-05-19T11:45:23.123456"
  105. )
  106. def test_time_formats(self):
  107. # Specifiers 'I', 'r', and 'U' are covered in test_timezones().
  108. my_birthday = datetime(1979, 7, 8, 22, 00)
  109. for specifier, expected in [
  110. ("a", "p.m."),
  111. ("A", "PM"),
  112. ("f", "10"),
  113. ("g", "10"),
  114. ("G", "22"),
  115. ("h", "10"),
  116. ("H", "22"),
  117. ("i", "00"),
  118. ("P", "10 p.m."),
  119. ("s", "00"),
  120. ("u", "000000"),
  121. ]:
  122. with self.subTest(specifier=specifier):
  123. self.assertEqual(dateformat.format(my_birthday, specifier), expected)
  124. def test_dateformat(self):
  125. my_birthday = datetime(1979, 7, 8, 22, 00)
  126. self.assertEqual(dateformat.format(my_birthday, r"Y z \C\E\T"), "1979 189 CET")
  127. self.assertEqual(dateformat.format(my_birthday, r"jS \o\f F"), "8th of July")
  128. def test_futuredates(self):
  129. the_future = datetime(2100, 10, 25, 0, 00)
  130. self.assertEqual(dateformat.format(the_future, r"Y"), "2100")
  131. def test_day_of_year_leap(self):
  132. self.assertEqual(dateformat.format(datetime(2000, 12, 31), "z"), "366")
  133. def test_timezones(self):
  134. my_birthday = datetime(1979, 7, 8, 22, 00)
  135. summertime = datetime(2005, 10, 30, 1, 00)
  136. wintertime = datetime(2005, 10, 30, 4, 00)
  137. noon = time(12, 0, 0)
  138. # 3h30m to the west of UTC
  139. tz = get_fixed_timezone(-210)
  140. aware_dt = datetime(2009, 5, 16, 5, 30, 30, tzinfo=tz)
  141. if TZ_SUPPORT:
  142. for specifier, expected in [
  143. ("e", ""),
  144. ("O", "+0100"),
  145. ("r", "Sun, 08 Jul 1979 22:00:00 +0100"),
  146. ("T", "CET"),
  147. ("U", "300315600"),
  148. ("Z", "3600"),
  149. ]:
  150. with self.subTest(specifier=specifier):
  151. self.assertEqual(
  152. dateformat.format(my_birthday, specifier), expected
  153. )
  154. self.assertEqual(dateformat.format(aware_dt, "e"), "-0330")
  155. self.assertEqual(
  156. dateformat.format(aware_dt, "r"),
  157. "Sat, 16 May 2009 05:30:30 -0330",
  158. )
  159. self.assertEqual(dateformat.format(summertime, "I"), "1")
  160. self.assertEqual(dateformat.format(summertime, "O"), "+0200")
  161. self.assertEqual(dateformat.format(wintertime, "I"), "0")
  162. self.assertEqual(dateformat.format(wintertime, "O"), "+0100")
  163. for specifier in ["e", "O", "T", "Z"]:
  164. with self.subTest(specifier=specifier):
  165. self.assertEqual(dateformat.time_format(noon, specifier), "")
  166. # Ticket #16924 -- We don't need timezone support to test this
  167. self.assertEqual(dateformat.format(aware_dt, "O"), "-0330")
  168. def test_invalid_time_format_specifiers(self):
  169. my_birthday = date(1984, 8, 7)
  170. for specifier in ["a", "A", "f", "g", "G", "h", "H", "i", "P", "r", "s", "u"]:
  171. with self.subTest(specifier=specifier):
  172. msg = (
  173. "The format for date objects may not contain time-related "
  174. f"format specifiers (found {specifier!r})."
  175. )
  176. with self.assertRaisesMessage(TypeError, msg):
  177. dateformat.format(my_birthday, specifier)
  178. @requires_tz_support
  179. def test_e_format_with_named_time_zone(self):
  180. dt = datetime(1970, 1, 1, tzinfo=timezone.utc)
  181. self.assertEqual(dateformat.format(dt, "e"), "UTC")
  182. @requires_tz_support
  183. def test_e_format_with_time_zone_with_unimplemented_tzname(self):
  184. class NoNameTZ(tzinfo):
  185. """Time zone without .tzname() defined."""
  186. def utcoffset(self, dt):
  187. return None
  188. dt = datetime(1970, 1, 1, tzinfo=NoNameTZ())
  189. self.assertEqual(dateformat.format(dt, "e"), "")
  190. def test_P_format(self):
  191. for expected, t in [
  192. ("midnight", time(0)),
  193. ("noon", time(12)),
  194. ("4 a.m.", time(4)),
  195. ("8:30 a.m.", time(8, 30)),
  196. ("4 p.m.", time(16)),
  197. ("8:30 p.m.", time(20, 30)),
  198. ]:
  199. with self.subTest(time=t):
  200. self.assertEqual(dateformat.time_format(t, "P"), expected)
  201. def test_r_format_with_non_en_locale(self):
  202. # Changing the locale doesn't change the "r" format.
  203. dt = datetime(1979, 7, 8, 22, 00)
  204. with translation.override("fr"):
  205. self.assertEqual(
  206. dateformat.format(dt, "r"),
  207. "Sun, 08 Jul 1979 22:00:00 +0100",
  208. )
  209. def test_S_format(self):
  210. for expected, days in [
  211. ("st", [1, 21, 31]),
  212. ("nd", [2, 22]),
  213. ("rd", [3, 23]),
  214. ("th", (n for n in range(4, 31) if n not in [21, 22, 23])),
  215. ]:
  216. for day in days:
  217. dt = date(1970, 1, day)
  218. with self.subTest(day=day):
  219. self.assertEqual(dateformat.format(dt, "S"), expected)
  220. def test_y_format_year_before_1000(self):
  221. tests = [
  222. (476, "76"),
  223. (42, "42"),
  224. (4, "04"),
  225. ]
  226. for year, expected_date in tests:
  227. with self.subTest(year=year):
  228. self.assertEqual(
  229. dateformat.format(datetime(year, 9, 8, 5, 0), "y"),
  230. expected_date,
  231. )
  232. def test_Y_format_year_before_1000(self):
  233. self.assertEqual(dateformat.format(datetime(1, 1, 1), "Y"), "0001")
  234. self.assertEqual(dateformat.format(datetime(999, 1, 1), "Y"), "0999")
  235. def test_twelve_hour_format(self):
  236. tests = [
  237. (0, "12", "12"),
  238. (1, "1", "01"),
  239. (11, "11", "11"),
  240. (12, "12", "12"),
  241. (13, "1", "01"),
  242. (23, "11", "11"),
  243. ]
  244. for hour, g_expected, h_expected in tests:
  245. dt = datetime(2000, 1, 1, hour)
  246. with self.subTest(hour=hour):
  247. self.assertEqual(dateformat.format(dt, "g"), g_expected)
  248. self.assertEqual(dateformat.format(dt, "h"), h_expected)