Skip to content

Commit

Permalink
Merge pull request #82 from benfitzpatrick/subtract-greater-timepoint
Browse files Browse the repository at this point in the history
Rationalise duration output when subtracting timepoints
  • Loading branch information
matthewrmshin authored Jan 9, 2018
2 parents c0ae6fa + da763a5 commit 4c0b4f4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
4 changes: 2 additions & 2 deletions isodatetime/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -1324,6 +1324,8 @@ def __cmp__(self, other):

def __sub__(self, other):
if isinstance(other, TimePoint):
if other > self:
return -1 * (other - self)
other = other.copy()
other.set_time_zone(self.get_time_zone())
my_year, my_day_of_year = self.get_ordinal_date()
Expand All @@ -1333,8 +1335,6 @@ def __sub__(self, other):
diff_day += get_days_in_year_range(other_year, my_year - 1)
else:
diff_day -= get_days_in_year_range(my_year, other_year - 1)
if diff_day < 0:
return -1 * (other - self)
my_hour, my_minute, my_second = self.get_hour_minute_second()
other_hour, other_minute, other_second = (
other.get_hour_minute_second())
Expand Down
15 changes: 15 additions & 0 deletions isodatetime/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,21 @@ def get_timepoint_subtract_tests():
"hour_of_day": 5, "time_zone_hour": 0, "time_zone_minute": 0},
"P29DT19H"
),
(
{"year": 1969, "month_of_year": 7, "day_of_month": 20,
"hour_of_day": 20, "time_zone_hour": 0, "time_zone_minute": 0},
{"year": 1969, "month_of_year": 7, "day_of_month": 20,
"hour_of_day": 19, "time_zone_hour": 0, "time_zone_minute": 0},
"PT1H"
),

(
{"year": 1969, "month_of_year": 7, "day_of_month": 20,
"hour_of_day": 19, "time_zone_hour": 0, "time_zone_minute": 0},
{"year": 1969, "month_of_year": 7, "day_of_month": 20,
"hour_of_day": 20, "time_zone_hour": 0, "time_zone_minute": 0},
"-PT1H"
),
(
{"year": 1991, "month_of_year": 5, "day_of_month": 4,
"hour_of_day": 5, "time_zone_hour": 0, "time_zone_minute": 0},
Expand Down

0 comments on commit 4c0b4f4

Please sign in to comment.