#!/usr/bin/env python3 import sqlite3 from datetime import datetime, timezone # Name of your SQLite database file DB_NAME = 'access_log.db' def convert_naive_to_timezone_aware(naive_ts): """ Convert a naive ISO timestamp (assumed to be in local time) into a timezone-aware timestamp. """ try: dt = datetime.fromisoformat(naive_ts) except Exception as e: print(f"Error parsing timestamp {naive_ts}: {e}") return naive_ts # If parsing fails, return the original value # If the timestamp is naive (i.e., no tzinfo), make it timezone-aware by assuming it's local time. if dt.tzinfo is None: # Get the local timezone info using the current time's offset. local_tz = datetime.now(timezone.utc).astimezone().tzinfo dt = dt.replace(tzinfo=local_tz) return dt.isoformat() else: # Timestamp is already timezone-aware return naive_ts def update_database(): conn = sqlite3.connect(DB_NAME) cursor = conn.cursor() # Fetch all records' IDs and timestamps cursor.execute("SELECT id, timestamp FROM file_access_log") rows = cursor.fetchall() updated_count = 0 for rec_id, ts in rows: new_ts = convert_naive_to_timezone_aware(ts) if new_ts != ts: cursor.execute("UPDATE file_access_log SET timestamp = ? WHERE id = ?", (new_ts, rec_id)) updated_count += 1 conn.commit() conn.close() print(f"Updated {updated_count} records to timezone-aware timestamps.") if __name__ == "__main__": update_database()