Skip to content

Instantly share code, notes, and snippets.

@dkergl
Forked from leondz/id_str-time.py
Created June 1, 2018 11:24
Show Gist options
  • Save dkergl/8f46bc6ed8b4f182eaf963b50a76475a to your computer and use it in GitHub Desktop.
Save dkergl/8f46bc6ed8b4f182eaf963b50a76475a to your computer and use it in GitHub Desktop.
Extract unix time to nearest millisecond from Twitter tweet ID
# use id_str instead of id to avoid overflows when accidentally casting to int
def twitter_id_to_epoch(id_str):
# credit to "On the endogenesis of Twitter’s Spritzer and Gardenhose sample streams" Kergl et al., Proc ASONAM 2014 (IEEE)
id_str = id_str.replace("'", "")
id_i = int(id_str)
snowflake_epoch = id_i >> 22
epoch_ms = snowflake_epoch + 1288834974657
return epoch_ms / 1000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment