From fbf9df4a19edc13139ebe497b1c0b7cf4357a7bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Vinot?= Date: Wed, 28 Mar 2018 09:47:36 +0200 Subject: [PATCH] new: More flexibility in -> datetime --- pymisp/tools/abstractgenerator.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/pymisp/tools/abstractgenerator.py b/pymisp/tools/abstractgenerator.py index b32aa6e..4fa85cd 100644 --- a/pymisp/tools/abstractgenerator.py +++ b/pymisp/tools/abstractgenerator.py @@ -5,7 +5,7 @@ import abc import six from .. import MISPObject from ..exceptions import InvalidMISPObject -from datetime import datetime +from datetime import datetime, date from dateutil.parser import parse @@ -13,14 +13,28 @@ from dateutil.parser import parse # Python3 way: class MISPObjectGenerator(metaclass=abc.ABCMeta): class AbstractMISPObjectGenerator(MISPObject): + def _detect_epoch(self, timestamp): + try: + float(timestamp) + return True + except ValueError: + return False + def _sanitize_timestamp(self, timestamp): if not timestamp: return datetime.now() + + if isinstance(timestamp, datetime): + return timestamp + elif isinstance(timestamp, date): + return datetime.combine(timestamp, datetime.min.time()) elif isinstance(timestamp, dict): if not isinstance(timestamp['value'], datetime): timestamp['value'] = parse(timestamp['value']) return timestamp - elif not isinstance(timestamp, datetime): + elif not isinstance(timestamp, datetime): # Supported: float/int/string + if self._detect_epoch(timestamp): + return datetime.fromtimestamp(float(timestamp)) return parse(timestamp) return timestamp