Commits

Sergey Schetinin committed fd87feb

add '_now' test hook in datetime_utils [from pycon2011 sprint w/ edits]

Comments (0)

Files changed (2)

tests/test_datetime_utils.py

 
 
 class _NowRestorer(object):
-
-    def __init__(self, new_NOW):
-        self._new_NOW = new_NOW
-        self._old_NOW = None
+    def __init__(self, new_now):
+        self._new_now = new_now
+        self._old_now = None
 
     def __enter__(self):
         import webob.datetime_utils
-        self._old_NOW = webob.datetime_utils._NOW
-        webob.datetime_utils._NOW = self._new_NOW
+        self._old_now = webob.datetime_utils._now
+        webob.datetime_utils._now = lambda: self._new_now
 
     def __exit__(self, exc_type, exc_value, traceback):
         import webob.datetime_utils
-        webob.datetime_utils._NOW = self._old_NOW
+        webob.datetime_utils._now = self._old_now

webob/datetime_utils.py

     'parse_date_delta', 'serialize_date_delta',
 ]
 
+_now = datetime.now # hook point for unit tests
+
 class _UTC(tzinfo):
     def dst(self, dt):
         return timedelta(0)
     if isinstance(dt, str):
         return dt
     if isinstance(dt, timedelta):
-        dt = datetime.now() + dt
+        dt = _now() + dt
     if isinstance(dt, (datetime, date)):
         dt = dt.timetuple()
     if isinstance(dt, (tuple, time.struct_time)):
     except ValueError:
         return parse_date(value)
     else:
-        return datetime.now() + timedelta(seconds=value)
+        return _now() + timedelta(seconds=value)
 
 
 def serialize_date_delta(value):