From 2c4d8a82f8539dad6244b11f0843afcee7b1aa39 Mon Sep 17 00:00:00 2001 From: luopc Date: Thu, 17 Oct 2024 18:25:24 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=AE=89=E5=8D=93=E7=8E=AF=E5=A2=83Go?= =?UTF-8?q?=20Time=20=E5=9B=BA=E5=AE=9AUTC=E6=97=B6=E5=8C=BA=EF=BC=8C?= =?UTF-8?q?=E9=80=9A=E8=BF=87=E6=97=B6=E5=8C=BA=E8=8E=B7=E5=8F=96=E5=81=8F?= =?UTF-8?q?=E7=A7=BB=E9=87=8F=E4=BF=AE=E6=AD=A3=E6=97=B6=E5=8C=BA=20(#1284?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: 安卓环境Go Time 固定UTC时区,通过时区获取偏移量修正时区 * fix: 去除tZ参数,只有安卓环境情况下根据getprop persist.sys.timezone 修正时区 --------- Co-authored-by: luo.pengcheng --- main.go | 6 ++++++ util/andriod_time.go | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 util/andriod_time.go diff --git a/main.go b/main.go index 22ccec3ff..b3fde96a6 100644 --- a/main.go +++ b/main.go @@ -11,6 +11,7 @@ import ( "os" "os/exec" "path/filepath" + "runtime" "strconv" "time" @@ -75,6 +76,11 @@ func main() { update.Self(version) return } + + // 安卓 go/src/time/zoneinfo_android.go 固定localLoc 为 UTC + if runtime.GOOS == "android" { + util.FixTimezone() + } // 检查监听地址 if _, err := net.ResolveTCPAddr("tcp", *listen); err != nil { log.Fatalf("Parse listen address failed! Exception: %s", err) diff --git a/util/andriod_time.go b/util/andriod_time.go new file mode 100644 index 000000000..1e0144ec1 --- /dev/null +++ b/util/andriod_time.go @@ -0,0 +1,19 @@ +package util + +import ( + "os/exec" + "strings" + "time" +) + +func FixTimezone() { + out, err := exec.Command("/system/bin/getprop", "persist.sys.timezone").Output() + if err != nil { + return + } + timeZone, err := time.LoadLocation(strings.TrimSpace(string(out))) + if err != nil { + return + } + time.Local = timeZone +}