diff --git a/README.md b/README.md index ea81e95..611ea41 100644 --- a/README.md +++ b/README.md @@ -105,9 +105,12 @@ Create a database and relevant tables before first-time program execution using # Initialize with current timestamp invoke init-database - # Initialize with a specific timestamp + # Initialize with a specific unix timestamp invoke init-database --batch-end-epoch + # Initialize with a specific date + invoke init-database --batch-end-epoch "2024-06-05 00:00:00" + # Initialize with a timestamp from n minutes ago invoke init-database --mins-ago 20 ``` diff --git a/tasks.py b/tasks.py index 3cc5983..6316243 100644 --- a/tasks.py +++ b/tasks.py @@ -1,4 +1,5 @@ from datetime import datetime, timedelta, timezone +import re from invoke import task import os import psycopg2 @@ -75,7 +76,15 @@ def init_database(ctx, batch_end_epoch=None, mins_ago=None, override_empty=False elif batch_end_epoch is None: batch_end_epoch = datetime.now(timezone.utc).timestamp() else: - batch_end_epoch = int(batch_end_epoch) + datetime_pattern = re.compile(r"^\d{4}-\d{2}-\d{2}.\d{2}:\d{2}:\d{2}.*$") + # Convert batch_end_epoch to a timestamp in utc if it is a datetime string + # use regex to check if it is of format 'YYYY-MM-DD HH:MM:SS' + if datetime_pattern.match(batch_end_epoch): + batch_end_epoch = datetime.fromisoformat(batch_end_epoch).timestamp() + print(f"Converted datetime string to timestamp: {batch_end_epoch}") + else: + batch_end_epoch = int(batch_end_epoch) + print(f"Using provided timestamp: {batch_end_epoch}") # Check if the table is empty, if override_empty is False should_insert = True