Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Adds time.time to the freezer #5

Closed
wants to merge 1 commit into from

2 participants

@sralmai

No description provided.

David Samuelson Freezes time.time 132761f
@spulec
Owner

Hi there,

Thanks so much for taking the time to do this and apologies for the delay in responding.

This looks great, but I have one or two concerns. If you have time to discuss/fix, great. Otherwise, I can pull the request in and make the changes myself.

If we're going to add support for the time module, we need to support all of the methods (time.clock(), time.timezone, etc). I don't want people to see the time module being mocked and assume incorrectly that all of the methods are mocked.

Second item is that we need to add some docs.

To reiterate, if you have some more time to work on this, great. Otherwise, I'll take the great start you've given me and run with it.

@sralmai

Your delay in response pales in comparison to mine. I apologize.

Your concerns:

  1. adding full support for time module
    I agree with adding time.timezone support. I will do this (and related function changes). But time.clock is a trickier (platform dependent) issue. And in any case, I don't believe it should change when time changes. Otherwise, we would break a(n implied, at least) strictly monotonic increasing guarantee. Programs stuck in my spaciotemporal plane don't run backwards ;).

  2. adding docs
    Absolutely. I will do this.

I'd like your feedback (and agreement) before I implement changes.

@spulec
Owner

Heh, yeah I haven't done any work on this.

I agree with your comments.

Make the changes and we can close this out. Also note that we are now python 3 compatible. Not sure if that changes any of your existing code.

@sralmai

And months later... I still haven't touched this. Based on your comments on the other adding time.time() branch, your rework using ctypes should resolve this, anyways.

@sralmai sralmai closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 13, 2012
  1. Freezes time.time

    David Samuelson authored
This page is out of date. Refresh to see the latest.
Showing with 42 additions and 1 deletion.
  1. +32 −1 freezegun/api.py
  2. +10 −0 tests/test_times.py
View
33 freezegun/api.py
@@ -1,4 +1,6 @@
import datetime
+import sys
+from calendar import timegm
import functools
from dateutil import parser
@@ -7,6 +9,32 @@
real_datetime = datetime.datetime
+class FakeTime(object):
+ def __init__(self):
+ import time
+ self._time_mod = time
+ self.active = False
+ self._time = 0 # frozen time to be filled in here
+
+ def time(self):
+ ''' faked time '''
+ if not self.active:
+ return self._time_mod.time()
+ return self._time
+
+ def set_time_from_datetime(self, time_to_freeze):
+ ''' set time to return '''
+ self._time = timegm(time_to_freeze.utctimetuple())
+
+ def __getattr__(self, name):
+ ''' pass through unknowns '''
+ return getattr(self._time_mod, name)
+
+
+sys.modules['time'] = FakeTime()
+import time
+
+
class FakeDate(real_date):
active = False
date_to_freeze = None
@@ -72,6 +100,7 @@ class _freeze_time():
def __init__(self, time_to_freeze_str, tz_offset):
time_to_freeze = parser.parse(time_to_freeze_str)
+ time.set_time_from_datetime(time_to_freeze)
self.time_to_freeze = time_to_freeze
self.tz_offset = tz_offset
@@ -90,14 +119,16 @@ def start(self):
datetime.datetime.tz_offset = self.tz_offset
datetime.datetime.active = True
- # Since datetime.datetime has already been mocket, just use that for
+ # Since datetime.datetime has already been mocked, just use that for
# calculating the date
datetime.date.date_to_freeze = datetime.datetime.now().date()
datetime.date.active = True
+ time.active = True
def stop(self):
datetime.datetime.active = False
datetime.date.active = False
+ time.active = False
def decorate_callable(self, func):
def wrapper(*args, **kwargs):
View
10 tests/test_times.py
@@ -0,0 +1,10 @@
+from freezegun import freeze_time
+import time
+
+
+def test_simple_api():
+ freezer = freeze_time("2012-01-14")
+ freezer.start()
+ assert time.time() == 1326499200
+ freezer.stop()
+ assert time.time() != 1326499200
Something went wrong with that request. Please try again.