Skip to content

Commit 9835385

Browse files
committed
Add dateutil parser to backtest_service.py
1 parent dcb773b commit 9835385

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

investing_algorithm_framework/services/backtesting/backtest_service.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from datetime import datetime, timedelta
2+
from dateutil import parser
23
import re
34
import os
45
import json
@@ -612,10 +613,15 @@ def _get_start_date_from_backtest_report_file(self, path: str) -> datetime:
612613

613614
# Get the backtest start date from the file name
614615
backtest_start_date = os.path.basename(path).split("_")[3]
615-
# Parse the backtest start date
616-
return datetime.strptime(
617-
backtest_start_date, DATETIME_FORMAT_BACKTESTING
618-
)
616+
617+
try:
618+
# Parse the backtest start date
619+
return datetime.strptime(
620+
backtest_start_date, DATETIME_FORMAT_BACKTESTING
621+
)
622+
except ValueError:
623+
# Try to parse the backtest start date with a different format
624+
return parser.parse(backtest_start_date)
619625

620626
def _get_end_date_from_backtest_report_file(self, path: str) -> datetime:
621627
"""
@@ -630,10 +636,15 @@ def _get_end_date_from_backtest_report_file(self, path: str) -> datetime:
630636

631637
# Get the backtest end date from the file name
632638
backtest_end_date = os.path.basename(path).split("_")[5]
633-
# Parse the backtest end date
634-
return datetime.strptime(
635-
backtest_end_date, DATETIME_FORMAT_BACKTESTING
636-
)
639+
640+
try:
641+
# Parse the backtest end date
642+
return datetime.strptime(
643+
backtest_end_date, DATETIME_FORMAT_BACKTESTING
644+
)
645+
except ValueError:
646+
# Try to parse the backtest end date with a different format
647+
return parser.parse(backtest_end_date)
637648

638649
def _get_algorithm_name_from_backtest_report_file(self, path: str) -> str:
639650
"""

0 commit comments

Comments
 (0)