Skip to content

Commit 9d06c75

Browse files
Corrected a bug to where time_override caused invalid date due to not converting to the correct timezone first. Refactored conversion code. Added testing support for Django 1.8. Removed Django from setup requirements - the onus of having a supported version of Django is on the developer.
1 parent fcb2db9 commit 9d06c75

File tree

6 files changed

+20
-22
lines changed

6 files changed

+20
-22
lines changed

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ python:
1010

1111
env:
1212
- DJANGO="https://github.com/django/django/archive/master.tar.gz"
13+
- DJANGO="django>=1.8.0,<1.9.0"
1314
- DJANGO="django>=1.7.0,<1.8.0"
1415
- DJANGO="django>=1.6.0,<1.7.0"
1516
- DJANGO="django>=1.5.0,<1.6.0"
@@ -45,7 +46,7 @@ matrix:
4546
- python: "2.6"
4647
env: DJANGO="django>=1.7.0,<1.8.0"
4748
- python: "2.6"
48-
env: DJANGO="django>=1.7.0,<1.8.0"
49+
env: DJANGO="django>=1.8.0,<1.9.0""
4950
- python: "2.6"
5051
env: DJANGO="https://github.com/django/django/archive/master.tar.gz"
5152
- python: "2.6"

README.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ Contributors
107107

108108
Changelog
109109
---------
110+
- 0.9 Corrected a bug to where ``time_override`` caused invalid date due to not converting to the correct timezone first. Refactored conversion code. Added testing support for Django 1.8. Removed Django from setup requirements - the onus of having a supported version of Django is on the developer.
110111
- 0.8 Corrected a bug to where ``time_override`` caused invalid date due to not converting to the correct timezone first. Added choices ``GROUPED_ALL_TIMEZONES_CHOICES`` and ``GROUPED_COMMON_TIMEZONES_CHOICES`` to the documentation.
111112
- 0.7 Corrected a bug where datetime.max.time() resulted in incorrect date/time. Changed tests to compare time_override models via string to prevent future regressions. Added choices ``GROUPED_ALL_TIMEZONES_CHOICES`` and ``GROUPED_COMMON_TIMEZONES_CHOICES``.
112113
- 0.6 Added RTD documentation. LinkedTZDateTimeField now returns the datetime object in the overidden timezone and time.

setup.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
packages=['timezone_utils'],
2222
install_requires=[
2323
'pytz',
24-
'django>=1.4,<1.8'
2524
],
2625
zip_safe=False,
2726
platforms='any',

tests/test_valid_tzdatetimefield.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def setUp(self):
3232
location = TZWithGoodStringDefault.objects.create()
3333
ModelWithForeignKeyToTimeZone.objects.create(other_model=location)
3434
TZTimeFramedModel.objects.create(
35-
start=make_aware(datetime(2014, 1, 1), pytz.timezone('US/Eastern')),
35+
start=datetime(2014, 1, 1),
3636
end=make_aware(datetime(2014, 12, 31), pytz.timezone('US/Eastern')),
3737
other_model=location
3838
)

timezone_utils/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
__version__ = (0, 8)
1+
__version__ = (0, 9)
22
VERSION = '.'.join(map(str, __version__))

timezone_utils/fields.py

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,9 @@ def to_python(self, value):
206206
if not value:
207207
return value
208208

209+
if is_naive(value):
210+
return make_aware(value=value, timezone=self.timezone)
211+
209212
return value.astimezone(self.timezone)
210213

211214
def pre_save(self, model_instance, add):
@@ -312,6 +315,11 @@ def _convert_value(self, value, model_instance, add):
312315
if self.populate_from is not None:
313316
tz = self._get_populate_from(model_instance)
314317

318+
# Convert the value to a datetime object in the correct timezone. This
319+
# insures that we will have the correct date if we are performing a time
320+
# override below.
321+
value = value.astimezone(tz)
322+
315323
# Do not convert the time to the time override if auto_now or
316324
# auto_now_add is set
317325
if self.time_override is not None and not (
@@ -320,24 +328,13 @@ def _convert_value(self, value, model_instance, add):
320328
# Retrieve the time override
321329
time_override = self._get_time_override()
322330

323-
# Convert the value to the correct timezone prior to applying the
324-
# time_override
325-
if not value.tzinfo == tz:
326-
value = value.astimezone(tz)
327-
328-
# Convert the value to the date/time
329-
value = datetime.combine(
330-
date=value.date(),
331-
time=time_override
331+
# Convert the value to the date/time with the appropriate timezone
332+
value = make_aware(
333+
value=datetime.combine(
334+
date=value.date(),
335+
time=time_override
336+
),
337+
timezone=tz
332338
)
333339

334-
# If the value is naive (due to the time_override conversion), make it
335-
# aware based on the appropriate timezone
336-
if is_naive(value):
337-
value = make_aware(value=value, timezone=tz)
338-
339-
# Set the value to the correct timezone
340-
if not value.tzinfo == tz:
341-
value = value.astimezone(tz)
342-
343340
return value

0 commit comments

Comments
 (0)