import datetime import decimal import time from threading import Thread def async_call(fn): def wrapper(*args, **kwargs): Thread(target=fn, args=args, kwargs=kwargs).start() return wrapper # 获取涨停价 def get_limit_up_price(price, max_rate=0.1): price = decimal.Decimal(str(price)) * decimal.Decimal(f"{round(1 + max_rate, 1)}") price = price.quantize(decimal.Decimal("0.00"), decimal.ROUND_HALF_UP) return price # 获取跌停价 def get_limit_down_price(price): return decimal.Decimal(str(price)) * decimal.Decimal("0.9").quantize(decimal.Decimal("0.00"), decimal.ROUND_HALF_UP) def get_time_as_second(time_str): ts = time_str.split(":") return int(ts[0]) * 3600 + int(ts[1]) * 60 + int(ts[2]) def trade_time_sub(time_str_1, time_str_2): split_time = get_time_as_second("11:30:00") time_1 = get_time_as_second(time_str_1) time_2 = get_time_as_second(time_str_2) if time_2 < split_time < time_1: time_2 += 90 * 60 else: if time_1 < split_time < time_2: time_2 -= 90 * 60 return time_1 - time_2 # 将秒数格式化为时间 def time_seconds_format(seconds): h = seconds // 3600 m = seconds % 3600 // 60 s = seconds % 60 return "{0:0>2}:{1:0>2}:{2:0>2}".format(h, m, s) # 交易时间加几s def trade_time_add_second(time_str, second): ts = time_str.split(":") s_ = int(ts[0]) * 3600 + int(ts[1]) * 60 + int(ts[2]) s = s_ + second # 结果在11:30到1点之间 if 13 * 3600 > s >= 11 * 3600 + 30 * 60: if second > 0: s += 90 * 60 else: s -= 90 * 60 return time_seconds_format(s) def is_trade_time(time_str=None): if time_str is None: time_str = get_now_time_str() if (int("092500") <= int(time_str.replace(":", "")) <= int("113000")) or ( int("130000") <= int(time_str.replace(":", "")) <= int("150000")): return True return False def get_now_time_str(): return datetime.datetime.now().strftime("%H:%M:%S") # return "10:20:00" def get_now_date_str(format="%Y-%m-%d"): return datetime.datetime.now().strftime(format) def money_desc(money): if abs(money) > 100000000: return f"{round(money / 100000000, 2)}亿" else: return f"{round(money / 10000, 2)}万" def time_format(timestamp): if timestamp: return time.strftime("%H:%M:%S", time.localtime(int(timestamp))) return "" if __name__ == "__main__": print(trade_time_sub("10:10:56", "11:30:00")) print(trade_time_sub("13:00:00", "11:29:00")) print(trade_time_sub("11:29:00", "13:00:00"))