Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save codingforentrepreneurs/eb644c4f8161b08721b1f5bfdb994147 to your computer and use it in GitHub Desktop.
Save codingforentrepreneurs/eb644c4f8161b08721b1f5bfdb994147 to your computer and use it in GitHub Desktop.
Convert a uuid1().time into a datetime object.

I've been using cassandra a lot lately. It's very common to denote ID fields in cassanda as a time-based uuid field called timeuuid docs. In python, a timeuuuid is mearly uuid.uuid1 from the built-in uuid package.

Using the cassandra-driver, you might end up with a model like:

import uuid
from cassandra.cqlengine import columns
from cassandra.cqlengine.models import Model


class VideoComment(Model):
    video_id = columns.Text(primary_key=True)
    comment_id = columns.TimeUUID(primary_key=True, default=uuid.uuid1) # second primary key component is a clustering key
    comment = columns.Text()
    

Convering the field comment_id into something a bit more useful, was a bit tricky. That's why I made this guide & code. Be sure to grab the uuid_cfe.py file to use the following examples:

Example 1 - Pure Python

import uuid_cfe
import uuid

example_time = uuid1().time
example_datetime = uuid_cfe.uuid1_time_to_datetime(example_time)

Example 2 -- Using Pandas

import uuid
import pandas as pd
import uuid_cfe

example_data = [{"x": y, "uuid1": uuid.uuid1()} for y in range(20)]
df = pd.DataFrame(example_data)
df['datetime'] = df['uuid1'].apply(lambda x: uuid_cfe.uuid1_time_to_datetime(x.time))

Example 3 - Cassandra Model along with Pandas

To me, this is the most practical use case. Query a cassandra database, load it into pandas (for all kinds of analysis), extract the amazing built-in timestamps to the timeuuid/uuid fields!

import pandas as pd
import uuid_cfe
# VideoComment defined above

# extract data from cassandra db
items = list(VideoComment.objects().values_list("video_id", "comment_id"))

# initialize a dataframe
df = pd.DataFrame(items, columns=['video_id', 'comment_id'])

# create a pandas column from the cassandra timeuuid column
df['time_int'] = df['comment_id'].apply(lambda x: x.time)

# convert the time from timeuuid/uuid1 into a datetime object
df["date"] = df['time_int'].apply(lambda x: uuid_cfe.uuid1_time_to_datetime(x))

# sort by our new datetime object
df = df.sort_values('date', inplace=True, ascending=False)
import datetime
def uuid1_time_to_datetime(time:int):
"""
Start datetime is on October 15th, 1582.
WHY? https://en.wikipedia.org/wiki/1582
add the time from uuid.uui1().time
divided by 10 (ignoring the remainder thus //)
"""
return datetime.datetime(1582, 10, 15) + datetime.timedelta(microseconds=time//10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment