Last active
July 15, 2017 15:11
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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