Skip to content

Commit

Permalink
Exchange.parse8601 sanitized input
Browse files Browse the repository at this point in the history
  • Loading branch information
meold committed Jun 5, 2018
1 parent 5be41c9 commit abcc40f
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions python/ccxt/base/exchange.py
Expand Up @@ -711,7 +711,11 @@ def parse_date(timestamp = None):
return Exchange.parse8601(timestamp)

@staticmethod
def parse8601(timestamp):
def parse8601(timestamp = None):
if timestamp is None:
return timestamp
if not isinstance(timestamp, str):
return None
yyyy = '([0-9]{4})-?'
mm = '([0-9]{2})-?'
dd = '([0-9]{2})(?:T|[\\s])?'
Expand All @@ -721,19 +725,22 @@ def parse8601(timestamp):
ms = '(\\.[0-9]{1,3})?'
tz = '(?:(\\+|\\-)([0-9]{2})\\:?([0-9]{2})|Z)?'
regex = r'' + yyyy + mm + dd + h + m + s + ms + tz
match = re.search(regex, timestamp, re.IGNORECASE)
yyyy, mm, dd, h, m, s, ms, sign, hours, minutes = match.groups()
ms = ms or '.000'
msint = int(ms[1:])
sign = sign or ''
sign = int(sign + '1')
hours = int(hours or 0) * sign
minutes = int(minutes or 0) * sign
offset = datetime.timedelta(hours=hours, minutes=minutes)
string = yyyy + mm + dd + h + m + s + ms + 'Z'
dt = datetime.datetime.strptime(string, "%Y%m%d%H%M%S.%fZ")
dt = dt + offset
return calendar.timegm(dt.utctimetuple()) * 1000 + msint
try:
match = re.search(regex, timestamp, re.IGNORECASE)
yyyy, mm, dd, h, m, s, ms, sign, hours, minutes = match.groups()
ms = ms or '.000'
msint = int(ms[1:])
sign = sign or ''
sign = int(sign + '1')
hours = int(hours or 0) * sign
minutes = int(minutes or 0) * sign
offset = datetime.timedelta(hours=hours, minutes=minutes)
string = yyyy + mm + dd + h + m + s + ms + 'Z'
dt = datetime.datetime.strptime(string, "%Y%m%d%H%M%S.%fZ")
dt = dt + offset
return calendar.timegm(dt.utctimetuple()) * 1000 + msint
except (TypeError, OverflowError, OSError, ValueError, AttributeError):
return None

@staticmethod
def hash(request, algorithm='md5', digest='hex'):
Expand Down

0 comments on commit abcc40f

Please sign in to comment.