Browse Source

解决服务器时区问题

charles_c 5 months ago
parent
commit
0a5cf69d59
4 changed files with 41 additions and 21 deletions
  1. 1 0
      package.json
  2. 13 0
      pnpm-lock.yaml
  3. 15 21
      src/app/api/match/route.js
  4. 12 0
      yarn.lock

+ 1 - 0
package.json

@@ -18,6 +18,7 @@
     "date-fns": "^4.0.0-beta.1",
     "jsonwebtoken": "^9.0.2",
     "lucide-react": "^0.440.0",
+    "moment-timezone": "^0.5.46",
     "mongodb": "^6.9.0",
     "mongoose": "^8.6.2",
     "next": "14.2.10",

+ 13 - 0
pnpm-lock.yaml

@@ -20,6 +20,9 @@ dependencies:
   lucide-react:
     specifier: ^0.440.0
     version: 0.440.0(react@18.3.1)
+  moment-timezone:
+    specifier: ^0.5.46
+    version: 0.5.46
   mongodb:
     specifier: ^6.9.0
     version: 6.9.0
@@ -729,6 +732,16 @@ packages:
     engines: {node: '>=16 || 14 >=14.17'}
     dev: true
 
+  /moment-timezone@0.5.46:
+    resolution: {integrity: sha512-ZXm9b36esbe7OmdABqIWJuBBiLLwAjrN7CE+7sYdCCx82Nabt1wHDj8TVseS59QIlfFPbOoiBPm6ca9BioG4hw==}
+    dependencies:
+      moment: 2.30.1
+    dev: false
+
+  /moment@2.30.1:
+    resolution: {integrity: sha512-uEmtNhbDOrWPFS+hdjFCBfy9f2YoyzRpwcl+DqpC6taX21FzsTLQVbMV/W7PzNSX6x/bhC1zA3c2UQ5NzH6how==}
+    dev: false
+
   /mongodb-connection-string-url@3.0.1:
     resolution: {integrity: sha512-XqMGwRX0Lgn05TDB4PyG2h2kKO/FfWJyCzYQbIhXUxz7ETt0I/FqHjUeqj37irJ+Dl1ZtU82uYyj14u2XsZKfg==}
     dependencies:

+ 15 - 21
src/app/api/match/route.js

@@ -2,6 +2,10 @@ import dbConnect from "../../lib/dbConnect";
 import Match from "../../models/Match";
 import { NextResponse } from "next/server";
 import { setCORSHeaders, handleError } from "../../lib/apiUtils";
+import moment from "moment-timezone/moment-timezone-utils";
+
+// 假设你的服务器时区是 'Asia/Shanghai'
+const timezone = "Asia/Shanghai";
 
 export async function GET(request) {
   await dbConnect();
@@ -15,6 +19,10 @@ export async function GET(request) {
 
     let response;
 
+    const now = moment.tz(timezone);
+    const currentDate = now.toDate(); // 当前日期,包含时间
+    const currentTimeString = now.format("HH:mm"); // 当前时间字符串
+
     switch (action) {
       case "getMatches":
         const searchQuery = {};
@@ -57,11 +65,10 @@ export async function GET(request) {
         console.log("Search Query:", searchQuery);
 
         // 更新比赛状态
-        const now = new Date();
         await Match.updateMany(
           {
-            date: { $lt: now },
-            time: { $lt: now.toTimeString().slice(0, 5) },
+            date: { $lte: currentDate }, // 日期小于或等于当前日期
+            time: { $lte: currentTimeString }, // 时间小于或等于当前时间
             status: "未开始",
           },
           { $set: { status: "进行中" } }
@@ -82,13 +89,12 @@ export async function GET(request) {
         break;
 
       case "getMatchDays":
-        // 获取当前日期
-        const currentDate = new Date();
-        currentDate.setHours(0, 0, 0, 0);
+        // 获取当前日期(使用指定时区)
+        const currentDate = moment().tz(timezone).startOf("day");
 
         // 查询未来的未开始比赛
         const futureMatches = await Match.find({
-          date: { $gte: currentDate },
+          date: { $gte: currentDate.toDate() },
           status: "未开始",
         }).sort("date");
 
@@ -108,20 +114,8 @@ export async function GET(request) {
         }
 
         // 创建日期范围
-        const startDate = new Date(date);
-        startDate.setHours(0, 0, 0, 0);
-        const endDate = new Date(date);
-        endDate.setHours(23, 59, 59, 999);
-
-        // 更新比赛状态
-        await Match.updateMany(
-          {
-            date: { $lt: new Date() },
-            time: { $lt: new Date().toTimeString().slice(0, 5) },
-            status: "未开始",
-          },
-          { $set: { status: "进行中" } }
-        );
+        const startDate = moment.tz(date, timezone).startOf("day").toDate();
+        const endDate = moment.tz(date, timezone).endOf("day").toDate();
 
         const matches = await Match.find({
           date: { $gte: startDate, $lte: endDate },

+ 12 - 0
yarn.lock

@@ -659,6 +659,18 @@ minimatch@^9.0.4:
   resolved "https://registry.npmjs.org/minipass/-/minipass-7.1.2.tgz"
   integrity sha512-qOOzS1cBTWYF4BH8fVePDBOO9iptMnGUEZwNc/cMWnTV2nVLZ7VoNWEPHkYczZA0pdoA7dl6e7FL659nX9S2aw==
 
+moment-timezone@^0.5.46:
+  version "0.5.46"
+  resolved "https://registry.npmmirror.com/moment-timezone/-/moment-timezone-0.5.46.tgz#a21aa6392b3c6b3ed916cd5e95858a28d893704a"
+  integrity sha512-ZXm9b36esbe7OmdABqIWJuBBiLLwAjrN7CE+7sYdCCx82Nabt1wHDj8TVseS59QIlfFPbOoiBPm6ca9BioG4hw==
+  dependencies:
+    moment "^2.29.4"
+
+moment@^2.29.4:
+  version "2.30.1"
+  resolved "https://registry.npmmirror.com/moment/-/moment-2.30.1.tgz#f8c91c07b7a786e30c59926df530b4eac96974ae"
+  integrity sha512-uEmtNhbDOrWPFS+hdjFCBfy9f2YoyzRpwcl+DqpC6taX21FzsTLQVbMV/W7PzNSX6x/bhC1zA3c2UQ5NzH6how==
+
 mongodb-connection-string-url@^3.0.0:
   version "3.0.1"
   resolved "https://registry.npmjs.org/mongodb-connection-string-url/-/mongodb-connection-string-url-3.0.1.tgz"