Skip to content

Commit 38f76dd

Browse files
committed
Fix timezone for Android and iOS
1 parent c38f08e commit 38f76dd

File tree

8 files changed

+64
-15
lines changed

8 files changed

+64
-15
lines changed

constant/quic.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
//go:build with_quic
2+
3+
package constant
4+
5+
const WithQUIC = true

constant/quic_stub.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
//go:build !with_quic
2+
3+
package constant
4+
5+
const WithQUIC = false

experimental/libbox/setup.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/sagernet/sing-box/common/humanize"
99
C "github.com/sagernet/sing-box/constant"
10+
_ "github.com/sagernet/sing-box/include"
1011
)
1112

1213
var (

inbound/naive.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"github.com/sagernet/sing-box/common/tls"
1616
"github.com/sagernet/sing-box/common/uot"
1717
C "github.com/sagernet/sing-box/constant"
18-
"github.com/sagernet/sing-box/include"
1918
"github.com/sagernet/sing-box/log"
2019
"github.com/sagernet/sing-box/option"
2120
"github.com/sagernet/sing/common"
@@ -109,8 +108,8 @@ func (n *Naive) Start() error {
109108

110109
if common.Contains(n.network, N.NetworkUDP) {
111110
err := n.configureHTTP3Listener()
112-
if !include.WithQUIC && len(n.network) > 1 {
113-
log.Warn(E.Cause(err, "naive http3 disabled"))
111+
if !C.WithQUIC && len(n.network) > 1 {
112+
n.logger.Warn(E.Cause(err, "naive http3 disabled"))
114113
} else if err != nil {
115114
return err
116115
}

include/quic.go

Lines changed: 0 additions & 10 deletions
This file was deleted.

include/quic_stub.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ import (
1616
N "github.com/sagernet/sing/common/network"
1717
)
1818

19-
const WithQUIC = false
20-
2119
func init() {
2220
dns.RegisterTransport([]string{"quic", "h3"}, func(name string, ctx context.Context, logger logger.ContextLogger, dialer N.Dialer, link string) (dns.Transport, error) {
2321
return nil, C.ErrQUICNotIncluded

include/tz_android.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2014 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// kanged from https://github.com/golang/mobile/blob/c713f31d574bb632a93f169b2cc99c9e753fef0e/app/android.go#L89
6+
7+
package include
8+
9+
// #include <time.h>
10+
import "C"
11+
import "time"
12+
13+
func init() {
14+
var currentT C.time_t
15+
var currentTM C.struct_tm
16+
C.time(&currentT)
17+
C.localtime_r(&currentT, &currentTM)
18+
tzOffset := int(currentTM.tm_gmtoff)
19+
tz := C.GoString(currentTM.tm_zone)
20+
time.Local = time.FixedZone(tz, tzOffset)
21+
}

include/tz_ios.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package include
2+
3+
/*
4+
#cgo CFLAGS: -x objective-c
5+
#cgo LDFLAGS: -framework Foundation
6+
#import <Foundation/Foundation.h>
7+
const char* getSystemTimeZone() {
8+
NSTimeZone *timeZone = [NSTimeZone systemTimeZone];
9+
NSString *timeZoneName = [timeZone description];
10+
return [timeZoneName UTF8String];
11+
}
12+
*/
13+
import "C"
14+
15+
import (
16+
"strings"
17+
"time"
18+
)
19+
20+
func init() {
21+
tzDescription := C.GoString(C.getSystemTimeZone())
22+
if len(tzDescription) == 0 {
23+
return
24+
}
25+
location, err := time.LoadLocation(strings.Split(tzDescription, " ")[0])
26+
if err != nil {
27+
return
28+
}
29+
time.Local = location
30+
}

0 commit comments

Comments
 (0)