Skip to content

Commit

Permalink
Merge pull request #115 from tophatmonocle/master
Browse files Browse the repository at this point in the history
Add manual time increment (as a more controlled alternative to `tick=True`)
  • Loading branch information
spulec committed Oct 18, 2015
2 parents 66366cc + 288f6ac commit 96b4e27
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
1 change: 1 addition & 0 deletions AUTHORS.rst
Expand Up @@ -12,3 +12,4 @@ Patches and Suggestions
- `Jesse London <https://github.com/jesteria>`_
- `Zach Smith <https://github.com/zmsmith>`_
- `Adam Johnson <https://github.com/adamchainz>`_
- `Alex Ehlke <https://github.com/aehlke>`_
8 changes: 6 additions & 2 deletions freezegun/api.py
Expand Up @@ -254,6 +254,9 @@ def __init__(self, time_to_freeze):
def __call__(self):
return self.time_to_freeze

def tick(self, delta=datetime.timedelta(seconds=1)):
self.time_to_freeze += delta


class _freeze_time(object):

Expand Down Expand Up @@ -323,13 +326,12 @@ def tearDownClass(cls):
return klass

def __enter__(self):
self.start()
return self.start()

def __exit__(self, *args):
self.stop()

def start(self):

if self.tick:
time_to_freeze = TickingDateTimeFactory(self.time_to_freeze, real_datetime.now())
else:
Expand Down Expand Up @@ -409,6 +411,8 @@ def start(self):
datetime.date.dates_to_freeze.append(time_to_freeze)
datetime.date.tz_offsets.append(self.tz_offset)

return time_to_freeze

def stop(self):
datetime.datetime.times_to_freeze.pop()
datetime.datetime.tz_offsets.pop()
Expand Down
15 changes: 15 additions & 0 deletions tests/test_datetimes.py
Expand Up @@ -122,6 +122,21 @@ def test_time_with_dst():
freezer.stop()


def test_manual_increment():
initial_datetime = datetime.datetime(year=1, month=7, day=12,
hour=15, minute=6, second=3)
with freeze_time(initial_datetime) as frozen_datetime:
assert frozen_datetime() == initial_datetime

frozen_datetime.tick()
initial_datetime += datetime.timedelta(seconds=1)
assert frozen_datetime() == initial_datetime

frozen_datetime.tick(delta=datetime.timedelta(seconds=10))
initial_datetime += datetime.timedelta(seconds=10)
assert frozen_datetime() == initial_datetime


def test_bad_time_argument():
try:
freeze_time("2012-13-14", tz_offset=-4)
Expand Down

0 comments on commit 96b4e27

Please sign in to comment.