We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
I finished a sqlite version:
from itertools import cycle from datetime import datetime import pytz def create_function_for_sharded_id(shard_id): get_next_seq_id = cycle(range(1024)) og_epoch_datetime = datetime.utcfromtimestamp(0).replace(tzinfo=pytz.utc) epoch_datetime = datetime(2016, 10, 19, 17, 54, 24, tzinfo=pytz.UTC) epoch_milliseconds = (epoch_datetime - og_epoch_datetime).total_seconds()*1000 def next_sharded_id(): now = datetime.utcnow() now = now.replace(tzinfo=pytz.UTC) stored_diff = (now - epoch_datetime).total_seconds()*1000 new_id = (int(stored_diff) << 23) | (shard_id << 10) | next(get_next_seq_id) return new_id return next_sharded_id import sqlite3 con = sqlite3.connect(":memory:") con.create_function("next_sharded_id", 0, create_function_for_sharded_id(31)) cur = con.cursor() cur.execute("select next_sharded_id()", ()) new_id = cur.fetchone()[0] print(new_id) og_epoch_datetime = datetime.utcfromtimestamp(0).replace(tzinfo=pytz.utc) epoch_datetime = datetime(2016, 10, 19, 17, 54, 24, tzinfo=pytz.UTC) epoch_milliseconds = (epoch_datetime - og_epoch_datetime).total_seconds()*1000 datetime.fromtimestamp(((new_id >> 23) + epoch_milliseconds)/ 1000, tz=pytz.UTC) # now in utc int(bin(new_id)[-10:], 2) # 0 int(bin(new_id)[-23:-10], 2) # 31
The text was updated successfully, but these errors were encountered:
No branches or pull requests
I finished a sqlite version:
The text was updated successfully, but these errors were encountered: