diff --git a/mockredis/client.py b/mockredis/client.py index 926e048..361cbdf 100644 --- a/mockredis/client.py +++ b/mockredis/client.py @@ -189,6 +189,17 @@ def exists(self, key): return self._encode(key) in self.redis __contains__ = exists + def time(self): + """ + Emulate time + + :returns: a tuple of the current Unix timestamp, and the amount of + microseconds already elapsed in the current second. + """ + now = datetime.utcnow() + return int(time.mktime(now.timetuple())), now.microsecond + + def _expire(self, key, delta): if key not in self.redis: return False diff --git a/mockredis/tests/test_redis.py b/mockredis/tests/test_redis.py index 2dd1b8a..8d7066b 100644 --- a/mockredis/tests/test_redis.py +++ b/mockredis/tests/test_redis.py @@ -92,6 +92,17 @@ def test_incr_init(self): self.redis.decr('dkey') eq_(b'-1', self.redis.get('dkey')) + def test_time_format(self): + result = self.redis.time() + ok_(isinstance(result, tuple)) + eq_(len(result), 2) + + timestamp, millis = result + ok_(isinstance(timestamp, int)) + ok_(timestamp > 0) + ok_(isinstance(millis, int)) + ok_(millis > 0) + def test_ttl(self): self.redis.set('key', 'key') self.redis.expire('key', 30)