Create A KSUID For A Specific Date In Python
NOTE: I'm using ulid
now instead of ksuid
since it allows going back to epoch (Jan. 1, 1970) instead of KSUID's epock of May 2014.
NOTE: I need to go back and review this one
NOTE: KSUIDs epoch is May 13th, 2014. You can't go before that. They run out in somewhere over 100 years too. Less of a problem there, but something to know.
python3 -i -c "from ksuid import Ksuid; from datetime import datetime; datetime = datetime(year=2015, month=1, day=1, hour=10, minute=22, second=13); print(Ksuid(datetime))"
Output:
0AbwgV4iNcd543b9khXluR2QJj7
The original ksuid package doesn't have a way to generate KSUIDs for a specific date. I need that feature to go back and add KSUIDs to my previous notes/posts.
This python module provides the functionality.
Install with:
pip install svix-ksuid
And then run it with:
from ksuid import Ksuid
from datetime import datetime
year = 2017
month = 11
day = 16
hour = 12
minute = 0
second = 0
datetime = datetime(year=year, month=month, day=day, hour=hour, minute=minute, second=second)
the_id = Ksuid(datetime)
print(the_id)
# This 12 character version is what I use for the slugs on my site
print(str(the_id)[0:12])
Output:
0wRwD3sllvzEOg53Grv73EA5815
0wRwD3sllvzE
(NOTE: Be cafeful if you try the ruby version. I got incorrect dates when examining the values)
-- end of line --