Skip to content

Commit 6bb10e2

Browse files
authored
return market hours in timestamp instead of iso string (#36)
1 parent 23fedd2 commit 6bb10e2

File tree

3 files changed

+36
-36
lines changed

3 files changed

+36
-36
lines changed

pythclient/calendar.py

+5-9
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def is_market_open(asset_type: str, dt: datetime.datetime) -> bool:
7474
return True
7575

7676

77-
def get_next_market_open(asset_type: str, dt: datetime.datetime) -> str:
77+
def get_next_market_open(asset_type: str, dt: datetime.datetime) -> int:
7878
# make sure time is in NY timezone
7979
dt = dt.astimezone(NY_TZ)
8080
time = dt.time()
@@ -119,10 +119,10 @@ def get_next_market_open(asset_type: str, dt: datetime.datetime) -> str:
119119
while not is_market_open(asset_type, next_market_open):
120120
next_market_open += datetime.timedelta(days=1)
121121

122-
return next_market_open.astimezone(UTC_TZ).strftime("%Y-%m-%dT%H:%M:%S") + "Z"
122+
return int(next_market_open.timestamp())
123123

124124

125-
def get_next_market_close(asset_type: str, dt: datetime.datetime) -> str:
125+
def get_next_market_close(asset_type: str, dt: datetime.datetime) -> int:
126126
# make sure time is in NY timezone
127127
dt = dt.astimezone(NY_TZ)
128128
time = dt.time()
@@ -148,16 +148,12 @@ def get_next_market_close(asset_type: str, dt: datetime.datetime) -> str:
148148
next_market_open = get_next_market_open(
149149
asset_type, dt + datetime.timedelta(days=1)
150150
)
151-
next_market_close = (
152-
datetime.datetime.fromisoformat(next_market_open.replace("Z", "+00:00"))
153-
.astimezone(NY_TZ)
154-
.replace(
151+
next_market_close = datetime.datetime.fromtimestamp(next_market_open).astimezone(NY_TZ).replace(
155152
hour=EQUITY_CLOSE.hour,
156153
minute=EQUITY_CLOSE.minute,
157154
second=0,
158155
microsecond=0,
159156
)
160-
)
161157
else:
162158
next_market_close = dt.replace(
163159
hour=EQUITY_CLOSE.hour,
@@ -189,4 +185,4 @@ def get_next_market_close(asset_type: str, dt: datetime.datetime) -> str:
189185
else: # crypto markets never close
190186
return None
191187

192-
return next_market_close.astimezone(UTC_TZ).strftime("%Y-%m-%dT%H:%M:%S") + "Z"
188+
return int(next_market_close.timestamp())

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
setup(
99
name='pythclient',
10-
version='0.1.8',
10+
version='0.1.9',
1111
packages=['pythclient'],
1212
author='Pyth Developers',
1313
author_email='[email protected]',

tests/test_calendar.py

+30-26
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,12 @@
2424
CRYPTO_OPEN_WED_2023_6_21_12 = datetime.datetime(2023, 6, 21, 12, 0, 0, tzinfo=NY_TZ)
2525
CRYPTO_OPEN_SUN_2023_6_18_12 = datetime.datetime(2023, 6, 18, 12, 0, 0, tzinfo=NY_TZ)
2626

27-
def format_datetime_to_utc_iso_string(dt: datetime.datetime):
28-
return dt.astimezone(UTC_TZ).strftime("%Y-%m-%dT%H:%M:%S") + "Z"
27+
28+
def format_datetime_to_unix_timestamp(dt: datetime.datetime):
29+
# Convert the datetime object to a Unix timestamp in UTC
30+
timestamp = dt.astimezone(UTC_TZ).timestamp()
31+
unix_timestamp_utc = int(timestamp)
32+
return unix_timestamp_utc
2933

3034
def test_is_market_open():
3135
# equity
@@ -67,65 +71,65 @@ def test_get_next_market_open():
6771
# equity within market hours
6872
assert (
6973
get_next_market_open("equity", EQUITY_OPEN_WED_2023_6_21_12)
70-
== format_datetime_to_utc_iso_string(datetime.datetime(2023, 6, 22, 9, 30, 0, tzinfo=NY_TZ))
74+
== format_datetime_to_unix_timestamp(datetime.datetime(2023, 6, 22, 9, 30, 0, tzinfo=NY_TZ))
7175
)
7276

7377
# equity out of market hours
7478
assert (
7579
get_next_market_open("equity", EQUITY_CLOSE_WED_2023_6_21_17)
76-
== format_datetime_to_utc_iso_string(datetime.datetime(2023, 6, 22, 9, 30, 0, tzinfo=NY_TZ))
80+
== format_datetime_to_unix_timestamp(datetime.datetime(2023, 6, 22, 9, 30, 0, tzinfo=NY_TZ))
7781
)
7882

7983
# equity weekend
8084
assert (
8185
get_next_market_open("equity", EQUITY_CLOSE_SAT_2023_6_10_17)
82-
== format_datetime_to_utc_iso_string(datetime.datetime(2023, 6, 12, 9, 30, 0, tzinfo=NY_TZ))
86+
== format_datetime_to_unix_timestamp(datetime.datetime(2023, 6, 12, 9, 30, 0, tzinfo=NY_TZ))
8387
)
8488

8589
# equity holiday
8690
assert (
8791
get_next_market_open("equity", EQUITY_HOLIDAY_MON_2023_6_19)
88-
== format_datetime_to_utc_iso_string(datetime.datetime(2023, 6, 20, 9, 30, 0, tzinfo=NY_TZ))
92+
== format_datetime_to_unix_timestamp(datetime.datetime(2023, 6, 20, 9, 30, 0, tzinfo=NY_TZ))
8993
)
9094

9195
# equity early close holiday
9296
assert (
9397
get_next_market_open("equity", EQUITY_EARLY_CLOSE_OPEN_FRI_2023_11_24_14)
94-
== format_datetime_to_utc_iso_string(datetime.datetime(2023, 11, 27, 9, 30, 0, tzinfo=NY_TZ))
98+
== format_datetime_to_unix_timestamp(datetime.datetime(2023, 11, 27, 9, 30, 0, tzinfo=NY_TZ))
9599
)
96100
assert (
97101
get_next_market_open("equity", EQUITY_EARLY_CLOSE_CLOSE_FRI_2023_11_24_14)
98-
== format_datetime_to_utc_iso_string(datetime.datetime(2023, 11, 27, 9, 30, 0, tzinfo=NY_TZ))
102+
== format_datetime_to_unix_timestamp(datetime.datetime(2023, 11, 27, 9, 30, 0, tzinfo=NY_TZ))
99103
)
100104

101105
# fx & metal within market hours
102106
assert (
103107
get_next_market_open("fx", FX_METAL_OPEN_WED_2023_6_21_22)
104-
== format_datetime_to_utc_iso_string(datetime.datetime(2023, 6, 25, 17, 0, 0, tzinfo=NY_TZ))
108+
== format_datetime_to_unix_timestamp(datetime.datetime(2023, 6, 25, 17, 0, 0, tzinfo=NY_TZ))
105109
)
106110
assert (
107111
get_next_market_open("metal", FX_METAL_OPEN_WED_2023_6_21_22)
108-
== format_datetime_to_utc_iso_string(datetime.datetime(2023, 6, 25, 17, 0, 0, tzinfo=NY_TZ))
112+
== format_datetime_to_unix_timestamp(datetime.datetime(2023, 6, 25, 17, 0, 0, tzinfo=NY_TZ))
109113
)
110114

111115
# fx & metal out of market hours
112116
assert (
113117
get_next_market_open("fx", FX_METAL_CLOSE_SUN_2023_6_18_16)
114-
== format_datetime_to_utc_iso_string(datetime.datetime(2023, 6, 18, 17, 0, 0, tzinfo=NY_TZ))
118+
== format_datetime_to_unix_timestamp(datetime.datetime(2023, 6, 18, 17, 0, 0, tzinfo=NY_TZ))
115119
)
116120
assert (
117121
get_next_market_open("metal", FX_METAL_CLOSE_SUN_2023_6_18_16)
118-
== format_datetime_to_utc_iso_string(datetime.datetime(2023, 6, 18, 17, 0, 0, tzinfo=NY_TZ))
122+
== format_datetime_to_unix_timestamp(datetime.datetime(2023, 6, 18, 17, 0, 0, tzinfo=NY_TZ))
119123
)
120124

121125
# fx & metal holiday
122126
assert (
123127
get_next_market_open("fx", FX_METAL_HOLIDAY_SUN_2023_1_1)
124-
== format_datetime_to_utc_iso_string(datetime.datetime(2023, 1, 2, 17, 0, 0, tzinfo=NY_TZ))
128+
== format_datetime_to_unix_timestamp(datetime.datetime(2023, 1, 2, 17, 0, 0, tzinfo=NY_TZ))
125129
)
126130
assert (
127131
get_next_market_open("metal", FX_METAL_HOLIDAY_SUN_2023_1_1)
128-
== format_datetime_to_utc_iso_string(datetime.datetime(2023, 1, 2, 17, 0, 0, tzinfo=NY_TZ))
132+
== format_datetime_to_unix_timestamp(datetime.datetime(2023, 1, 2, 17, 0, 0, tzinfo=NY_TZ))
129133
)
130134

131135
# crypto
@@ -137,65 +141,65 @@ def test_get_next_market_close():
137141
# equity within market hours
138142
assert (
139143
get_next_market_close("equity", EQUITY_OPEN_WED_2023_6_21_12)
140-
== format_datetime_to_utc_iso_string(datetime.datetime(2023, 6, 21, 16, 0, 0, tzinfo=NY_TZ))
144+
== format_datetime_to_unix_timestamp(datetime.datetime(2023, 6, 21, 16, 0, 0, tzinfo=NY_TZ))
141145
)
142146

143147
# equity out of market hours
144148
assert (
145149
get_next_market_close("equity", EQUITY_CLOSE_WED_2023_6_21_17)
146-
== format_datetime_to_utc_iso_string(datetime.datetime(2023, 6, 22, 16, 0, 0, tzinfo=NY_TZ))
150+
== format_datetime_to_unix_timestamp(datetime.datetime(2023, 6, 22, 16, 0, 0, tzinfo=NY_TZ))
147151
)
148152

149153
# equity weekend
150154
assert (
151155
get_next_market_close("equity", EQUITY_CLOSE_SAT_2023_6_10_17)
152-
== format_datetime_to_utc_iso_string(datetime.datetime(2023, 6, 12, 16, 0, 0, tzinfo=NY_TZ))
156+
== format_datetime_to_unix_timestamp(datetime.datetime(2023, 6, 12, 16, 0, 0, tzinfo=NY_TZ))
153157
)
154158

155159
# equity holiday
156160
assert (
157161
get_next_market_close("equity", EQUITY_HOLIDAY_MON_2023_6_19)
158-
== format_datetime_to_utc_iso_string(datetime.datetime(2023, 6, 20, 16, 0, 0, tzinfo=NY_TZ))
162+
== format_datetime_to_unix_timestamp(datetime.datetime(2023, 6, 20, 16, 0, 0, tzinfo=NY_TZ))
159163
)
160164

161165
# equity early close holiday
162166
assert (
163167
get_next_market_close("equity", EQUITY_EARLY_CLOSE_OPEN_FRI_2023_11_24_14)
164-
== format_datetime_to_utc_iso_string(datetime.datetime(2023, 11, 24, 13, 0, 0, tzinfo=NY_TZ))
168+
== format_datetime_to_unix_timestamp(datetime.datetime(2023, 11, 24, 13, 0, 0, tzinfo=NY_TZ))
165169
)
166170
assert (
167171
get_next_market_close("equity", EQUITY_EARLY_CLOSE_CLOSE_FRI_2023_11_24_14)
168-
== format_datetime_to_utc_iso_string(datetime.datetime(2023, 11, 27, 16, 0, 0, tzinfo=NY_TZ))
172+
== format_datetime_to_unix_timestamp(datetime.datetime(2023, 11, 27, 16, 0, 0, tzinfo=NY_TZ))
169173
)
170174

171175
# fx & metal within market hours
172176
assert (
173177
get_next_market_close("fx", FX_METAL_OPEN_WED_2023_6_21_22)
174-
== format_datetime_to_utc_iso_string(datetime.datetime(2023, 6, 23, 17, 0, 0, tzinfo=NY_TZ))
178+
== format_datetime_to_unix_timestamp(datetime.datetime(2023, 6, 23, 17, 0, 0, tzinfo=NY_TZ))
175179
)
176180
assert (
177181
get_next_market_close("metal", FX_METAL_OPEN_WED_2023_6_21_22)
178-
== format_datetime_to_utc_iso_string(datetime.datetime(2023, 6, 23, 17, 0, 0, tzinfo=NY_TZ))
182+
== format_datetime_to_unix_timestamp(datetime.datetime(2023, 6, 23, 17, 0, 0, tzinfo=NY_TZ))
179183
)
180184

181185
# fx & metal out of market hours
182186
assert (
183187
get_next_market_close("fx", FX_METAL_CLOSE_SUN_2023_6_18_16)
184-
== format_datetime_to_utc_iso_string(datetime.datetime(2023, 6, 23, 17, 0, 0, tzinfo=NY_TZ))
188+
== format_datetime_to_unix_timestamp(datetime.datetime(2023, 6, 23, 17, 0, 0, tzinfo=NY_TZ))
185189
)
186190
assert (
187191
get_next_market_close("metal", FX_METAL_CLOSE_SUN_2023_6_18_16)
188-
== format_datetime_to_utc_iso_string(datetime.datetime(2023, 6, 23, 17, 0, 0, tzinfo=NY_TZ))
192+
== format_datetime_to_unix_timestamp(datetime.datetime(2023, 6, 23, 17, 0, 0, tzinfo=NY_TZ))
189193
)
190194

191195
# fx & metal holiday
192196
assert (
193197
get_next_market_close("fx", FX_METAL_HOLIDAY_SUN_2023_1_1)
194-
== format_datetime_to_utc_iso_string(datetime.datetime(2023, 1, 6, 17, 0, 0, tzinfo=NY_TZ))
198+
== format_datetime_to_unix_timestamp(datetime.datetime(2023, 1, 6, 17, 0, 0, tzinfo=NY_TZ))
195199
)
196200
assert (
197201
get_next_market_close("metal", FX_METAL_HOLIDAY_SUN_2023_1_1)
198-
== format_datetime_to_utc_iso_string(datetime.datetime(2023, 1, 6, 17, 0, 0, tzinfo=NY_TZ))
202+
== format_datetime_to_unix_timestamp(datetime.datetime(2023, 1, 6, 17, 0, 0, tzinfo=NY_TZ))
199203
)
200204

201205
# crypto

0 commit comments

Comments
 (0)