Skip to content

Commit

Permalink
Merge d10c1aa into c2d1dd2
Browse files Browse the repository at this point in the history
  • Loading branch information
pganssle committed May 17, 2019
2 parents c2d1dd2 + d10c1aa commit b58e9bd
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 19 deletions.
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ matrix:
- python: 3.7
dist: xenial
sudo: required
- python: nightly
dist: xenial
sudo: required
allow_failures:
- python: "nightly"

script: make travis
install:
- pip install .
Expand Down
38 changes: 22 additions & 16 deletions freezegun/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@
real_localtime = time.localtime
real_gmtime = time.gmtime
real_strftime = time.strftime
real_clock = time.clock
real_date = datetime.date
real_datetime = datetime.datetime
real_date_objects = [real_time, real_localtime, real_gmtime, real_strftime, real_date, real_datetime]
_real_time_object_ids = set(id(obj) for obj in real_date_objects)

# time.clock is deprecated and was removed in Python 3.8
real_clock = getattr(time, 'clock', None)

freeze_factories = []
tz_offsets = []
ignore_lists = []
Expand Down Expand Up @@ -201,24 +203,24 @@ def fake_strftime(format, time_to_format=None):
else:
return real_strftime(format, time_to_format)

if real_clock is not None:
def fake_clock():
if _should_use_real_time():
return real_clock()

def fake_clock():
if _should_use_real_time():
return real_clock()

if len(freeze_factories) == 1:
return 0.0 if not tick_flags[-1] else real_clock()
if len(freeze_factories) == 1:
return 0.0 if not tick_flags[-1] else real_clock()

first_frozen_time = freeze_factories[0]()
last_frozen_time = get_current_time()
first_frozen_time = freeze_factories[0]()
last_frozen_time = get_current_time()

timedelta = (last_frozen_time - first_frozen_time)
total_seconds = timedelta.total_seconds()
timedelta = (last_frozen_time - first_frozen_time)
total_seconds = timedelta.total_seconds()

if tick_flags[-1]:
total_seconds += real_clock()
if tick_flags[-1]:
total_seconds += real_clock()

return total_seconds
return total_seconds


class FakeDateMeta(type):
Expand Down Expand Up @@ -579,7 +581,6 @@ def start(self):
time.localtime = fake_localtime
time.gmtime = fake_gmtime
time.strftime = fake_strftime
time.clock = fake_clock
if uuid_generate_time_attr:
setattr(uuid, uuid_generate_time_attr, None)
uuid._UuidCreate = None
Expand All @@ -596,8 +597,13 @@ def start(self):
('real_localtime', real_localtime, fake_localtime),
('real_strftime', real_strftime, fake_strftime),
('real_time', real_time, fake_time),
('real_clock', real_clock, fake_clock),
]

if real_clock is not None:
# time.clock is deprecated and was removed in Python 3.8
time.clock = fake_clock
to_patch.append(('real_clock', real_clock, fake_clock))

self.fake_names = tuple(fake.__name__ for real_name, real, fake in to_patch)
self.reals = dict((id(fake), real) for real_name, real, fake in to_patch)
fakes = dict((id(real), fake) for real_name, real, fake in to_patch)
Expand Down
10 changes: 8 additions & 2 deletions tests/test_datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
except ImportError:
maya = None

# time.clock was removed in Python 3.8
HAS_CLOCK = hasattr(time, 'clock')

class temp_locale(object):
"""Temporarily change the locale."""
Expand Down Expand Up @@ -207,6 +209,8 @@ def test_time_gmtime():
assert time_struct.tm_isdst == -1


@pytest.mark.skipif(not HAS_CLOCK,
reason="time.clock was removed in Python 3.8")
def test_time_clock():
with freeze_time('2012-01-14 03:21:34'):
assert time.clock() == 0
Expand Down Expand Up @@ -643,10 +647,12 @@ def test_should_use_real_time():
assert time.time() == expected_frozen
# assert time.localtime() == expected_frozen_local
assert time.gmtime() == expected_frozen_gmt
assert time.clock() == expected_clock
if HAS_CLOCK:
assert time.clock() == expected_clock

with freeze_time(frozen, ignore=['_pytest', 'nose']):
assert time.time() != expected_frozen
# assert time.localtime() != expected_frozen_local
assert time.gmtime() != expected_frozen_gmt
assert time.clock() != expected_clock
if HAS_CLOCK:
assert time.clock() != expected_clock
4 changes: 3 additions & 1 deletion tests/test_ticking.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import datetime
import time
import mock
import pytest

from freezegun import freeze_time
from tests import utils


@utils.cpython_only
def test_ticking_datetime():
with freeze_time("Jan 14th, 2012", tick=True):
time.sleep(0.001) # Deal with potential clock resolution problems
assert datetime.datetime.now() > datetime.datetime(2012, 1, 14)


@pytest.mark.skipif(not hasattr(time, "clock"),
reason="time.clock was removed in Python 3.8")
@utils.cpython_only
def test_ticking_time_clock():
with freeze_time('2012-01-14 03:21:34', tick=True):
Expand Down

0 comments on commit b58e9bd

Please sign in to comment.