Skip to content

Added time support #136

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions mockredis/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions mockredis/tests/test_redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down