Skip to content

Commit

Permalink
fix: 安卓环境Go Time 固定UTC时区,通过时区获取偏移量修正时区 (#1284)
Browse files Browse the repository at this point in the history
* fix: 安卓环境Go Time 固定UTC时区,通过时区获取偏移量修正时区

* fix: 去除tZ参数,只有安卓环境情况下根据getprop persist.sys.timezone 修正时区

---------

Co-authored-by: luo.pengcheng <[email protected]>
  • Loading branch information
lpcheng1208 and luo.pengcheng authored Oct 17, 2024
1 parent e99ab31 commit 2c4d8a8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
6 changes: 6 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"os"
"os/exec"
"path/filepath"
"runtime"
"strconv"
"time"

Expand Down Expand Up @@ -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)
Expand Down
19 changes: 19 additions & 0 deletions util/andriod_time.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 2c4d8a8

Please sign in to comment.