Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix is_valid_timestamp for strings with timezone #417

Merged
merged 6 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -133,6 +133,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))