Quellcode durchsuchen

fix(时区处理): 统一使用上海时区显示和存储时间戳

在前后端统一处理时间戳时区问题,确保所有时间显示和存储都使用Asia/Shanghai时区
skyfffire vor 1 Woche
Ursprung
Commit
44988350e0
2 geänderte Dateien mit 25 neuen und 6 gelöschten Zeilen
  1. 17 2
      src/dashboard/app.py
  2. 8 4
      src/dashboard/static/index.html

+ 17 - 2
src/dashboard/app.py

@@ -13,6 +13,7 @@ from psycopg.rows import dict_row
 from datetime import datetime, timedelta
 import logging
 import os
+import pytz
 
 # 配置日志
 logging.basicConfig(level=logging.INFO)
@@ -119,9 +120,16 @@ def get_symbol_data(symbol):
         
         # 转换为JSON格式
         data = []
+        shanghai_tz = pytz.timezone('Asia/Shanghai')
         for row in rows:
+            # 将时间戳转换为东8区时间
+            timestamp = row["timestamp"]
+            if timestamp and timestamp.tzinfo is None:
+                timestamp = pytz.utc.localize(timestamp)
+            if timestamp:
+                timestamp = timestamp.astimezone(shanghai_tz)
             data.append({
-                "timestamp": row["timestamp"].isoformat() if row["timestamp"] else None,
+                "timestamp": timestamp.isoformat() if timestamp else None,
                 "binance_mark_price": float(row["binance_mark_price"]) if row["binance_mark_price"] else None,
                 "binance_price": float(row["binance_price"]) if row["binance_price"] else None,
                 "lighter_mark_price": float(row["lighter_mark_price"]) if row["lighter_mark_price"] else None,
@@ -182,9 +190,16 @@ def get_latest_data(symbol):
         if not row:
             return jsonify({"error": "没有找到数据"}), 404
         
+        shanghai_tz = pytz.timezone('Asia/Shanghai')
+        timestamp = row["timestamp"]
+        if timestamp and timestamp.tzinfo is None:
+            timestamp = pytz.utc.localize(timestamp)
+        if timestamp:
+            timestamp = timestamp.astimezone(shanghai_tz)
+        
         data = {
             "symbol": symbol,
-            "timestamp": row["timestamp"].isoformat() if row["timestamp"] else None,
+            "timestamp": timestamp.isoformat() if timestamp else None,
             "binance_mark_price": float(row["binance_mark_price"]) if row["binance_mark_price"] else None,
             "binance_price": float(row["binance_price"]) if row["binance_price"] else None,
             "lighter_mark_price": float(row["lighter_mark_price"]) if row["lighter_mark_price"] else None,

+ 8 - 4
src/dashboard/static/index.html

@@ -394,7 +394,10 @@
                 return;
             }
 
-            const labels = data.map(item => new Date(item.timestamp).toLocaleTimeString());
+            const labels = data.map(item => {
+                const date = new Date(item.timestamp);
+                return date.toLocaleTimeString('zh-CN', { timeZone: 'Asia/Shanghai' });
+            });
             
             // 价格对比图
             updatePriceChart(labels, data, symbol);
@@ -478,7 +481,7 @@
                                 title: function(context) {
                                     const dataIndex = context[0].dataIndex;
                                     const timestamp = data[dataIndex].timestamp;
-                                    return new Date(timestamp).toLocaleString();
+                                    return new Date(timestamp).toLocaleString('zh-CN', { timeZone: 'Asia/Shanghai' });
                                 },
                                 label: function(context) {
                                     const value = context.parsed.y;
@@ -574,7 +577,7 @@
                                 title: function(context) {
                                     const dataIndex = context[0].dataIndex;
                                     const timestamp = data[dataIndex].timestamp;
-                                    return new Date(timestamp).toLocaleString();
+                                    return new Date(timestamp).toLocaleString('zh-CN', { timeZone: 'Asia/Shanghai' });
                                 },
                                 label: function(context) {
                                     const value = context.parsed.y;
@@ -647,7 +650,8 @@
 
         function updateLastUpdateTime() {
             const now = new Date();
-            document.getElementById('lastUpdate').textContent = `最后更新: ${now.toLocaleString()}`;
+            const shanghaiTime = now.toLocaleString('zh-CN', { timeZone: 'Asia/Shanghai' });
+            document.getElementById('lastUpdate').textContent = `最后更新: ${shanghaiTime}`;
         }
     </script>
 </body>