Skip to content

Commit

Permalink
Added a now() function to utils.dates which returns the current times…
Browse files Browse the repository at this point in the history
…tamp in UTC
  • Loading branch information
Bryan Worrell committed Feb 28, 2015
1 parent 851c69b commit d013f6a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
25 changes: 11 additions & 14 deletions stix/core/stix_package.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
# Copyright (c) 2015, The MITRE Corporation. All rights reserved.
# See LICENSE.txt for complete terms.

# stdlib
from datetime import datetime

# external
from dateutil.tz import tzutc
from cybox.core import Observables

# internal
import stix
import stix.utils
from stix.utils import dates
from stix.utils import parser
from stix_header import STIXHeader
import stix.utils as utils
from stix.campaign import Campaign
from stix.coa import CourseOfAction
from stix.exploit_target import ExploitTarget
from stix.indicator import Indicator
from stix.incident import Incident
from stix.threat_actor import ThreatActor
from stix.common.related import RelatedPackages

# relative imports
from .stix_header import STIXHeader
from .ttps import TTPs

# binding imports
import stix.bindings.stix_common as stix_common_binding
import stix.bindings.stix_core as stix_core_binding

Expand Down Expand Up @@ -51,7 +48,7 @@ def __init__(self, id_=None, idref=None, timestamp=None, stix_header=None, cours
if timestamp:
self.timestamp = timestamp
else:
self.timestamp = datetime.now(tzutc()) if not idref else None
self.timestamp = utils.dates.now() if not idref else None

@property
def id_(self):
Expand Down Expand Up @@ -83,7 +80,7 @@ def timestamp(self):

@timestamp.setter
def timestamp(self, value):
self._timestamp = dates.parse_value(value)
self._timestamp = utils.dates.parse_value(value)

@property
def stix_header(self):
Expand Down Expand Up @@ -317,7 +314,7 @@ def to_obj(self, return_obj=None, ns_info=None):
return_obj.id = self.id_
return_obj.idref = self.idref
return_obj.version = self.version
return_obj.timestamp = dates.serialize_value(self.timestamp)
return_obj.timestamp = utils.dates.serialize_value(self.timestamp)

if self.stix_header:
return_obj.STIX_Header = self.stix_header.to_obj(ns_info=ns_info)
Expand Down Expand Up @@ -353,7 +350,7 @@ def to_obj(self, return_obj=None, ns_info=None):

def to_dict(self):
d = {}

if self.id_:
d['id'] = self.id_
if self.idref:
Expand All @@ -363,7 +360,7 @@ def to_dict(self):
if self.idref:
d['idref'] = self.idref
if self.timestamp:
d['timestamp'] = dates.serialize_value(self.timestamp)
d['timestamp'] = utils.dates.serialize_value(self.timestamp)
if self.stix_header:
d['stix_header'] = self.stix_header.to_dict()
if self.campaigns:
Expand Down Expand Up @@ -457,7 +454,7 @@ def from_xml(cls, xml_file, encoding=None):
An instance of :class:`STIXPackage`.
"""
entity_parser = parser.EntityParser()
entity_parser = utils.parser.EntityParser()
return entity_parser.parse_xml(xml_file, encoding=encoding)


Expand Down
5 changes: 5 additions & 0 deletions stix/utils/dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from datetime import datetime

import dateutil
import dateutil.tz

def parse_value(value):
if not value:
Expand All @@ -16,3 +17,7 @@ def serialize_value(value):
if not value:
return None
return value.isoformat()


def now():
return datetime.now(tz=dateutil.tz.tzutc())

0 comments on commit d013f6a

Please sign in to comment.