Skip to content

Instantly share code, notes, and snippets.

@s-c-p
Last active July 15, 2017 15:11
Show Gist options
  • Save s-c-p/2b40cfda5d07fe5f2c5791cb6cd94006 to your computer and use it in GitHub Desktop.
Save s-c-p/2b40cfda5d07fe5f2c5791cb6cd94006 to your computer and use it in GitHub Desktop.
2 way binding for codec-like behavior connecting python and sqlite
import time
import sqlite3
import datetime
sqlite3.register_adapter(float, py2sql)
sqlite3.register_converter("pyTS", sql2py)
py2sql = lambda unix_epoch: str(datetime.datetime.fromtimestamp(unix_epoch))
sql2py = lambda strTime: datetime.datetime.timestamp(
datetime.datetime.strptime(strTime, "%Y-%m-%d %H:%M:%S.%f")
)
# test
x = time.time()
assert x == sql2py(py2sql(x))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment