mirror of
https://github.com/OpenBB-finance/OpenBB.git
synced 2026-10-28 04:52:07 +00:00
[BugFix] Handling negative timestamps on Windows (#6298)
* safe fromtimestamp * sage timestamp convertion on equity profile --------- Co-authored-by: hjoaquim <h.joaquim@campus.fct.unl.pt>
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
"""Provider helpers."""
|
||||
|
||||
import asyncio
|
||||
import os
|
||||
import re
|
||||
from datetime import date, datetime
|
||||
from datetime import date, datetime, timedelta, timezone
|
||||
from difflib import SequenceMatcher
|
||||
from functools import partial
|
||||
from inspect import iscoroutinefunction
|
||||
@@ -313,3 +314,10 @@ def filter_by_dates(
|
||||
return False
|
||||
|
||||
return list(filter(_filter, data))
|
||||
|
||||
|
||||
def safe_fromtimestamp(timestamp: float, tz: Optional[timezone] = None) -> datetime:
|
||||
"""datetime.fromtimestamp alternative which supports negative timestamps on Windows platform."""
|
||||
if os.name == "nt" and timestamp < 0:
|
||||
return datetime(1970, 1, 1, tzinfo=tz) + timedelta(seconds=timestamp)
|
||||
return datetime.fromtimestamp(timestamp, tz)
|
||||
|
||||
@@ -3,7 +3,10 @@
|
||||
# pylint: disable=unused-argument
|
||||
import asyncio
|
||||
import warnings
|
||||
from datetime import datetime
|
||||
from datetime import (
|
||||
date as dateType,
|
||||
timezone,
|
||||
)
|
||||
from typing import Any, Dict, List, Optional
|
||||
|
||||
from openbb_core.provider.abstract.fetcher import Fetcher
|
||||
@@ -11,6 +14,7 @@ from openbb_core.provider.standard_models.equity_info import (
|
||||
EquityInfoData,
|
||||
EquityInfoQueryParams,
|
||||
)
|
||||
from openbb_core.provider.utils.helpers import safe_fromtimestamp
|
||||
from pydantic import Field, field_validator
|
||||
from yfinance import Ticker
|
||||
|
||||
@@ -97,9 +101,9 @@ class YFinanceEquityProfileData(EquityInfoData):
|
||||
|
||||
@field_validator("first_stock_price_date", mode="before", check_fields=False)
|
||||
@classmethod
|
||||
def validate_first_trade_date(cls, v):
|
||||
def validate_first_trade_date(cls, v: float) -> Optional[dateType]:
|
||||
"""Validate first stock price date."""
|
||||
return datetime.utcfromtimestamp(v).date() if v else None
|
||||
return safe_fromtimestamp(v, tz=timezone.utc).date() if v else None
|
||||
|
||||
|
||||
class YFinanceEquityProfileFetcher(
|
||||
|
||||
Reference in New Issue
Block a user