import sqlite3 db = sqlite3.connect( ":memory:", detect_types=sqlite3.PARSE_COLNAMES | sqlite3.PARSE_DECLTYPES ) cursor = db.cursor() # Create a table and insert a timestamp into it with a UTC offset. cursor.execute("CREATE TABLE data(created_at TIMESTAMP);") cursor.execute("INSERT INTO data VALUES (?)", (b"2021-10-02 01:33:02.454000+00:00",)) # Fetch the timestamp as text from the database. # # UTC offset is present, because CAST(...) sidesteps Python's default converter. cursor.execute("SELECT CAST(created_at AS TEXT) FROM data") row = cursor.fetchone() print("Original row as string: ", repr(row[0])) # Fetch the timestamp as a datetime object. # # UTC offset is not present, because Python's default converter ignores UTC offsets. cursor.execute("SELECT rowid, created_at FROM data") rowid, created_at = cursor.fetchone() print("Original row as datetime: ", repr(created_at)) # Save the timestamp back to the database. cursor.execute("UPDATE data SET created_at = ? WHERE rowid = ?", (created_at, rowid)) # Fetch the timestamp as text from the database. # # Disaster! UTC offset has been erased. cursor.execute("SELECT CAST(created_at AS TEXT) FROM data") row = cursor.fetchone() print("Updated row as string: ", repr(row[0])) db.close()