-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
68 lines (57 loc) · 2.13 KB
/
Copy pathmain.go
File metadata and controls
68 lines (57 loc) · 2.13 KB
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
package main
import (
"flag"
"log"
"net/http"
"time"
"github.com/beevik/ntp"
"github.com/gin-gonic/gin"
)
// 默认参数
const defaultPort = "8080"
// 为了适配各种对时工具,这里各种大小写的DateTime都返回,避免因为大小写不匹配导致的对时失败
type timeResponse struct {
DateTime string `json:"DateTime"`
DateTimeLow string `json:"dateTime"`
DataTimeAllLow string `json:"datetime"`
}
func main() {
portFlag := flag.String("port", defaultPort, "port to listen on")
offsetFlag := flag.Duration("offset", 0, "time offset to add to now (e.g. 100ns, 50ms, 1s)")
flag.Parse()
r := gin.Default()
// 直接返回当前UTC时间,格式为RFC3339Nano,包含纳秒级别的时间戳
r.GET("/time", func(c *gin.Context) {
now := time.Now().UTC().Format(time.RFC3339Nano) // 使用RFC3339Nano格式,包含纳秒级别的时间戳
c.JSON(http.StatusOK, timeResponse{DateTime: now, DateTimeLow: now, DataTimeAllLow: now})
})
// 返回带偏移量的当前UTC时间,格式同上
r.GET("/offset_time", func(c *gin.Context) {
now := time.Now().UTC().Add(*offsetFlag).Format(time.RFC3339Nano)
c.JSON(http.StatusOK, timeResponse{DateTime: now, DateTimeLow: now, DataTimeAllLow: now})
})
// 返回 NTP 获取的 UTC 时间
r.GET("/ntp_time", func(c *gin.Context) {
t, err := ntp.Time("pool.ntp.org")
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to fetch ntp time"})
return
}
now := t.UTC().Format(time.RFC3339Nano)
c.JSON(http.StatusOK, timeResponse{DateTime: now, DateTimeLow: now, DataTimeAllLow: now})
})
// 返回带偏移量的 NTP UTC 时间
r.GET("/offset_npt_time", func(c *gin.Context) { // 拼写按需求使用 npt
t, err := ntp.Time("pool.ntp.org")
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to fetch ntp time"})
return
}
now := t.UTC().Add(*offsetFlag).Format(time.RFC3339Nano)
c.JSON(http.StatusOK, timeResponse{DateTime: now, DateTimeLow: now, DataTimeAllLow: now})
})
// 启动服务器
if err := r.Run(":" + *portFlag); err != nil {
log.Fatalf("failed to start server: %v", err)
}
}