Fix Flake8 error and use UTC as default timezone

stix2.1
clenk 2017-04-17 13:16:14 -04:00
parent b4f116a33f
commit 40810646fb
2 changed files with 4 additions and 10 deletions

View File

@ -1,6 +1,6 @@
import re import re
import uuid import uuid
from six import PY2 from six import text_type
import datetime as dt import datetime as dt
import pytz import pytz
from dateutil import parser from dateutil import parser
@ -134,10 +134,7 @@ class ListProperty(Property):
class StringProperty(Property): class StringProperty(Property):
def __init__(self, **kwargs): def __init__(self, **kwargs):
if PY2: self.string_type = text_type
self.string_type = unicode
else:
self.string_type = str
super(StringProperty, self).__init__(**kwargs) super(StringProperty, self).__init__(**kwargs)
def clean(self, value): def clean(self, value):
@ -224,7 +221,6 @@ class TimestampProperty(Property):
return parsed.astimezone(pytz.utc) return parsed.astimezone(pytz.utc)
else: else:
# Doesn't have timezone info in the string; assume UTC # Doesn't have timezone info in the string; assume UTC
# TODO Should we default to system local timezone instead?
return pytz.utc.localize(parsed) return pytz.utc.localize(parsed)

View File

@ -1,7 +1,6 @@
"""Utility functions and classes for the stix2 library.""" """Utility functions and classes for the stix2 library."""
import datetime as dt import datetime as dt
import time
import pytz import pytz
# Sentinel value for fields that should be set to the current time. # Sentinel value for fields that should be set to the current time.
@ -24,9 +23,8 @@ def format_datetime(dttm):
try: try:
zoned = dttm.astimezone(pytz.utc) zoned = dttm.astimezone(pytz.utc)
except ValueError: except ValueError:
# dttm is timezone-naive # dttm is timezone-naive; assume UTC
tz_name = time.tzname[time.localtime().tm_isdst] pytz.utc.localize(dttm)
zoned = pytz.timezone(tz_name).localize(dttm).astimezone(pytz.utc)
ts = zoned.strftime("%Y-%m-%dT%H:%M:%S") ts = zoned.strftime("%Y-%m-%dT%H:%M:%S")
if zoned.microsecond > 0: if zoned.microsecond > 0:
ms = zoned.strftime("%f") ms = zoned.strftime("%f")