Browse Source

bugfix:修复时间显示错误,并修改时区为东八区

liufan 1 month ago
parent
commit
1b151b627f
1 changed files with 28 additions and 4 deletions
  1. 28 4
      server/Certificate_convert.go

+ 28 - 4
server/Certificate_convert.go

@@ -2,11 +2,12 @@ package server
 
 
 import (
 import (
 	cert "cfTest/cloudflareApi/certificate"
 	cert "cfTest/cloudflareApi/certificate"
+	"fmt"
 	"github.com/cloudflare/cloudflare-go"
 	"github.com/cloudflare/cloudflare-go"
+	"time"
 )
 )
 
 
 func Go2ProtoCertificate(goCertificate cloudflare.ZoneCustomSSL) *cert.ZoneCustomSSL {
 func Go2ProtoCertificate(goCertificate cloudflare.ZoneCustomSSL) *cert.ZoneCustomSSL {
-	format := "2024-12-02 15:04:05"
 	return &cert.ZoneCustomSSL{
 	return &cert.ZoneCustomSSL{
 		Id:           goCertificate.ID,
 		Id:           goCertificate.ID,
 		Hosts:        goCertificate.Hosts,
 		Hosts:        goCertificate.Hosts,
@@ -15,9 +16,32 @@ func Go2ProtoCertificate(goCertificate cloudflare.ZoneCustomSSL) *cert.ZoneCusto
 		Status:       goCertificate.Status,
 		Status:       goCertificate.Status,
 		BundleMethod: goCertificate.BundleMethod,
 		BundleMethod: goCertificate.BundleMethod,
 		ZoneId:       goCertificate.ZoneID,
 		ZoneId:       goCertificate.ZoneID,
-		UploadedOn:   goCertificate.UploadedOn.Format(format),
-		ModifiedOn:   goCertificate.ModifiedOn.Format(format),
-		ExpiresOn:    goCertificate.ExpiresOn.Format(format),
+		UploadedOn:   timeConvert(goCertificate.UploadedOn.String()),
+		ModifiedOn:   timeConvert(goCertificate.ModifiedOn.String()),
+		ExpiresOn:    timeConvert(goCertificate.ExpiresOn.String()),
 		Priority:     int32(goCertificate.Priority),
 		Priority:     int32(goCertificate.Priority),
 	}
 	}
 }
 }
+
+func timeConvert(input string) string {
+	// API返回的时间字符串
+
+	// 解析时间字符串,注意这里的布局字符串要与API返回的格式相匹配
+	layout := "2006-01-02 15:04:05 -0700 MST"
+	t, err := time.Parse(layout, input)
+	if err != nil {
+		fmt.Println("时间解析错误:", err)
+		return input
+	}
+
+	// 转换为东八区时区
+	location, err := time.LoadLocation("Asia/Shanghai")
+	if err != nil {
+		fmt.Println("时区加载错误:", err)
+		return input
+	}
+	t = t.In(location)
+	// 格式化时间为"2006-01-02 15:04:05"格式
+	formattedTime := t.Format("2006-01-02 15:04:05")
+	return formattedTime
+}