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

Ensure new_version() actually creates a new version with a correct "modified" property value. #330

Closed
wants to merge 3 commits into from
Closed
Changes from 1 commit
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
7 changes: 6 additions & 1 deletion stix2/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,12 @@ def new_version(data, **kwargs):

cls = type(data)
if 'modified' not in kwargs:
kwargs['modified'] = get_timestamp()
old_modified = parse_into_datetime(data["modified"])
new_modified = get_timestamp()
# Ensure the new is newer than the old!
if new_modified <= old_modified:
new_modified = old_modified + dt.timedelta(microseconds=1000)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason to use microseconds=1000 instead of milliseconds=1?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

timedelta doesn't have a milliseconds field, but I guess it does support it as a kwarg. I hadn't realized that.

One thing I did think of though, is that if new_modified is later but less than a millisecond later, you may still wind up with a duplicate modified field due to loss of precision when it is truncated to milliseconds. What is really needed is to ensure that new_modified and old_modified represent different milliseconds. This is guaranteed if they're >= 1 millisecond different, but not necessarily if they are < 1 millisecond different (but it's still possible). So it probably actually needs further modification.

kwargs['modified'] = new_modified
elif 'modified' in data:
old_modified_property = parse_into_datetime(data.get('modified'), precision='millisecond')
new_modified_property = parse_into_datetime(kwargs['modified'], precision='millisecond')
Expand Down