Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #6 from dnmiller/master

Add support for subtracting date/datetime objects to create timedelta objects.
  • Loading branch information...
commit 0bebcf2ca73ea8a107008627de3a67c6aa6a1e72 2 parents 793bc7b + 9c2fb14
@spulec authored
Showing with 12 additions and 2 deletions.
  1. +8 −2 freezegun/api.py
  2. +4 −0 tests/test_operations.py
View
10 freezegun/api.py
@@ -24,7 +24,10 @@ def __sub__(self, other):
result = super(FakeDate, self).__sub__(other)
if result is NotImplemented:
return result
- return date_to_fakedate(result)
+ if isinstance(result, real_date):
+ return date_to_fakedate(result)
+ else:
+ return result
@classmethod
def today(cls):
@@ -53,7 +56,10 @@ def __sub__(self, other):
result = super(FakeDatetime, self).__sub__(other)
if result is NotImplemented:
return result
- return datetime_to_fakedatetime(result)
+ if isinstance(result, real_datetime):
+ return datetime_to_fakedatetime(result)
+ else:
+ return result
@classmethod
def now(cls):
View
4 tests/test_operations.py
@@ -21,11 +21,15 @@ def test_subtraction():
now = datetime.datetime.now()
before = now - datetime.timedelta(days=1)
other_before = now - relativedelta(days=1)
+ how_long = now - before
assert isinstance(before, datetime.datetime)
assert isinstance(other_before, datetime.datetime)
+ assert isinstance(how_long, datetime.timedelta)
today = datetime.date.today()
yesterday = today - datetime.timedelta(days=1)
other_yesterday = today - relativedelta(days=1)
+ how_long = today - yesterday
assert isinstance(yesterday, datetime.date)
assert isinstance(other_yesterday, datetime.date)
+ assert isinstance(how_long, datetime.timedelta)
Please sign in to comment.
Something went wrong with that request. Please try again.