Closed happyCodingGirl-XD closed 2 years ago
Well, iso8601 defines various formats. STIX requires one particular ISO format. Your timestamps are missing "Z"s on the ends. Adding Z implies a zero UTC timezone offset, so if your timestamps are relative to a different timezone, you may need to adjust them before appending the Z.
Yeah, we did change datetime libraries awhile back, to address performance issues (see https://github.com/oasis-open/cti-python-stix2/pull/386 , https://github.com/oasis-open/cti-python-stix2/pull/397). We now use Python's builtin module. It performs better, but there was a tradeoff that there is less flexibility in accepted formats.
Thanks for the quick response. Yeah, I know it would work if I have the "Z" at the end. However, we use this in our automation to create the Report and other stix objects from the data we pull from ThreatStream. Their dates don't have the "Z" at the end. This breaks our automation procedure that worked before. We would have to write some custom code to add the "Z" to their dates or do something else. Do you have any suggestion? Thanks
If anyone needs a workaround, you can put
import stix2.utils
class stix_old_date_style(object):
def __enter__(self):
self._TIMESTAMP_FORMAT = stix2.utils._TIMESTAMP_FORMAT
self._TIMESTAMP_FORMAT_FRAC = stix2.utils._TIMESTAMP_FORMAT_FRAC
stix2.utils._TIMESTAMP_FORMAT = "%Y-%m-%dT%H:%M:%S"
stix2.utils._TIMESTAMP_FORMAT_FRAC = "%Y-%m-%dT%H:%M:%S.%f"
return self
def __exit__(self ,type, value, traceback):
stix2.utils._TIMESTAMP_FORMAT = self._TIMESTAMP_FORMAT
stix2.utils._TIMESTAMP_FORMAT_FRAC = self._TIMESTAMP_FORMAT_FRAC
return False
with stix_old_date_style() as s:
stix_report = Report(....)
around your stix_report = Report( ...)
block.
I don't have a simple workaround. Ramayer's is ugly, but I guess it would work. Of course, those module-level variables would not be considered a public API. So there would be no promise of its stability.
Depending on how your automation works, just adding a Z to the end is probably simpler than ramayer's clever workaround. We could add code to check for special cases like this, but it would slow the library down and we're bound to miss other formatting variations.
Thanks, I ended up writing a function to add the Z to all the dates if there is not already a Z in them
We have been using version 1.2.1 and it worked perfectly fine for datetime with ISO format. Today, I upgraded to version 3.0.1, it can't parse ISO datetime anymore.
Test to reproduce the error:
This is the error: