From 6cc7730d24dc07613c3e6067a95d88604339330a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Vinot?= Date: Thu, 10 Oct 2019 18:27:47 +0200 Subject: [PATCH] chg: Decode datetime without dateutils if possible --- pymisp/abstract.py | 7 +++++++ pymisp/mispevent.py | 9 ++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/pymisp/abstract.py b/pymisp/abstract.py index 0e2d486..6b13143 100644 --- a/pymisp/abstract.py +++ b/pymisp/abstract.py @@ -6,6 +6,7 @@ import datetime from deprecated import deprecated from json import JSONEncoder +from uuid import UUID try: from rapidjson import load @@ -123,6 +124,8 @@ class MISPEncode(JSONEncoder): return obj.isoformat() elif isinstance(obj, Enum): return obj.value + elif isinstance(obj, UUID): + return str(obj) return JSONEncoder.default(self, obj) @@ -134,6 +137,8 @@ if HAS_RAPIDJSON: return obj.isoformat() elif isinstance(obj, Enum): return obj.value + elif isinstance(obj, UUID): + return str(obj) return rapidjson.default(obj) else: def pymisp_json_default(obj): @@ -143,6 +148,8 @@ else: return obj.isoformat() elif isinstance(obj, Enum): return obj.value + elif isinstance(obj, UUID): + return str(obj) return json.default(obj) diff --git a/pymisp/mispevent.py b/pymisp/mispevent.py index e3a10e8..47e4cf5 100644 --- a/pymisp/mispevent.py +++ b/pymisp/mispevent.py @@ -220,7 +220,14 @@ class MISPAttribute(AbstractMISP): if self.value is None: raise NewAttributeError('The value of the attribute is required.') if self.type == 'datetime' and isinstance(self.value, str): - self.value = parse(self.value) + try: + if '.' in self.value: + self.value = datetime.datetime.strptime(self.value, "%Y-%m-%dT%H:%M:%S.%f") + else: + self.value = datetime.datetime.strptime(self.value, "%Y-%m-%dT%H:%M:%S") + except ValueError: + # Slower, but if the other ones fail, that's a good fallback + self.value = parse(self.value) # Default values self.category = kwargs.pop('category', type_defaults['default_category'])