Skip to content

Commit 1147026

Browse files
authored
fixes timestamp issue (csirtgadgets#90)
1 parent c59b3f4 commit 1147026

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

csirtg_indicator/indicator.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ def __init__(self, indicator=None, version=PROTOCOL_VERSION, **kwargs):
4242
setattr(self, k, v)
4343
continue
4444

45+
# set this at the end
4546
if k in FIELDS_TIME:
46-
setattr(self, k, kwargs[k])
4747
continue
4848

4949
if isinstance(kwargs[k], basestring):
@@ -63,6 +63,9 @@ def __init__(self, indicator=None, version=PROTOCOL_VERSION, **kwargs):
6363
self._count = None
6464
self.count = kwargs.get('count', 1)
6565

66+
for k in FIELDS_TIME:
67+
setattr(self, k, kwargs.get(k, None))
68+
6669
@property
6770
def indicator(self):
6871
return self.__indicator
@@ -96,6 +99,9 @@ def confidence(self):
9699
return self._confidence
97100

98101
def _time_setter(self, v):
102+
if not v:
103+
return
104+
99105
if isinstance(v, datetime):
100106
return v
101107
else:
@@ -194,7 +200,7 @@ def format_keys(self):
194200

195201
try:
196202
d[k] = d[k].format(**d)
197-
except (KeyError, ValueError):
203+
except (KeyError, ValueError, IndexError):
198204
pass
199205

200206
yield Indicator(**d)

test/test_timestamps.py

+26
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,29 @@ def test_indicator_timezones():
3131
assert i.firsttime == a
3232
assert i.lasttime == a
3333
assert i.reporttime == a
34+
35+
36+
def test_lasttime_only():
37+
l = arrow.utcnow().strftime("%Y-%m-%dT%H:%M:%S.%fZ")
38+
39+
i = Indicator('192.168.1.1', lasttime=l)
40+
41+
assert i.lasttime == arrow.get(l).datetime
42+
43+
s = str(i)
44+
i = json.loads(s)
45+
46+
assert i.get('firsttime') is None
47+
48+
49+
def test_firsttime_only():
50+
l = arrow.utcnow().strftime("%Y-%m-%dT%H:%M:%S.%fZ")
51+
52+
i = Indicator('192.168.1.1', firsttime=l)
53+
54+
assert i.firsttime == arrow.get(l).datetime
55+
56+
s = str(i)
57+
i = json.loads(s)
58+
59+
assert i.get('lasttime') is None

0 commit comments

Comments
 (0)