Account for full date when replacing time values.

This prevents issues when the dates in question cross months or years as well as days.
This commit is contained in:
Christopher Charbonneau Wells 2017-10-01 10:00:42 -04:00
parent 76bb7b84ad
commit 4bb96b19be
2 changed files with 9 additions and 4 deletions

View File

@ -89,9 +89,11 @@ def card_sleep_day(child, date=None):
end = timezone.localtime(instance.end) end = timezone.localtime(instance.end)
# Account for dates crossing midnight. # Account for dates crossing midnight.
if start.date() != date: if start.date() != date:
start = start.replace(day=end.day, hour=0, minute=0, second=0) start = start.replace(year=end.year, month=end.month, day=end.day,
hour=0, minute=0, second=0)
elif end.date() != date: elif end.date() != date:
end = start.replace(day=start.day, hour=23, minute=59, second=59) end = start.replace(year=start.year, month=start.month,
day=start.day, hour=23, minute=59, second=59)
total += end - start total += end - start

View File

@ -109,9 +109,11 @@ def sleep_totals(child):
# Account for dates crossing midnight. # Account for dates crossing midnight.
if start.date() != end.date(): if start.date() != end.date():
totals[start.date()] += end.replace( totals[start.date()] += end.replace(
day=start.day, hour=23, minute=59, second=59) - start year=start.year, month=start.month, day=start.day,
hour=23, minute=59, second=59) - start
totals[end.date()] += end - start.replace( totals[end.date()] += end - start.replace(
day=end.day, hour=0, minute=0, second=0) year=end.year, month=end.month, day=end.day, hour=0, minute=0,
second=0)
else: else:
totals[start.date()] += instance.duration totals[start.date()] += instance.duration
@ -193,6 +195,7 @@ def sleep_pattern(child):
# Adjust end_time for the current entry. # Adjust end_time for the current entry.
end_time = end_time.replace( end_time = end_time.replace(
year=start_time.year, month=start_time.month,
day=start_time.day, hour=23, minute=59, second=0) day=start_time.day, hour=23, minute=59, second=0)
duration = end_time - start_time duration = end_time - start_time