Skip to content

Commit

Permalink
further dodgy IG dates
Browse files Browse the repository at this point in the history
  • Loading branch information
bug-or-feature committed Oct 31, 2023
1 parent cb25e6a commit b8f6c8d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
12 changes: 6 additions & 6 deletions sysbrokers/IG/ig_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@

def convert_ig_date(date_str):
# possible formats:
# 2023-10-19T13:30:33.565
# 2023-10-19T13:30:33
# 2023-10-19T13:30:33.565 (23)
# 2023-10-31T15:57:48.39 (22)
# 2023-10-19T13:30:33 (19)
# PROBLEM: Unexpected format for IG date: 2023-10-31T15:57:48.39. Returning now
try:
if len(date_str) == 23:
return datetime.datetime.strptime(date_str, "%Y-%m-%dT%H:%M:%S.%f")
else:
return datetime.datetime.strptime(date_str, "%Y-%m-%dT%H:%M:%S")
if len(date_str) >= 19:
return datetime.datetime.strptime(date_str[:19], "%Y-%m-%dT%H:%M:%S")
except:
print(f"PROBLEM: Unexpected format for IG date: {date_str}. Returning now")
return datetime.datetime.now()
14 changes: 14 additions & 0 deletions sysbrokers/IG/tests/test_ig.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import datetime

import pytest
from sysbrokers.IG.ig_instruments_data import IgFuturesInstrumentData
from sysbrokers.IG.ig_futures_contract_data import IgFuturesContractData
from sysbrokers.IG.ig_utils import convert_ig_date
from sysobjects.contracts import futuresContract as fc
from sysobjects.contract_dates_and_expiries import expiryDate
from syscore.exceptions import missingContract
Expand Down Expand Up @@ -218,3 +221,14 @@ def test_expiry_dates(self):
data.broker_futures_contract.get_actual_expiry_date_for_single_contract(
fc.from_two_strings("CRAP", "20220300")
)

def test_date_formats(self):
assert convert_ig_date("2023-09-18T12:30:00.018") == datetime.datetime(
2023, 9, 18, 12, 30
)
assert convert_ig_date("2023-09-18T12:30:00.45") == datetime.datetime(
2023, 9, 18, 12, 30
)
assert convert_ig_date("2023-09-18T12:30:00") == datetime.datetime(
2023, 9, 18, 12, 30
)

0 comments on commit b8f6c8d

Please sign in to comment.