Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

adding get_timestamp_from_id

  • Loading branch information...
commit 65cce2bdaa2df9e8f2c3ba6b29494ceed3cecdf3 1 parent fe2f90c
Michael Montano authored
Showing with 12 additions and 1 deletion.
  1. +3 −0  src/idworker.py
  2. +9 −1 test/test_idworker.py
3  src/idworker.py
View
@@ -19,6 +19,9 @@ def gen_id(timestamp, datacenter_id, worker_id, sequence):
(worker_id << WORKER_ID_SHIFT) | \
sequence
+def get_timestamp_from_id(id_):
+ return (id_ >> TIMESTAMP_SHIFT) + TWEPOCH
+
class IdWorker(object):
def __init__(self, worker_id, datacenter_id):
assert worker_id >= 0 and worker_id <= MAX_WORKER_ID
10 test/test_idworker.py
View
@@ -93,4 +93,12 @@ def test_generate_unique_ids():
for i in range(n):
tid = worker.next_id()
s.add(tid)
- assert len(s) == n
+ assert len(s) == n
+
+def test_get_timestamp_from_id():
+ worker = idworker.WakingIdWorker(1, 1)
+ ts = worker.get_timestamp()
+ id_ = idworker.gen_id(ts, 0, 0, 0)
+ id_ts = idworker.get_timestamp_from_id(id_)
+ print ts, id_ts
+ assert id_ts == ts
Please sign in to comment.
Something went wrong with that request. Please try again.