diff --git a/AUTHORS b/AUTHORS index 626d986e88..a151f530e9 100644 --- a/AUTHORS +++ b/AUTHORS @@ -521,6 +521,7 @@ answer newbie questions, and generally made Django that much better: Olivier Sels Orestis Markou Orne Brocaar + Oscar Ramirez Ossama M. Khayat Owen Griffiths Pablo Martín diff --git a/django/db/migrations/writer.py b/django/db/migrations/writer.py index 9907874bb4..75089ae2e2 100644 --- a/django/db/migrations/writer.py +++ b/django/db/migrations/writer.py @@ -305,6 +305,8 @@ class MigrationWriter(object): # Times elif isinstance(value, datetime.time): value_repr = repr(value) + if isinstance(value, datetime_safe.time): + value_repr = "datetime.%s" % value_repr return value_repr, {"import datetime"} # Settings references elif isinstance(value, SettingsReference): diff --git a/django/utils/datetime_safe.py b/django/utils/datetime_safe.py index 6cd0bf1cdd..51cdfe31ef 100644 --- a/django/utils/datetime_safe.py +++ b/django/utils/datetime_safe.py @@ -7,9 +7,9 @@ # >>> datetime_safe.date(1850, 8, 2).strftime("%Y/%m/%d was a %A") # '1850/08/02 was a Friday' -from datetime import date as real_date, datetime as real_datetime +from datetime import date as real_date, time as real_time, datetime as real_datetime import re -import time +import time as ttime class date(real_date): @@ -31,6 +31,10 @@ class datetime(real_datetime): return date(self.year, self.month, self.day) +class time(real_time): + pass + + def new_date(d): "Generate a safe date from a datetime.date object." return date(d.year, d.month, d.day) @@ -80,10 +84,10 @@ def strftime(dt, fmt): # Move to around the year 2000 year = year + ((2000 - year) // 28) * 28 timetuple = dt.timetuple() - s1 = time.strftime(fmt, (year,) + timetuple[1:]) + s1 = ttime.strftime(fmt, (year,) + timetuple[1:]) sites1 = _findall(s1, str(year)) - s2 = time.strftime(fmt, (year + 28,) + timetuple[1:]) + s2 = ttime.strftime(fmt, (year + 28,) + timetuple[1:]) sites2 = _findall(s2, str(year + 28)) sites = [] diff --git a/docs/releases/1.7.2.txt b/docs/releases/1.7.2.txt index fc7bb98349..ef65a37963 100644 --- a/docs/releases/1.7.2.txt +++ b/docs/releases/1.7.2.txt @@ -158,3 +158,5 @@ Bugfixes * Added quoting to field indexes in the SQL generated by migrations to prevent a crash when the index name requires it (:ticket:`#24015`). + +* Added ``datetime.time`` support to migrations questioner (:ticket:`23998`). diff --git a/tests/migrations/test_writer.py b/tests/migrations/test_writer.py index 71841a11d1..5e7e8b75cd 100644 --- a/tests/migrations/test_writer.py +++ b/tests/migrations/test_writer.py @@ -114,6 +114,10 @@ class WriterTests(TestCase): string, imports = MigrationWriter.serialize(safe_date) self.assertEqual(string, repr(datetime.date(2014, 3, 31))) self.assertEqual(imports, {'import datetime'}) + safe_time = datetime_safe.time(10, 25) + string, imports = MigrationWriter.serialize(safe_time) + self.assertEqual(string, repr(datetime.time(10, 25))) + self.assertEqual(imports, {'import datetime'}) safe_datetime = datetime_safe.datetime(2014, 3, 31, 16, 4, 31) string, imports = MigrationWriter.serialize(safe_datetime) self.assertEqual(string, repr(datetime.datetime(2014, 3, 31, 16, 4, 31))) diff --git a/tests/utils_tests/test_datetime_safe.py b/tests/utils_tests/test_datetime_safe.py index 3a8d31d6d8..655a7350d5 100644 --- a/tests/utils_tests/test_datetime_safe.py +++ b/tests/utils_tests/test_datetime_safe.py @@ -1,7 +1,7 @@ import unittest -from datetime import date as original_date, datetime as original_datetime -from django.utils.datetime_safe import date, datetime +from datetime import date as original_date, datetime as original_datetime, time as original_time +from django.utils.datetime_safe import date, datetime, time class DatetimeTests(unittest.TestCase): @@ -9,6 +9,7 @@ class DatetimeTests(unittest.TestCase): def setUp(self): self.just_safe = (1900, 1, 1) self.just_unsafe = (1899, 12, 31, 23, 59, 59) + self.just_time = (11, 30, 59) self.really_old = (20, 1, 1) self.more_recent = (2006, 1, 1) @@ -21,6 +22,8 @@ class DatetimeTests(unittest.TestCase): self.assertEqual(original_date(*self.just_safe).strftime('%Y-%m-%d'), date(*self.just_safe).strftime('%Y-%m-%d')) self.assertEqual(original_datetime(*self.just_safe).strftime('%Y-%m-%d'), datetime(*self.just_safe).strftime('%Y-%m-%d')) + self.assertEqual(original_time(*self.just_time).strftime('%H:%M:%S'), time(*self.just_time).strftime('%H:%M:%S')) + def test_safe_strftime(self): self.assertEqual(date(*self.just_unsafe[:3]).strftime('%Y-%m-%d (weekday %w)'), '1899-12-31 (weekday 0)') self.assertEqual(date(*self.just_safe).strftime('%Y-%m-%d (weekday %w)'), '1900-01-01 (weekday 1)') @@ -28,6 +31,8 @@ class DatetimeTests(unittest.TestCase): self.assertEqual(datetime(*self.just_unsafe).strftime('%Y-%m-%d %H:%M:%S (weekday %w)'), '1899-12-31 23:59:59 (weekday 0)') self.assertEqual(datetime(*self.just_safe).strftime('%Y-%m-%d %H:%M:%S (weekday %w)'), '1900-01-01 00:00:00 (weekday 1)') + self.assertEqual(time(*self.just_time).strftime('%H:%M:%S AM'), '11:30:59 AM') + # %y will error before this date self.assertEqual(date(*self.just_safe).strftime('%y'), '00') self.assertEqual(datetime(*self.just_safe).strftime('%y'), '00')