Quellcode durchsuchen

操作日志,显示时间时区问题

lhq vor 4 Jahren
Ursprung
Commit
3ad8fb0997
1 geänderte Dateien mit 6 neuen und 3 gelöschten Zeilen
  1. 6 3
      Service/OperatingLogs.py

+ 6 - 3
Service/OperatingLogs.py

@@ -46,8 +46,11 @@ def addLog(request, status_code):
             if request_path in api_list:
                 user = MiscellService.get_access_name(request_dict=request_dict)
                 clientIP = CommonService.get_ip_address(request)
-                now_time = datetime.datetime.utcnow().replace(tzinfo=utc).astimezone(utc)
+                # now_time = datetime.datetime.utcnow().replace(tzinfo=utc).astimezone(utc)
                 nowtime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
+                beijing = datetime.timezone(datetime.timedelta(hours=8))
+                utc = datetime.timezone.utc  # 获取 UTC 的时区对象
+                beijing_time = datetime.datetime.utcnow().replace(tzinfo=utc).astimezone(beijing)  # 强制转换加上 UTC 时区。此处敲黑板,需要特别注意。
                 # content:从前端获取到的参数数据,对获取到的数据进行处理
                 content = json.dumps(request_dict)
                 d = eval(content)
@@ -71,7 +74,7 @@ def addLog(request, status_code):
                     print('空')
                     user = '空'
                 # 数据拼接
-                caozuo = '用户账号为:[' + user + ']' + '在' + str(nowtime) + '进行' + str(jiekou[request_path])
+                caozuo = '用户账号为:[' + user + ']' + '在' + str(beijing_time) + '进行' + str(jiekou[request_path])
                 # + ',' + '设备uid或主键id为:' + str(d['uid']) + str(d['equipmentid'])
                 print('request_dict:', d)
                 add_data = {
@@ -80,7 +83,7 @@ def addLog(request, status_code):
                     'equipmentid': d['equipmentid'],
                     'NickName': d['NickName'],
                     'ip': clientIP,
-                    'time': now_time,
+                    'time': beijing_time,
                     'url': request.path,
                     'operatingcontent': caozuo,
                 }