-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgontp.go
353 lines (333 loc) · 10.4 KB
/
gontp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package gontp
import (
"bytes"
"encoding/binary"
"fmt"
"io/ioutil"
"net"
"net/http"
"runtime"
"time"
"github.com/gogf/gf/os/gproc"
"github.com/gogf/gf/text/gstr"
"golang.org/x/text/encoding/simplifiedchinese"
"golang.org/x/text/transform"
"strings"
)
//Based on https://github.com/vladimirvivien/go-ntp-client
// NTP packet format (v3 with optional v4 fields removed)
//
// 0 1 2 3
// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// |LI | VN |Mode | Stratum | Poll | Precision |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// | Root Delay |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// | Root Dispersion |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// | Reference ID |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// | |
// + Reference Timestamp (64) +
// | |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// | |
// + Origin Timestamp (64) +
// | |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// | |
// + Receive Timestamp (64) +
// | |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// | |
// + Transmit Timestamp (64) +
// | |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
//
// This program implements a trivial NTP client over UDP.
//
// Usage:
// time -e <host endpoint as addr:port>
//
// Get NTP time by ntp server path
// ntpserver like: us.pool.ntp.org
func GetNtpTime(ntpserver string) (now time.Time, er error) {
const ntpEpochOffset = 2208988800
type packet struct {
Settings uint8 // leap yr indicator, ver number, and mode
Stratum uint8 // stratum of local clock
Poll int8 // poll exponent
Precision int8 // precision exponent
RootDelay uint32 // root delay
RootDispersion uint32 // root dispersion
ReferenceID uint32 // reference id
RefTimeSec uint32 // reference timestamp sec
RefTimeFrac uint32 // reference timestamp fractional
OrigTimeSec uint32 // origin time secs
OrigTimeFrac uint32 // origin time fractional
RxTimeSec uint32 // receive time secs
RxTimeFrac uint32 // receive time frac
TxTimeSec uint32 // transmit time secs
TxTimeFrac uint32 // transmit time frac
}
host := ntpserver
//flag.StringVar(&host, "e", ntpserver, "NTP host")
///flag.Parse()
if !strings.Contains(host, ":123") {
host = ntpserver + ":123"
}
// Setup a UDP connection
conn, err := net.Dial("udp", host)
if err != nil {
er = fmt.Errorf("failed to connect: %v", err)
return
}
defer conn.Close()
if err := conn.SetDeadline(time.Now().Add(15 * time.Second)); err != nil {
er = fmt.Errorf("failed to set deadline: %v", err)
return
}
// configure request settings by specifying the first byte as
// 00 011 011 (or 0x1B)
// | | +-- client mode (3)
// | + ----- version (3)
// + -------- leap year indicator, 0 no warning
req := &packet{Settings: 0x1B}
// send time request
if err := binary.Write(conn, binary.BigEndian, req); err != nil {
er = fmt.Errorf("failed to send request: %v", err)
return
}
// block to receive server response
rsp := &packet{}
if err := binary.Read(conn, binary.BigEndian, rsp); err != nil {
er = fmt.Errorf("failed to read server response: %v", err)
return
}
// On POSIX-compliant OS, time is expressed
// using the Unix time epoch (or secs since year 1970).
// NTP seconds are counted since 1900 and therefore must
// be corrected with an epoch offset to convert NTP seconds
// to Unix time by removing 70 yrs of seconds (1970-1900)
// or 2208988800 seconds.
secs := float64(rsp.TxTimeSec) - ntpEpochOffset
nanos := (int64(rsp.TxTimeFrac) * 1e9) >> 32 // convert fractional to nanos
now = time.Unix(int64(secs), nanos)
return
}
//Update System Date and time
func UpdateSystemDateTime(t time.Time) error {
dateTime := t.Format("2006-01-02 15:04:05.000000000")
system := runtime.GOOS
switch system {
case "windows":
dateTime = t.Format("2006-01-02 15:04:05")
{
dandt := gstr.Split(dateTime, " ")
//fmt.Println("日期:", dandt[0], "时间:", dandt[1])
_, err1 := gproc.ShellExec(`date ` + dandt[0])
//fmt.Println("日期转换:", string(GbkToUtf8([]byte(str1))), string(GbkToUtf8([]byte(err1.Error()))))
if err1 != nil {
return fmt.Errorf("error updating system date,please start the program as an administrator:%s", err1.Error())
}
_, err2 := gproc.ShellExec(`time ` + dandt[1])
//fmt.Println("时间转换:", string(GbkToUtf8([]byte(str2))), string(GbkToUtf8([]byte(err2.Error()))))
if err2 != nil {
return fmt.Errorf("error updating system time,please start the program as an administrator:%s", err2.Error())
}
return nil
}
case "linux":
{
_, err := gproc.ShellExec(`date -s "` + dateTime + `"`)
if err != nil {
return fmt.Errorf("error updating system time:%s", err.Error())
}
return nil
}
case "darwin":
{
_, err := gproc.ShellExec(`date -s "` + dateTime + `"`)
if err != nil {
return fmt.Errorf("error updating system time:%s", err.Error())
}
return nil
}
}
return fmt.Errorf("error updating system time: unsupported operating system < %s >", system)
}
//Convert time string to go time.Time
func TimeParse(s string, loc ...*time.Location) (time.Time, error) {
location := time.Local
if len(loc) > 0 {
for _, v := range loc {
location = v
}
}
s = strings.ReplaceAll(s, "\"", "")
if strings.Contains(s, ".") {
strs := strings.Split(s, ".") //用.切片
dsec := "." //小数点后的秒格式
tzone := "Z0700"
havezone := false //是否包含时区
if len(strs) > 1 {
dseclen := 0 //小数部分的长度
if strings.Contains(strs[1], "+") { //包含时区
havezone = true
dotseconds := strings.Split(strs[1], "+") //分割小数的秒和时区
dseclen = len(dotseconds[0])
if len(dotseconds) > 1 { //时区切片
if strings.Contains(dotseconds[1], ":") { //时区切片含有分号
tzone = "Z07:00"
}
}
} else {
dseclen = len(strs[1])
}
for i := 0; i < dseclen; i++ {
dsec += "0"
}
}
layout := "2006-01-02 15:04:05"
if strings.Contains(s, "T") {
layout = "2006-01-02T15:04:05"
}
layout += dsec
if havezone { //包含时区
layout += tzone
t, err := time.Parse(layout, s)
if err == nil {
return t, nil
}
} else {
t, err := time.ParseInLocation(layout, s, location)
if err == nil {
return t, nil
}
}
} else {
if strings.Contains(s, "+") { //包含时区
tzone := "Z0700"
strs := strings.Split(s, "+") //时区
if len(strs) > 1 { //时区切片
if strings.Contains(strs[1], ":") { //时区切片含有分号
tzone = "Z07:00"
}
}
layout := "2006-01-02 15:04:05"
if strings.Contains(s, "T") {
layout = "2006-01-02T15:04:05"
}
layout += tzone
t, err := time.Parse(layout, s)
if err == nil {
return t, nil
}
}
}
t, err := time.ParseInLocation("2006-01-02 15:04:05", s, location)
if err == nil {
return t, nil
}
t, err = time.ParseInLocation("2006-01-02 15:04", s, location)
if err == nil {
return t, nil
}
t, err = time.ParseInLocation("2006年01月02日 15:04:05", s, location)
if err == nil {
return t, nil
}
t, err = time.ParseInLocation("2006年01月02日 15:04", s, location)
if err == nil {
return t, nil
}
t, err = time.ParseInLocation("2006年01月02日 15点04分05秒", s, location)
if err == nil {
return t, nil
}
t, err = time.ParseInLocation("2006年01月02日 15点04分", s, location)
if err == nil {
return t, nil
}
t, err = time.ParseInLocation("2006-1-2 15:04:05", s, location)
if err == nil {
return t, nil
}
t, err = time.ParseInLocation("2006-1-2 15:04", s, location)
if err == nil {
return t, nil
}
t, err = time.ParseInLocation("2006/01/02 15:04:05", s, location)
if err == nil {
return t, nil
}
t, err = time.ParseInLocation("2006/01/02 15:04", s, location)
if err == nil {
return t, nil
}
t, err = time.ParseInLocation("2006/1/2 15:04:05", s, location)
if err == nil {
return t, nil
}
t, err = time.ParseInLocation("2006/1/2 15:04", s, location)
if err == nil {
return t, nil
}
t, err = time.ParseInLocation("2006-01-02T15:04:05Z", s, location)
if err == nil {
return t, nil
}
t, err = time.ParseInLocation("2006-01-02T15:04:05", s, location)
if err == nil {
return t, nil
}
t, err = time.ParseInLocation("2006-01-02T15:04", s, location)
if err == nil {
return t, nil
}
t, err = time.ParseInLocation("2006-1-2T15:04:05Z", s, location)
if err == nil {
return t, nil
}
t, err = time.ParseInLocation("2006-1-2T15:04", s, location)
if err == nil {
return t, nil
}
t, err = time.ParseInLocation("20060102150405", s, location)
if err == nil {
return t, nil
}
t, err = time.ParseInLocation("200601021504", s, location)
if err == nil {
return t, nil
}
return t, fmt.Errorf("the raw time string is:%s,the error is:%s", s, err.Error())
}
//Get string from http url
func GetFromHttpUrl(urlstr string) (string, int, error) {
resp, err := http.Get(urlstr) //Get Data
if err != nil {
return "", 0, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body) //Read data
if err != nil {
return "", 0, err
}
return string(body), resp.StatusCode, nil
}
func GbkToUtf8(s []byte) []byte {
reader := transform.NewReader(bytes.NewReader(s), simplifiedchinese.GBK.NewDecoder())
d, e := ioutil.ReadAll(reader)
if e != nil {
return nil
}
var bstr []byte
for _, c := range d {
if c > 0 {
bstr = append(bstr, c)
}
}
return bstr
}