24
24
CRYPTO_OPEN_WED_2023_6_21_12 = datetime .datetime (2023 , 6 , 21 , 12 , 0 , 0 , tzinfo = NY_TZ )
25
25
CRYPTO_OPEN_SUN_2023_6_18_12 = datetime .datetime (2023 , 6 , 18 , 12 , 0 , 0 , tzinfo = NY_TZ )
26
26
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
29
33
30
34
def test_is_market_open ():
31
35
# equity
@@ -67,65 +71,65 @@ def test_get_next_market_open():
67
71
# equity within market hours
68
72
assert (
69
73
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 ))
71
75
)
72
76
73
77
# equity out of market hours
74
78
assert (
75
79
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 ))
77
81
)
78
82
79
83
# equity weekend
80
84
assert (
81
85
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 ))
83
87
)
84
88
85
89
# equity holiday
86
90
assert (
87
91
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 ))
89
93
)
90
94
91
95
# equity early close holiday
92
96
assert (
93
97
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 ))
95
99
)
96
100
assert (
97
101
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 ))
99
103
)
100
104
101
105
# fx & metal within market hours
102
106
assert (
103
107
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 ))
105
109
)
106
110
assert (
107
111
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 ))
109
113
)
110
114
111
115
# fx & metal out of market hours
112
116
assert (
113
117
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 ))
115
119
)
116
120
assert (
117
121
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 ))
119
123
)
120
124
121
125
# fx & metal holiday
122
126
assert (
123
127
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 ))
125
129
)
126
130
assert (
127
131
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 ))
129
133
)
130
134
131
135
# crypto
@@ -137,65 +141,65 @@ def test_get_next_market_close():
137
141
# equity within market hours
138
142
assert (
139
143
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 ))
141
145
)
142
146
143
147
# equity out of market hours
144
148
assert (
145
149
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 ))
147
151
)
148
152
149
153
# equity weekend
150
154
assert (
151
155
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 ))
153
157
)
154
158
155
159
# equity holiday
156
160
assert (
157
161
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 ))
159
163
)
160
164
161
165
# equity early close holiday
162
166
assert (
163
167
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 ))
165
169
)
166
170
assert (
167
171
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 ))
169
173
)
170
174
171
175
# fx & metal within market hours
172
176
assert (
173
177
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 ))
175
179
)
176
180
assert (
177
181
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 ))
179
183
)
180
184
181
185
# fx & metal out of market hours
182
186
assert (
183
187
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 ))
185
189
)
186
190
assert (
187
191
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 ))
189
193
)
190
194
191
195
# fx & metal holiday
192
196
assert (
193
197
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 ))
195
199
)
196
200
assert (
197
201
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 ))
199
203
)
200
204
201
205
# crypto
0 commit comments