Skip to content

Commit

Permalink
fix: Checking if both timestamps are datetime to avoid comparison issues
Browse files Browse the repository at this point in the history
- One could still be str if it is not properly formatted
  • Loading branch information
chrisr3d committed Mar 29, 2024
1 parent 3faacb1 commit cdd96fa
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
5 changes: 4 additions & 1 deletion stix2validator/v20/musts.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ def timestamp(instance):

def compare_timestamps(modified, created):
if TIMESTAMP_FORMAT_RE.match(modified) and TIMESTAMP_FORMAT_RE.match(created):
return datetime_from_str(modified) < datetime_from_str(created)
created_datetime = datetime_from_str(created)
modified_datetime = datetime_from_str(modified)
if isinstance(created_datetime, datetime) and isinstance(modified_datetime, datetime):
return modified_datetime < created_datetime
return modified < created


Expand Down
5 changes: 4 additions & 1 deletion stix2validator/v21/musts.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,10 @@ def timestamp(instance):
def compare(first, op, second):
comp = getattr(operator, op)
if TIMESTAMP_FORMAT_RE.match(first) and TIMESTAMP_FORMAT_RE.match(second):
return comp(datetime_from_str(first), datetime_from_str(second))
created_datetime = datetime_from_str(first)
modified_datetime = datetime_from_str(second)
if isinstance(created_datetime, datetime) and isinstance(modified_datetime, datetime):
return comp(created_datetime, modified_datetime)
return comp(first, second)


Expand Down

0 comments on commit cdd96fa

Please sign in to comment.