diff --git a/pythclient/calendar.py b/pythclient/calendar.py index 3d6ac57..9f44e11 100644 --- a/pythclient/calendar.py +++ b/pythclient/calendar.py @@ -74,7 +74,7 @@ def is_market_open(asset_type: str, dt: datetime.datetime) -> bool: return True -def get_next_market_open(asset_type: str, dt: datetime.datetime) -> str: +def get_next_market_open(asset_type: str, dt: datetime.datetime) -> int: # make sure time is in NY timezone dt = dt.astimezone(NY_TZ) time = dt.time() @@ -119,10 +119,10 @@ def get_next_market_open(asset_type: str, dt: datetime.datetime) -> str: while not is_market_open(asset_type, next_market_open): next_market_open += datetime.timedelta(days=1) - return next_market_open.astimezone(UTC_TZ).strftime("%Y-%m-%dT%H:%M:%S") + "Z" + return int(next_market_open.timestamp()) -def get_next_market_close(asset_type: str, dt: datetime.datetime) -> str: +def get_next_market_close(asset_type: str, dt: datetime.datetime) -> int: # make sure time is in NY timezone dt = dt.astimezone(NY_TZ) time = dt.time() @@ -148,16 +148,12 @@ def get_next_market_close(asset_type: str, dt: datetime.datetime) -> str: next_market_open = get_next_market_open( asset_type, dt + datetime.timedelta(days=1) ) - next_market_close = ( - datetime.datetime.fromisoformat(next_market_open.replace("Z", "+00:00")) - .astimezone(NY_TZ) - .replace( + next_market_close = datetime.datetime.fromtimestamp(next_market_open).astimezone(NY_TZ).replace( hour=EQUITY_CLOSE.hour, minute=EQUITY_CLOSE.minute, second=0, microsecond=0, ) - ) else: next_market_close = dt.replace( hour=EQUITY_CLOSE.hour, @@ -189,4 +185,4 @@ def get_next_market_close(asset_type: str, dt: datetime.datetime) -> str: else: # crypto markets never close return None - return next_market_close.astimezone(UTC_TZ).strftime("%Y-%m-%dT%H:%M:%S") + "Z" + return int(next_market_close.timestamp()) diff --git a/setup.py b/setup.py index 73c5d18..bbb81c9 100644 --- a/setup.py +++ b/setup.py @@ -7,7 +7,7 @@ setup( name='pythclient', - version='0.1.8', + version='0.1.9', packages=['pythclient'], author='Pyth Developers', author_email='contact@pyth.network', diff --git a/tests/test_calendar.py b/tests/test_calendar.py index 602c049..1c94fa4 100644 --- a/tests/test_calendar.py +++ b/tests/test_calendar.py @@ -24,8 +24,12 @@ CRYPTO_OPEN_WED_2023_6_21_12 = datetime.datetime(2023, 6, 21, 12, 0, 0, tzinfo=NY_TZ) CRYPTO_OPEN_SUN_2023_6_18_12 = datetime.datetime(2023, 6, 18, 12, 0, 0, tzinfo=NY_TZ) -def format_datetime_to_utc_iso_string(dt: datetime.datetime): - return dt.astimezone(UTC_TZ).strftime("%Y-%m-%dT%H:%M:%S") + "Z" + +def format_datetime_to_unix_timestamp(dt: datetime.datetime): + # Convert the datetime object to a Unix timestamp in UTC + timestamp = dt.astimezone(UTC_TZ).timestamp() + unix_timestamp_utc = int(timestamp) + return unix_timestamp_utc def test_is_market_open(): # equity @@ -67,65 +71,65 @@ def test_get_next_market_open(): # equity within market hours assert ( get_next_market_open("equity", EQUITY_OPEN_WED_2023_6_21_12) - == format_datetime_to_utc_iso_string(datetime.datetime(2023, 6, 22, 9, 30, 0, tzinfo=NY_TZ)) + == format_datetime_to_unix_timestamp(datetime.datetime(2023, 6, 22, 9, 30, 0, tzinfo=NY_TZ)) ) # equity out of market hours assert ( get_next_market_open("equity", EQUITY_CLOSE_WED_2023_6_21_17) - == format_datetime_to_utc_iso_string(datetime.datetime(2023, 6, 22, 9, 30, 0, tzinfo=NY_TZ)) + == format_datetime_to_unix_timestamp(datetime.datetime(2023, 6, 22, 9, 30, 0, tzinfo=NY_TZ)) ) # equity weekend assert ( get_next_market_open("equity", EQUITY_CLOSE_SAT_2023_6_10_17) - == format_datetime_to_utc_iso_string(datetime.datetime(2023, 6, 12, 9, 30, 0, tzinfo=NY_TZ)) + == format_datetime_to_unix_timestamp(datetime.datetime(2023, 6, 12, 9, 30, 0, tzinfo=NY_TZ)) ) # equity holiday assert ( get_next_market_open("equity", EQUITY_HOLIDAY_MON_2023_6_19) - == format_datetime_to_utc_iso_string(datetime.datetime(2023, 6, 20, 9, 30, 0, tzinfo=NY_TZ)) + == format_datetime_to_unix_timestamp(datetime.datetime(2023, 6, 20, 9, 30, 0, tzinfo=NY_TZ)) ) # equity early close holiday assert ( get_next_market_open("equity", EQUITY_EARLY_CLOSE_OPEN_FRI_2023_11_24_14) - == format_datetime_to_utc_iso_string(datetime.datetime(2023, 11, 27, 9, 30, 0, tzinfo=NY_TZ)) + == format_datetime_to_unix_timestamp(datetime.datetime(2023, 11, 27, 9, 30, 0, tzinfo=NY_TZ)) ) assert ( get_next_market_open("equity", EQUITY_EARLY_CLOSE_CLOSE_FRI_2023_11_24_14) - == format_datetime_to_utc_iso_string(datetime.datetime(2023, 11, 27, 9, 30, 0, tzinfo=NY_TZ)) + == format_datetime_to_unix_timestamp(datetime.datetime(2023, 11, 27, 9, 30, 0, tzinfo=NY_TZ)) ) # fx & metal within market hours assert ( get_next_market_open("fx", FX_METAL_OPEN_WED_2023_6_21_22) - == format_datetime_to_utc_iso_string(datetime.datetime(2023, 6, 25, 17, 0, 0, tzinfo=NY_TZ)) + == format_datetime_to_unix_timestamp(datetime.datetime(2023, 6, 25, 17, 0, 0, tzinfo=NY_TZ)) ) assert ( get_next_market_open("metal", FX_METAL_OPEN_WED_2023_6_21_22) - == format_datetime_to_utc_iso_string(datetime.datetime(2023, 6, 25, 17, 0, 0, tzinfo=NY_TZ)) + == format_datetime_to_unix_timestamp(datetime.datetime(2023, 6, 25, 17, 0, 0, tzinfo=NY_TZ)) ) # fx & metal out of market hours assert ( get_next_market_open("fx", FX_METAL_CLOSE_SUN_2023_6_18_16) - == format_datetime_to_utc_iso_string(datetime.datetime(2023, 6, 18, 17, 0, 0, tzinfo=NY_TZ)) + == format_datetime_to_unix_timestamp(datetime.datetime(2023, 6, 18, 17, 0, 0, tzinfo=NY_TZ)) ) assert ( get_next_market_open("metal", FX_METAL_CLOSE_SUN_2023_6_18_16) - == format_datetime_to_utc_iso_string(datetime.datetime(2023, 6, 18, 17, 0, 0, tzinfo=NY_TZ)) + == format_datetime_to_unix_timestamp(datetime.datetime(2023, 6, 18, 17, 0, 0, tzinfo=NY_TZ)) ) # fx & metal holiday assert ( get_next_market_open("fx", FX_METAL_HOLIDAY_SUN_2023_1_1) - == format_datetime_to_utc_iso_string(datetime.datetime(2023, 1, 2, 17, 0, 0, tzinfo=NY_TZ)) + == format_datetime_to_unix_timestamp(datetime.datetime(2023, 1, 2, 17, 0, 0, tzinfo=NY_TZ)) ) assert ( get_next_market_open("metal", FX_METAL_HOLIDAY_SUN_2023_1_1) - == format_datetime_to_utc_iso_string(datetime.datetime(2023, 1, 2, 17, 0, 0, tzinfo=NY_TZ)) + == format_datetime_to_unix_timestamp(datetime.datetime(2023, 1, 2, 17, 0, 0, tzinfo=NY_TZ)) ) # crypto @@ -137,65 +141,65 @@ def test_get_next_market_close(): # equity within market hours assert ( get_next_market_close("equity", EQUITY_OPEN_WED_2023_6_21_12) - == format_datetime_to_utc_iso_string(datetime.datetime(2023, 6, 21, 16, 0, 0, tzinfo=NY_TZ)) + == format_datetime_to_unix_timestamp(datetime.datetime(2023, 6, 21, 16, 0, 0, tzinfo=NY_TZ)) ) # equity out of market hours assert ( get_next_market_close("equity", EQUITY_CLOSE_WED_2023_6_21_17) - == format_datetime_to_utc_iso_string(datetime.datetime(2023, 6, 22, 16, 0, 0, tzinfo=NY_TZ)) + == format_datetime_to_unix_timestamp(datetime.datetime(2023, 6, 22, 16, 0, 0, tzinfo=NY_TZ)) ) # equity weekend assert ( get_next_market_close("equity", EQUITY_CLOSE_SAT_2023_6_10_17) - == format_datetime_to_utc_iso_string(datetime.datetime(2023, 6, 12, 16, 0, 0, tzinfo=NY_TZ)) + == format_datetime_to_unix_timestamp(datetime.datetime(2023, 6, 12, 16, 0, 0, tzinfo=NY_TZ)) ) # equity holiday assert ( get_next_market_close("equity", EQUITY_HOLIDAY_MON_2023_6_19) - == format_datetime_to_utc_iso_string(datetime.datetime(2023, 6, 20, 16, 0, 0, tzinfo=NY_TZ)) + == format_datetime_to_unix_timestamp(datetime.datetime(2023, 6, 20, 16, 0, 0, tzinfo=NY_TZ)) ) # equity early close holiday assert ( get_next_market_close("equity", EQUITY_EARLY_CLOSE_OPEN_FRI_2023_11_24_14) - == format_datetime_to_utc_iso_string(datetime.datetime(2023, 11, 24, 13, 0, 0, tzinfo=NY_TZ)) + == format_datetime_to_unix_timestamp(datetime.datetime(2023, 11, 24, 13, 0, 0, tzinfo=NY_TZ)) ) assert ( get_next_market_close("equity", EQUITY_EARLY_CLOSE_CLOSE_FRI_2023_11_24_14) - == format_datetime_to_utc_iso_string(datetime.datetime(2023, 11, 27, 16, 0, 0, tzinfo=NY_TZ)) + == format_datetime_to_unix_timestamp(datetime.datetime(2023, 11, 27, 16, 0, 0, tzinfo=NY_TZ)) ) # fx & metal within market hours assert ( get_next_market_close("fx", FX_METAL_OPEN_WED_2023_6_21_22) - == format_datetime_to_utc_iso_string(datetime.datetime(2023, 6, 23, 17, 0, 0, tzinfo=NY_TZ)) + == format_datetime_to_unix_timestamp(datetime.datetime(2023, 6, 23, 17, 0, 0, tzinfo=NY_TZ)) ) assert ( get_next_market_close("metal", FX_METAL_OPEN_WED_2023_6_21_22) - == format_datetime_to_utc_iso_string(datetime.datetime(2023, 6, 23, 17, 0, 0, tzinfo=NY_TZ)) + == format_datetime_to_unix_timestamp(datetime.datetime(2023, 6, 23, 17, 0, 0, tzinfo=NY_TZ)) ) # fx & metal out of market hours assert ( get_next_market_close("fx", FX_METAL_CLOSE_SUN_2023_6_18_16) - == format_datetime_to_utc_iso_string(datetime.datetime(2023, 6, 23, 17, 0, 0, tzinfo=NY_TZ)) + == format_datetime_to_unix_timestamp(datetime.datetime(2023, 6, 23, 17, 0, 0, tzinfo=NY_TZ)) ) assert ( get_next_market_close("metal", FX_METAL_CLOSE_SUN_2023_6_18_16) - == format_datetime_to_utc_iso_string(datetime.datetime(2023, 6, 23, 17, 0, 0, tzinfo=NY_TZ)) + == format_datetime_to_unix_timestamp(datetime.datetime(2023, 6, 23, 17, 0, 0, tzinfo=NY_TZ)) ) # fx & metal holiday assert ( get_next_market_close("fx", FX_METAL_HOLIDAY_SUN_2023_1_1) - == format_datetime_to_utc_iso_string(datetime.datetime(2023, 1, 6, 17, 0, 0, tzinfo=NY_TZ)) + == format_datetime_to_unix_timestamp(datetime.datetime(2023, 1, 6, 17, 0, 0, tzinfo=NY_TZ)) ) assert ( get_next_market_close("metal", FX_METAL_HOLIDAY_SUN_2023_1_1) - == format_datetime_to_utc_iso_string(datetime.datetime(2023, 1, 6, 17, 0, 0, tzinfo=NY_TZ)) + == format_datetime_to_unix_timestamp(datetime.datetime(2023, 1, 6, 17, 0, 0, tzinfo=NY_TZ)) ) # crypto