修复 NDX 日期解析和网络重试
This commit is contained in:
@@ -363,16 +363,29 @@ def parse_trade_date(timestamp: str, timezone_name: str) -> str:
|
||||
|
||||
def parse_trade_datetime(timestamp: str, timezone_name: str) -> datetime:
|
||||
cleaned = timestamp.replace("ET", "").strip()
|
||||
formats = ["%b %d, %Y %I:%M %p", "%m/%d/%Y %I:%M %p", "%Y-%m-%d %H:%M:%S", "%Y-%m-%d"]
|
||||
tz = get_timezone(timezone_name)
|
||||
for fmt in formats:
|
||||
formats = [
|
||||
("%b %d, %Y %I:%M %p", False),
|
||||
("%m/%d/%Y %I:%M %p", False),
|
||||
("%Y-%m-%d %H:%M:%S", False),
|
||||
("%b %d, %Y", True),
|
||||
("%m/%d/%Y", True),
|
||||
("%Y-%m-%d", True),
|
||||
]
|
||||
for fmt, date_only in formats:
|
||||
try:
|
||||
dt = datetime.strptime(cleaned, fmt)
|
||||
if date_only:
|
||||
# Nasdaq occasionally returns only the date for an index close.
|
||||
dt = datetime.combine(dt.date(), datetime_time(16, 0))
|
||||
return dt.replace(tzinfo=tz)
|
||||
except ValueError:
|
||||
continue
|
||||
try:
|
||||
return datetime.fromisoformat(cleaned).astimezone(tz)
|
||||
dt = datetime.fromisoformat(cleaned)
|
||||
if dt.tzinfo is None:
|
||||
return dt.replace(tzinfo=tz)
|
||||
return dt.astimezone(tz)
|
||||
except ValueError as exc:
|
||||
raise ValueError(f"无法解析 lastTradeTimestamp: {timestamp}") from exc
|
||||
|
||||
@@ -614,6 +627,20 @@ def curl_request(
|
||||
"--request",
|
||||
method,
|
||||
]
|
||||
if bool(proxy_config.get("curl_http1_1", True)):
|
||||
# Some HTTPS proxies intermittently fail while tunnelling HTTP/2.
|
||||
command.append("--http1.1")
|
||||
retry_count = max(0, int(proxy_config.get("curl_retry_count", 2)))
|
||||
if retry_count and method.upper() in {"GET", "HEAD"}:
|
||||
command.extend(
|
||||
[
|
||||
"--retry",
|
||||
str(retry_count),
|
||||
"--retry-delay",
|
||||
str(max(0, int(proxy_config.get("curl_retry_delay_seconds", 1)))),
|
||||
"--retry-all-errors",
|
||||
]
|
||||
)
|
||||
proxy_url = str(proxy_config.get("proxy_url", "")).strip()
|
||||
if proxy_url:
|
||||
command.extend(["--proxy", proxy_url])
|
||||
|
||||
Reference in New Issue
Block a user