You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is this still open? I'd like to contribute. It seems that the problem is in line 42 of betfairlightweight/compat.py. The parse_datetime_as_naive format ignores timezones.
As this is essentially an interface change that will break existing code, for example:
events=trading.betting.list_events()
# Pretend betfairlightweight now returns aware datetimesopen_date=events[0].event.open_date.replace(tzinfo=datetime.timezone.utc)
# Raises TypeError: can't subtract offset-naive and offset-aware datetimestime_until_event_starts=open_date-datetime.datetime.utcnow()
the default behaviour needs to remain the same with the returning of aware datetimes controlled by a parameter similar to lightweight.
BaseResource.strip_datetime feels like a better place to apply the timezone than parse_datetime:
@staticmethoddefstrip_datetime(value, offset_aware=False):
""" Converts value to datetime if string or int. """ifisinstance(value, basestring):
try:
dt=parse_datetime(value)
ifoffset_aware:
dt=dt.replace(tzinfo=datetime.timezone.utc)
returndtexceptValueError:
returnelifisinstance(value, integer_types):
try:
dt=datetime.datetime.utcfromtimestamp(value/1e3)
ifoffset_aware:
dt=dt.replace(tzinfo=datetime.timezone.utc)
returndtexcept (ValueError, OverflowError, OSError):
return
The problem is that the places BaseResource.strip_datetime is called from (of which there are many) are typically the __init__s of objects which are exclusively concerned with assigning fields values and have no easy access to the state that would control this behaviour.
list_events()
returns timezone aware data for the openDate field, e.g. as indicated by the 'Z' for the UTC time zone in the response below:However, following conversion to a
datetime.datetime
object the timezone information is lost.Conversion from JSON to
datetime.datetime
should not lose the timezone info.The text was updated successfully, but these errors were encountered: