From 77c10d90f3b13c26bc4a26a8258054b664773d0d Mon Sep 17 00:00:00 2001
From: xishang0128 <xishang02@gmail.com>
Date: Sat, 9 Mar 2024 19:25:26 +0800
Subject: [PATCH] chore: Replace android timezone implementation kanged from
 https://github.com/SagerNet/sing-box/blob/dev-next/include/tz_android.go

---
 android_tz.go | 21 +++++++++++++++++++++
 main.go       | 17 -----------------
 2 files changed, 21 insertions(+), 17 deletions(-)
 create mode 100644 android_tz.go

diff --git a/android_tz.go b/android_tz.go
new file mode 100644
index 00000000..82fc38e3
--- /dev/null
+++ b/android_tz.go
@@ -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)
+}
diff --git a/main.go b/main.go
index 4b2dff9c..748fa2e3 100644
--- a/main.go
+++ b/main.go
@@ -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
-}