Created
June 19, 2014 20:29
-
-
Save bbelyeu/96bee3136a2ae4441183 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime | |
import sys | |
import time | |
''' | |
Test with moment id 1856634658674967553 | |
this is the first moment inserted in our yv_1 shard SO the sequence id should be 1, | |
shard id should be 1 and the datetime should be 2014-06-05 16:01:38+00 | |
''' | |
YV_EPOCH = 1180656000000 | |
if len(sys.argv) != 2: | |
print "Usage: python <script> <moment id>" | |
sys.exit(1) | |
moment_id = int(sys.argv[1]) | |
# sequence id is last 10 bits | |
seq_id = moment_id & 1023 | |
# shard id is bits 11-23 | |
shard_id = (moment_id & (8191 << 10)) >> 10 | |
# timestamp is bits 24-63 | |
# NOTE: we don't have access to bit 64 b/c we chose signed instead of unsigned | |
# for the db column | |
ts = moment_id >> 23 | |
# Our custom yv epoch is June 1st 2007 so add it to the ts | |
ts = ts + YV_EPOCH | |
# Break off milliseconds | |
milliseconds = ts % 1000 | |
ts = int(ts / 1000) | |
# Create datetime object from timestamp | |
dt = datetime.datetime.utcfromtimestamp(ts) | |
# Add milliseconds back | |
dt = dt + datetime.timedelta(microseconds=(milliseconds * 1000)) | |
# Now in postgresql string format | |
created_dt = dt.strftime("%Y-%m-%d %H:%M:%S+00") | |
print "Sequence Id:", seq_id | |
print "Shard:", "yv_" + str(shard_id) | |
print "Created Dt:", created_dt |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment