Skip to content

Commit

Permalink
Fix is_valid_timestamp for strings with timezone (#417)
Browse files Browse the repository at this point in the history
* fix timestamp_format timezone

* add tests for is_valid_timestamp

* ruff format

* make comments consistent

* update generated timezone comment

* delete utc blurb
  • Loading branch information
g-marconet authored Feb 6, 2025
1 parent 402b05f commit 1f0ac7d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import logging
import time
from collections import namedtuple
from datetime import datetime, timedelta
from datetime import datetime, timedelta, timezone
from json import JSONDecodeError
from typing import Optional

Expand All @@ -17,7 +17,7 @@
logging.getLogger("botocore").setLevel(logging.WARNING)

DYNAMODB_TTL_DAYS = 60
TIMESTAMP_FORMAT = "%Y-%m-%dT%H:%M:%SZ"
TIMESTAMP_FORMAT = "%Y-%m-%dT%H:%M:%S%z"


# -- This constant represents the maximum age for a HEIMDALL Scan --
Expand Down Expand Up @@ -102,14 +102,13 @@ def parse_timestamp(timestamp: Optional[str] = None) -> str:
Examples:
>>> parse_timestamp()
{"level":"DEBUG","location":"parse_timestamp","message":"Generating Default timestamp"}
"2024-04-24T22:35:36Z" # Output will vary based on the current date and time
"2024-01-01T6:14:57-0800" # Output will vary based on the current date, time, and timezone
>>> parse_timestamp("2024-06-24T22:50:00Z")
"2024-06-24T22:50:00Z"
Notes:
- The function uses the current system time to calculate the 3-month offset.
- All returned timestamps are in UTC (denoted by the 'Z' suffix).
"""
if timestamp and is_valid_timestamp(timestamp):
return timestamp
Expand All @@ -133,6 +132,6 @@ def is_valid_timestamp(timestamp: str) -> bool:


def get_datetime_from_past(days: int) -> datetime:
current_timestamp = datetime.now()
current_timestamp = datetime.now(timezone.utc)
three_months_ago = current_timestamp - timedelta(days=days)
return three_months_ago
12 changes: 12 additions & 0 deletions orchestrator/lambdas/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,15 @@ def test_get_json_from_response_success(self):
result = self.json_utils.get_json_from_response(json.dumps(test_dict))

self.assertEqual(test_dict, result)

def test_is_valid_timestamp_utc(self):
# Checks relative to current time minus a max scan age.
# Make sure to change this before 9998
time = "9999-01-01T00:00:00Z"
self.assertTrue(utils.is_valid_timestamp(time))

def test_is_valid_timestamp_localized(self):
# Checks relative to current time minus a max scan age.
# Make sure to change this before 9998
time = "9999-01-01T00:00:00-08:00"
self.assertTrue(utils.is_valid_timestamp(time))

0 comments on commit 1f0ac7d

Please sign in to comment.