From 654c488ed8ac51975d13a16eed5160483420f081 Mon Sep 17 00:00:00 2001
From: kr328 <kr328app@outlook.com>
Date: Sun, 12 Sep 2021 11:46:20 +0800
Subject: [PATCH] Fix: fix time zone

---
 core/src/main/cpp/main.c                      | 10 +++++++++
 core/src/main/golang/native/app.go            |  7 ++++++
 core/src/main/golang/native/app/app.go        |  5 +++++
 .../java/com/github/kr328/clash/core/Clash.kt |  4 ++++
 .../github/kr328/clash/core/bridge/Bridge.kt  |  1 +
 .../kr328/clash/service/ClashService.kt       |  1 +
 .../github/kr328/clash/service/TunService.kt  |  1 +
 .../service/clash/module/TimeZoneModule.kt    | 22 +++++++++++++++++++
 8 files changed, 51 insertions(+)
 create mode 100644 service/src/main/java/com/github/kr328/clash/service/clash/module/TimeZoneModule.kt

diff --git a/core/src/main/cpp/main.c b/core/src/main/cpp/main.c
index 6c1f2ece..f1c632b0 100644
--- a/core/src/main/cpp/main.c
+++ b/core/src/main/cpp/main.c
@@ -84,6 +84,16 @@ Java_com_github_kr328_clash_core_bridge_Bridge_nativeNotifyDnsChanged(JNIEnv *en
     notifyDnsChanged(_dns_list);
 }
 
+JNIEXPORT void JNICALL
+Java_com_github_kr328_clash_core_bridge_Bridge_nativeNotifyTimeZoneChanged(JNIEnv *env, jobject thiz,
+                                                                           jstring name, jint offset) {
+    TRACE_METHOD();
+
+    scoped_string _name = get_string(name);
+
+    notifyTimeZoneChanged(_name, offset);
+}
+
 JNIEXPORT void JNICALL
 Java_com_github_kr328_clash_core_bridge_Bridge_nativeNotifyInstalledAppChanged(JNIEnv *env,
                                                                                jobject thiz,
diff --git a/core/src/main/golang/native/app.go b/core/src/main/golang/native/app.go
index a2b5342b..b21224f1 100644
--- a/core/src/main/golang/native/app.go
+++ b/core/src/main/golang/native/app.go
@@ -8,6 +8,7 @@ import (
 	"unsafe"
 
 	"cfa/native/app"
+
 	"github.com/Dreamacro/clash/log"
 )
 
@@ -42,6 +43,12 @@ func notifyInstalledAppsChanged(uids C.c_string) {
 	app.NotifyInstallAppsChanged(u)
 }
 
+//export notifyTimeZoneChanged
+func notifyTimeZoneChanged(name C.c_string, offset C.int) {
+	app.NotifyTimeZoneChanged(C.GoString(name), int(offset))
+}
+
+
 //export queryConfiguration
 func queryConfiguration() *C.char {
 	response := &struct{}{}
diff --git a/core/src/main/golang/native/app/app.go b/core/src/main/golang/native/app/app.go
index a946c6ab..7575d1e9 100644
--- a/core/src/main/golang/native/app/app.go
+++ b/core/src/main/golang/native/app/app.go
@@ -3,6 +3,7 @@ package app
 import (
 	"strconv"
 	"strings"
+	"time"
 )
 
 var appVersionName string
@@ -46,3 +47,7 @@ func NotifyInstallAppsChanged(uidList string) {
 func QueryAppByUid(uid int) string {
 	return installedAppsUid[uid]
 }
+
+func NotifyTimeZoneChanged(name string, offset int) {
+	time.Local = time.FixedZone(name, offset)
+}
\ No newline at end of file
diff --git a/core/src/main/java/com/github/kr328/clash/core/Clash.kt b/core/src/main/java/com/github/kr328/clash/core/Clash.kt
index eac180e1..3b2d5bb8 100644
--- a/core/src/main/java/com/github/kr328/clash/core/Clash.kt
+++ b/core/src/main/java/com/github/kr328/clash/core/Clash.kt
@@ -52,6 +52,10 @@ object Clash {
         Bridge.nativeNotifyDnsChanged(dns.joinToString(separator = ","))
     }
 
+    fun notifyTimeZoneChanged(name: String, offset: Int) {
+        Bridge.nativeNotifyTimeZoneChanged(name, offset)
+    }
+
     fun notifyInstalledAppsChanged(uids: List<Pair<Int, String>>) {
         val uidList = uids.joinToString(separator = ",") { "${it.first}:${it.second}" }
 
diff --git a/core/src/main/java/com/github/kr328/clash/core/bridge/Bridge.kt b/core/src/main/java/com/github/kr328/clash/core/bridge/Bridge.kt
index a775853c..b77588a3 100644
--- a/core/src/main/java/com/github/kr328/clash/core/bridge/Bridge.kt
+++ b/core/src/main/java/com/github/kr328/clash/core/bridge/Bridge.kt
@@ -16,6 +16,7 @@ object Bridge {
     external fun nativeQueryTrafficNow(): Long
     external fun nativeQueryTrafficTotal(): Long
     external fun nativeNotifyDnsChanged(dnsList: String)
+    external fun nativeNotifyTimeZoneChanged(name: String, offset: Int)
     external fun nativeNotifyInstalledAppChanged(uidList: String)
     external fun nativeStartTun(fd: Int, mtu: Int, dns: String, blocking: String, cb: TunInterface)
     external fun nativeStopTun()
diff --git a/service/src/main/java/com/github/kr328/clash/service/ClashService.kt b/service/src/main/java/com/github/kr328/clash/service/ClashService.kt
index dd58a644..14bde28b 100644
--- a/service/src/main/java/com/github/kr328/clash/service/ClashService.kt
+++ b/service/src/main/java/com/github/kr328/clash/service/ClashService.kt
@@ -35,6 +35,7 @@ class ClashService : BaseService() {
             install(StaticNotificationModule(self))
 
         install(AppListCacheModule(self))
+        install(TimeZoneModule(self))
         install(SuspendModule(self))
 
         try {
diff --git a/service/src/main/java/com/github/kr328/clash/service/TunService.kt b/service/src/main/java/com/github/kr328/clash/service/TunService.kt
index f861145d..a08a9312 100644
--- a/service/src/main/java/com/github/kr328/clash/service/TunService.kt
+++ b/service/src/main/java/com/github/kr328/clash/service/TunService.kt
@@ -40,6 +40,7 @@ class TunService : VpnService(), CoroutineScope by CoroutineScope(Dispatchers.De
             install(StaticNotificationModule(self))
 
         install(AppListCacheModule(self))
+        install(TimeZoneModule(self))
         install(SuspendModule(self))
 
         try {
diff --git a/service/src/main/java/com/github/kr328/clash/service/clash/module/TimeZoneModule.kt b/service/src/main/java/com/github/kr328/clash/service/clash/module/TimeZoneModule.kt
new file mode 100644
index 00000000..08159517
--- /dev/null
+++ b/service/src/main/java/com/github/kr328/clash/service/clash/module/TimeZoneModule.kt
@@ -0,0 +1,22 @@
+package com.github.kr328.clash.service.clash.module
+
+import android.app.Service
+import android.content.Intent
+import com.github.kr328.clash.core.Clash
+import java.util.*
+
+class TimeZoneModule(service: Service) : Module<Unit>(service) {
+    override suspend fun run() {
+        val timeZones = receiveBroadcast {
+            addAction(Intent.ACTION_TIMEZONE_CHANGED)
+        }
+
+        while (true) {
+            val timeZone = TimeZone.getDefault()
+
+            Clash.notifyTimeZoneChanged(timeZone.id, timeZone.rawOffset)
+
+            timeZones.receive()
+        }
+    }
+}
\ No newline at end of file
-- 
GitLab