Skip to content

Commit

Permalink
chore: Replace android timezone implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
xishang0128 committed Mar 9, 2024
1 parent e0248fa commit 77c10d9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
21 changes: 21 additions & 0 deletions android_tz.go
Original file line number Diff line number Diff line change
@@ -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 changes: 0 additions & 17 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"flag"
"fmt"
"os"
"os/exec"
"os/signal"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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
}

0 comments on commit 77c10d9

Please sign in to comment.