Skip to content

Instantly share code, notes, and snippets.

@thewh1teagle
Last active March 8, 2024 20:59
Show Gist options
  • Save thewh1teagle/f13436109b908eb5e66ed700baa75b16 to your computer and use it in GitHub Desktop.
Save thewh1teagle/f13436109b908eb5e66ed700baa75b16 to your computer and use it in GitHub Desktop.
from datetime import timedelta, datetime
import sqlite3
# Create database with table
path = 'data.db'
conn = sqlite3.connect(path)
cur = conn.cursor()
cur.execute('CREATE TABLE IF NOT EXISTS data (id INTEGER PRIMARY KEY, date TEXT)')
# Create mock data from latest 30 days
virtual_date = datetime.utcnow() - timedelta(days=30)
interval = timedelta(minutes=30)
while virtual_date < datetime.utcnow():
utc_date = virtual_date.strftime('%Y-%m-%d %H:%M:%S')
cur.execute("INSERT INTO data (date) VALUES (?)", (utc_date,))
virtual_date += interval
# Commit chnages
conn.commit()
# Get latest hour data
cur.execute('SELECT id, date FROM data WHERE date >= DATETIME("now");')
result = cur.fetchall()
print(f'Found {len(result)} rows from latest hour') # 41 rows
for row in result:
print(row)
print('Current UTC: ', datetime.utcnow().isoformat() + 'Z')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment