Skip to content

Commit 49dc58a

Browse files
committed
更新mongo测试
1 parent 238d6f7 commit 49dc58a

1 file changed

Lines changed: 61 additions & 1 deletion

File tree

test/test_mongo.py

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,46 @@
66
sys.path.append('..')
77
from tools.mongo import Mongodb
88

9+
import time
10+
import calendar
11+
from datetime import datetime, timedelta, date
12+
13+
14+
def time_local_to_utc(local_time):
15+
"""
16+
本地时间转UTC时间
17+
:param local_time:
18+
:return:
19+
"""
20+
# 字符串处理
21+
if isinstance(local_time, str) and len(local_time) == 10:
22+
local_time = datetime.strptime(local_time, '%Y-%m-%d')
23+
elif isinstance(local_time, str) and len(local_time) >= 19:
24+
local_time = datetime.strptime(local_time[:19], '%Y-%m-%d %H:%M:%S')
25+
elif not (isinstance(local_time, datetime) or isinstance(local_time, date)):
26+
local_time = datetime.now()
27+
# 时间转换
28+
utc_time = local_time + timedelta(seconds=time.timezone)
29+
return utc_time
30+
31+
32+
def time_utc_to_local(utc_time):
33+
"""
34+
UTC时间转本地时间
35+
:param utc_time:
36+
:return:
37+
"""
38+
# 字符串处理
39+
if isinstance(utc_time, str) and len(utc_time) == 10:
40+
utc_time = datetime.strptime(utc_time, '%Y-%m-%d')
41+
elif isinstance(utc_time, str) and len(utc_time) >= 19:
42+
utc_time = datetime.strptime(utc_time[:19], '%Y-%m-%d %H:%M:%S')
43+
elif not (isinstance(utc_time, datetime) or isinstance(utc_time, date)):
44+
utc_time = datetime.utcnow()
45+
# 时间转换
46+
local_time = utc_time - timedelta(seconds=time.timezone)
47+
return local_time
48+
949

1050
db_config = {
1151
'host': 'localhost',
@@ -68,8 +108,28 @@ def test():
68108
print e
69109

70110

111+
def test_02():
112+
table_name = 'user'
113+
conn = Mongodb(db_config)
114+
print conn.db
115+
print conn.find_one(table_name)
116+
test_doc = {
117+
'_id': 1,
118+
'id': 1,
119+
'name': 'admin',
120+
'create_time': datetime.strptime('2017-07-07 08:00:00', '%Y-%m-%d %H:%M:%S'),
121+
'create_time_utc': time_local_to_utc('2017-07-07 08:00:00'),
122+
'create_time_str': '2017-07-07 08:00:00'
123+
}
124+
print conn.remove(table_name) # 清空记录
125+
print conn.insert(table_name, test_doc) # 插入记录
126+
print conn.find_one(table_name)
127+
conn.output_rows(table_name)
128+
129+
71130
if __name__ == '__main__':
72-
test()
131+
# test()
132+
test_02()
73133

74134

75135
"""

0 commit comments

Comments
 (0)