From 65e2f09b97a16b757eaf9abaeea19e4600765bdb Mon Sep 17 00:00:00 2001 From: "Logan.Li" Date: Wed, 29 Jul 2026 05:57:07 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=8C=89=E4=B8=9A=E5=8A=A1=E6=97=B6?= =?UTF-8?q?=E5=8C=BA=E5=90=8C=E6=AD=A5=E6=98=A8=E6=97=A5=E8=B4=A6=E5=8D=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 固定按 Asia/Shanghai 计算账单昨日日期 - 补充 UTC 服务器时区回归测试 --- backend/pkg/billingsync/sync.go | 11 ++++++++++- backend/pkg/billingsync/sync_test.go | 14 ++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 backend/pkg/billingsync/sync_test.go diff --git a/backend/pkg/billingsync/sync.go b/backend/pkg/billingsync/sync.go index 72bc283..12f79bb 100644 --- a/backend/pkg/billingsync/sync.go +++ b/backend/pkg/billingsync/sync.go @@ -23,7 +23,16 @@ type Result struct { // Yesterday 昨日日期 YYYY-MM-DD func Yesterday() string { - return time.Now().AddDate(0, 0, -1).Format("2006-01-02") + return billingYesterdayAt(time.Now()) +} + +// billingYesterdayAt 按平台业务时区计算昨日,避免服务器 UTC 时区造成日期偏差。 // Logan.Li +func billingYesterdayAt(now time.Time) string { + location, err := time.LoadLocation("Asia/Shanghai") + if err != nil { + location = time.FixedZone("CST", 8*60*60) + } + return now.In(location).AddDate(0, 0, -1).Format("2006-01-02") } // SyncDate 同步指定日期账单(全部或单个活跃云账号) diff --git a/backend/pkg/billingsync/sync_test.go b/backend/pkg/billingsync/sync_test.go new file mode 100644 index 0000000..6da20ab --- /dev/null +++ b/backend/pkg/billingsync/sync_test.go @@ -0,0 +1,14 @@ +package billingsync + +import ( + "testing" + "time" +) + +// TestBillingYesterdayAt 验证 UTC 服务器按中国业务时区计算昨日。 // Logan.Li +func TestBillingYesterdayAt(t *testing.T) { + now := time.Date(2026, 7, 28, 21, 30, 0, 0, time.UTC) + if got := billingYesterdayAt(now); got != "2026-07-28" { + t.Fatalf("昨日日期错误: got=%s want=2026-07-28", got) + } +}