chore: Replace android timezone implementation

kanged from https://github.com/SagerNet/sing-box/blob/dev-next/include/tz_android.go
This commit is contained in:
xishang0128 2024-03-09 19:25:26 +08:00
parent e0248faebd
commit 77c10d90f3
No known key found for this signature in database
GPG Key ID: 44A1E10B5ADF68CB
2 changed files with 21 additions and 17 deletions

21
android_tz.go Normal file
View File

@ -0,0 +1,21 @@
// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// kanged from https://github.com/golang/mobile/blob/c713f31d574bb632a93f169b2cc99c9e753fef0e/app/android.go#L89
package main
// #include <time.h>
import "C"
import "time"
func init() {
var currentT C.time_t
var currentTM C.struct_tm
C.time(&currentT)
C.localtime_r(&currentT, &currentTM)
tzOffset := int(currentTM.tm_gmtoff)
tz := C.GoString(currentTM.tm_zone)
time.Local = time.FixedZone(tz, tzOffset)
}

17
main.go
View File

@ -4,7 +4,6 @@ import (
"flag"
"fmt"
"os"
"os/exec"
"os/signal"
"path/filepath"
"runtime"
@ -49,10 +48,6 @@ func init() {
}
func main() {
if runtime.GOOS == "android" {
SetAndroidTZ()
}
_, _ = maxprocs.Set(maxprocs.Logger(func(string, ...any) {}))
if version {
fmt.Printf("Mihomo Meta %s %s %s with %s %s\n",
@ -181,15 +176,3 @@ func updateGeoDatabases() {
executor.ApplyConfig(cfg, false)
}()
}
func SetAndroidTZ() {
out, err := exec.Command("getprop", "persist.sys.timezone").Output()
if err != nil {
return
}
z, err := time.LoadLocation(strings.TrimSpace(string(out)))
if err != nil {
return
}
time.Local = z
}