diff options
author | Markus Holtermann <info@markusholtermann.eu> | 2013-02-20 03:07:25 +0100 |
---|---|---|
committer | Markus Holtermann <info@markusholtermann.eu> | 2013-02-20 03:08:20 +0100 |
commit | 5b6601f31b3e4d653336e484c044df3041c2b030 (patch) | |
tree | 729df656a786720491c6777f0ec68d4819e65281 | |
parent | a77bd63f6e87ab44e7bb797d36b8b87a6e42a9a5 (diff) | |
download | bvg-grabber-5b6601f31b3e4d653336e484c044df3041c2b030.tar.gz bvg-grabber-5b6601f31b3e4d653336e484c044df3041c2b030.tar.bz2 bvg-grabber-5b6601f31b3e4d653336e484c044df3041c2b030.zip |
Fix the bug that prevents making departures after a day-change to be treated as departures of the current day.
-rw-r--r-- | bvggrabber/api/__init__.py | 10 | ||||
-rw-r--r-- | tests/test_api.py | 28 |
2 files changed, 36 insertions, 2 deletions
diff --git a/bvggrabber/api/__init__.py b/bvggrabber/api/__init__.py index 3cd1239..fe45879 100644 --- a/bvggrabber/api/__init__.py +++ b/bvggrabber/api/__init__.py @@ -82,14 +82,20 @@ class Departure(object): # We assume to get a UNIX / POSIX timestamp self.when = datetime.datetime.fromtimestamp(when) elif isinstance(when, str): + # We need to parse a string. But we need to remove trailing + # whitespaces and * self.when = parse(re.sub('[\s*]$', '', when)) - #if (self.when - self.now).total_seconds() < -60: - # self.when = self.when + timedelta(days=1) elif isinstance(when, datetime.datetime): + # Everything's fine, we can just take the parameter as is self.when = when else: raise TypeError("when must be a valid datetime, timestamp or " "string!") + diff = abs((self.when - self.now).total_seconds()) + if not no_add_day and self.when < self.now and diff > 43200: + # 43200 are 12 hours in seconds So we accept a offset of 12 hours + # that is still counted as "time gone" for the current day. + self.when = self.when + datetime.timedelta(days=1) @property def remaining(self): diff --git a/tests/test_api.py b/tests/test_api.py index 8487bd2..4c403a9 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -238,6 +238,34 @@ class TestDeparture(BaseTestDeparture): "now: 03:04, line: A Line" self.assertEqual(str2, str(dep2)) + def test_regression_new_day(self): + now = datetime.datetime(2013, 1, 2, 23, 59, 0) + + dep = datetime.datetime(2013, 1, 2, 0, 1, 2) + departure = Departure("From My Station", "To Your Station", dep, + "A Line", since=now) + self.assertEqual(departure.remaining, 120) + departure = Departure("From My Station", "To Your Station", dep, + "A Line", since=now, no_add_day=True) + self.assertEqual(departure.remaining, -86280) + + mitday_current_day = datetime.datetime(2013, 1, 2, 11, 59, 0) + departure = Departure("From My Station", "To Your Station", + mitday_current_day, "A Line", since=now) + self.assertEqual(departure.remaining, -43200) + departure = Departure("From My Station", "To Your Station", + mitday_current_day, "A Line", since=now, + no_add_day=True) + + mitday_next_day = datetime.datetime(2013, 1, 2, 11, 58, 59) + departure = Departure("From My Station", "To Your Station", + mitday_next_day, "A Line", since=now) + self.assertEqual(departure.remaining, 43140) + departure = Departure("From My Station", "To Your Station", + mitday_next_day, "A Line", since=now, + no_add_day=True) + self.assertEqual(departure.remaining, -43260) + class TestDepartureTotalOrder(unittest.TestCase): |