22 lines
667 B
Python
22 lines
667 B
Python
|
|
from datetime import datetime, time
|
||
|
|
from zoneinfo import ZoneInfo
|
||
|
|
|
||
|
|
from app.api.schemas import MarketState
|
||
|
|
|
||
|
|
|
||
|
|
def get_market_state(now: datetime | None = None) -> MarketState:
|
||
|
|
current = now or datetime.now(ZoneInfo("Asia/Shanghai"))
|
||
|
|
current_time = current.time()
|
||
|
|
|
||
|
|
if current_time < time(9, 30):
|
||
|
|
return "pre_open"
|
||
|
|
if time(9, 30) <= current_time < time(12, 0):
|
||
|
|
return "trading_am"
|
||
|
|
if time(12, 0) <= current_time < time(13, 0):
|
||
|
|
return "midday_break"
|
||
|
|
if time(13, 0) <= current_time < time(16, 0):
|
||
|
|
return "trading_pm"
|
||
|
|
if time(16, 0) <= current_time < time(16, 10):
|
||
|
|
return "finalizing"
|
||
|
|
return "closed"
|