Skip to content

Commit

Permalink
fix: tZ 参数不设置默认值, 根据环境获取时区字符串, 传入 tZ 则用自定义的时区
Browse files Browse the repository at this point in the history
  • Loading branch information
luo.pengcheng committed Oct 17, 2024
1 parent 7e5a969 commit 7596de5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ var customDNS = flag.String("dns", "", "Custom DNS server address, example: 8.8.
var newPassword = flag.String("resetPassword", "", "Reset password to the one entered")

// 修正安卓时区
var timeZone = flag.String("tZ", "Asia/Shanghai", "fix Android Time Zone")
var timeZone = flag.String("tZ", "", "fix Android Time Zone")

//go:embed static
var staticEmbeddedFiles embed.FS
Expand Down
15 changes: 14 additions & 1 deletion util/andriod_time.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package util

import "time"
import (
"os/exec"
"strings"
"time"
)

func FixTimezone(timeZoneStr string) {
offset, err := getTimeZoneOffset(timeZoneStr)
Expand All @@ -16,6 +20,15 @@ func FixTimezone(timeZoneStr string) {

// 将时区字符串转换为偏移量(以小时为单位)
func getTimeZoneOffset(timeZoneStr string) (int, error) {
// tZ 没有设置,从 安卓环境 persist.sys.timezone 获取
if timeZoneStr == "" {
out, err := exec.Command("/system/bin/getprop", "persist.sys.timezone").Output()
if err != nil {
return 0, err
}
timeZoneStr = strings.TrimSpace(string(out))
}

// 获取时区信息
location, err := time.LoadLocation(timeZoneStr)
if err != nil {
Expand Down

0 comments on commit 7596de5

Please sign in to comment.