2015-10-16 08:50:44 +00:00
|
|
|
|
# encoding: UTF-8
|
|
|
|
|
|
|
|
|
|
"""
|
2016-10-28 14:55:46 +00:00
|
|
|
|
包含一些开发中常用的函数
|
2015-10-16 08:50:44 +00:00
|
|
|
|
"""
|
|
|
|
|
|
2017-04-26 14:47:59 +00:00
|
|
|
|
import vtGlobal
|
2016-10-28 14:55:46 +00:00
|
|
|
|
import os
|
2015-10-16 08:50:44 +00:00
|
|
|
|
import decimal
|
2016-04-28 15:03:36 +00:00
|
|
|
|
import json
|
2017-04-26 14:47:59 +00:00
|
|
|
|
import time
|
|
|
|
|
import datetime as dt
|
2016-05-05 13:52:11 +00:00
|
|
|
|
from datetime import datetime
|
2015-10-16 08:50:44 +00:00
|
|
|
|
|
2016-04-02 09:04:58 +00:00
|
|
|
|
MAX_NUMBER = 10000000000000
|
2015-10-16 08:50:44 +00:00
|
|
|
|
MAX_DECIMAL = 4
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def safeUnicode(value):
|
|
|
|
|
"""检查接口数据潜在的错误,保证转化为的字符串正确"""
|
|
|
|
|
# 检查是数字接近0时会出现的浮点数上限
|
|
|
|
|
if type(value) is int or type(value) is float:
|
|
|
|
|
if value > MAX_NUMBER:
|
|
|
|
|
value = 0
|
|
|
|
|
|
|
|
|
|
# 检查防止小数点位过多
|
|
|
|
|
if type(value) is float:
|
|
|
|
|
d = decimal.Decimal(str(value))
|
|
|
|
|
if abs(d.as_tuple().exponent) > MAX_DECIMAL:
|
|
|
|
|
value = round(value, ndigits=MAX_DECIMAL)
|
|
|
|
|
|
2016-04-20 15:14:21 +00:00
|
|
|
|
return unicode(value)
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def loadMongoSetting():
|
|
|
|
|
"""载入MongoDB数据库的配置"""
|
2017-04-26 14:47:59 +00:00
|
|
|
|
# fileName = 'VT_setting.json'
|
|
|
|
|
# path = os.path.abspath(os.path.dirname(__file__))
|
|
|
|
|
# fileName = os.path.join(path, fileName)
|
|
|
|
|
setting = vtGlobal.VT_setting.copy()
|
|
|
|
|
|
2016-04-20 15:14:21 +00:00
|
|
|
|
try:
|
2017-04-26 14:47:59 +00:00
|
|
|
|
# f = file(fileName)
|
|
|
|
|
# setting = json.load(f)
|
2016-04-20 15:14:21 +00:00
|
|
|
|
host = setting['mongoHost']
|
|
|
|
|
port = setting['mongoPort']
|
2017-01-05 14:57:08 +00:00
|
|
|
|
logging = setting['mongoLogging']
|
2016-04-20 15:14:21 +00:00
|
|
|
|
except:
|
|
|
|
|
host = 'localhost'
|
|
|
|
|
port = 27017
|
2017-01-05 14:57:08 +00:00
|
|
|
|
logging = False
|
2016-04-20 15:14:21 +00:00
|
|
|
|
|
2017-01-05 14:57:08 +00:00
|
|
|
|
return host, port, logging
|
2016-04-20 15:14:21 +00:00
|
|
|
|
|
2016-05-05 13:52:11 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def todayDate():
|
|
|
|
|
"""获取当前本机电脑时间的日期"""
|
|
|
|
|
return datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
|
|
|
|
|
2016-04-28 23:47:29 +00:00
|
|
|
|
|
2017-04-26 14:47:59 +00:00
|
|
|
|
def autoshutdown(clocks=None):
|
|
|
|
|
"""
|
|
|
|
|
根据当前启动时间,设置自动关闭时间
|
|
|
|
|
2:31 之前启动服务器,那么在2:31 会自动关闭;
|
|
|
|
|
2:31 ~ 8:39 之间启动,会在8:39关闭,以此类推;
|
|
|
|
|
20:39 之后关闭,会在次日 2:31 自动关闭
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
>>> autoshutdown().closeTime == todayDate().replace(hour=15, minute=39)
|
|
|
|
|
True
|
|
|
|
|
|
|
|
|
|
:return:
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
clocks = clocks or [
|
|
|
|
|
dt.time(2, 31), # 夜盘后关闭
|
|
|
|
|
dt.time(8, 39), # 日盘前关闭
|
|
|
|
|
dt.time(15, 39), # 日盘后关闭
|
|
|
|
|
dt.time(20, 39), # 夜盘前关闭
|
|
|
|
|
]
|
|
|
|
|
from threading import Thread
|
|
|
|
|
|
|
|
|
|
now = datetime.now()
|
|
|
|
|
today = todayDate()
|
|
|
|
|
tomorrow = today + dt.timedelta(days=1)
|
|
|
|
|
|
|
|
|
|
for t in clocks:
|
|
|
|
|
if now.time() < t:
|
|
|
|
|
# 关闭时间在当日
|
|
|
|
|
closeTime = datetime.combine(today, t)
|
|
|
|
|
break
|
|
|
|
|
else:
|
|
|
|
|
# 关闭时间在次日
|
|
|
|
|
closeTime = datetime.combine(tomorrow, clocks[0])
|
|
|
|
|
|
|
|
|
|
def shutdown(closeTime):
|
|
|
|
|
# 子线程阻塞直到到达关闭时间
|
|
|
|
|
while datetime.now() < closeTime:
|
|
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
|
|
t = Thread(target=shutdown, args=(closeTime,))
|
|
|
|
|
t.closeTime = closeTime
|
|
|
|
|
t.start()
|
|
|
|
|
return t
|
|
|
|
|
|