惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Tailwind CSS Blog
人人都是产品经理
人人都是产品经理
博客园 - 叶小钗
大猫的无限游戏
大猫的无限游戏
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 【当耐特】
The Cloudflare Blog
博客园 - 聂微东
博客园 - 司徒正美
量子位
博客园 - 三生石上(FineUI控件)
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
G
Google Developers Blog
Apple Machine Learning Research
Apple Machine Learning Research
罗磊的独立博客
酷 壳 – CoolShell
酷 壳 – CoolShell
Y
Y Combinator Blog
S
SegmentFault 最新的问题
T
The Blog of Author Tim Ferriss
P
Proofpoint News Feed
Google DeepMind News
Google DeepMind News
Blog — PlanetScale
Blog — PlanetScale
有赞技术团队
有赞技术团队
A
About on SuperTechFans

陈少文的网站

巨变与机遇的未来十年 Kubernetes 平台管理软件压力测试方案 使用镜像部署 Hexo 静态页面 终于等到你 - GitHub 镜像仓库服务(ghcr.io) 一起来学 Go --(6)Interface 一起来学 Go --(5)Goroutine 和 Channel 什么是函数式编程 如何在 Kubernetes 集群集成 Kata 柯里化与偏函数 使用 PyGithub 自动创建 Label 软件产品是团队能力的输出 Helm 2 、Helm 3 比较 IoT 变现 Kubernetes 中的 DNS 服务 国内的 Helm 镜像源 Harbor 使用自签证书支持 Https 访问 DevOps 工具链之 Prow 如何使用 kfctl 安装 Kubeflow VS Code 无法下载 Go 插件的工具包 工程师更应具有服务精神 你不知道的 Docker 使用技巧 使用 Docker 运行 Tensorflow 论中国 什么是左移 如何清空 Git 仓库全部历史记录 一禅小和尚 有风吹过厨房 时间的玫瑰 如何在 CentOS 安装 GPU 驱动 开发 Tips(19)
Go 中的时间和时区问题
微信公众号 · 2021-12-07 · via 陈少文的网站

Please enable Javascript to view the contents

1. 时间与时区

1.1 时间标准

UTC,世界标准时间,是现在的时间标准,以原子时计时。

GMT,格林威治时间,是以前的时间标准,规定太阳每天经过位于英国伦敦郊区的皇家格林威治天文台的时间为中午 12 点。

UTC 时间更加准确,但如果对精度要求不高,可以视两种标准等同。

1.2 时区划分

从格林威治本初子午线起,经度每向东或者向西间隔 15°,就划分一个时区,因此一共有 24 个时区,东、西个 12 个。

但为了行政上的方便,通常会将一个国家或者一个省份划分在一起。下面是几个 UTC 表示的时间:

  • UTC-6(CST — 北美中部标准时间)
  • UTC+9(JST — 日本标准时间)
  • UTC+8(CT/CST — 中原标准时间)
  • UTC+5:30(IST — 印度标准时间)
  • UTC+3(MSK — 莫斯科时区)

1.3 Local 时间

Local 时间为当前系统的带时区时间,可以通过 /etc/localtime 获取。实际上 /etc/localtime 是指向 zoneinfo 目录下的某个时区。下面是 MacOS 上的执行结果,Linux 上的路径会不一样:

1
2
3
ls -al  /etc/localtime

lrwxr-xr-x  1 root  wheel  39 Apr 26  2021 /etc/localtime -> /var/db/timezone/zoneinfo/Asia/Shanghai

2. Go 中的时间及序列化

2.1 Go 如何初始化时区

  1. 查找 TZ 变量获取时区
  2. 如果没有 TZ,那么使用 /etc/localtime
  3. 如果 TZ="",那么使用 UTC
  4. 当 TZ=“foo” 或者 TZ=":foo"时,如果 foo 指向的文件将被用于初始化时区,否则使用 /usr/share/zoneinfo/foo

下面是 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
tz, ok := syscall.Getenv("TZ")
switch {
case !ok:
	z, err := loadLocation("localtime", []string{"/etc"})
	if err == nil {
		localLoc = *z
		localLoc.name = "Local"
		return
	}
case tz != "":
	if tz[0] == ':' {
		tz = tz[1:]
	}
	if tz != "" && tz[0] == '/' {
		if z, err := loadLocation(tz, []string{""}); err == nil {
			localLoc = *z
			if tz == "/etc/localtime" {
				localLoc.name = "Local"
			} else {
				localLoc.name = tz
			}
			return
		}
	} else if tz != "" && tz != "UTC" {
		if z, err := loadLocation(tz, zoneSources); err == nil {
			localLoc = *z
			return
		}
	}
}

2.2 Go 时间字段的序列化

在 Go 使用 “encoding/json” 可以对 Time 字段进行序列化,使用 Format 可以对时间格式进行自定义。如下示例:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
package main

import (
	"encoding/json"
	"fmt"
	"time"
)

func main(){
	fmt.Println(time.Now())
	var a, _ := json.Marshal(time.Now())
	fmt.Println(string(a))
	a, _ = json.Marshal(time.Now().Format(time.RFC1123))
	fmt.Println(string(a))
	a, _ = json.Marshal(time.Now().Format("06-01-02"))
	fmt.Println(string(a))
}

输出结果:

1
2
3
4
5
2021-12-07 16:44:44.874809 +0800 CST m=+0.000070010
"2021-12-07T16:44:44.874937+08:00"
"Tue, 07 Dec 2021 16:44:44 CST"
"00-120-74 16:44:07"
"21-12-07"

2.3 Go 结构体中的时间字段序列化

在结构体中,如果直接使用 “encoding/json” 对结构体进行序列化,得到的将会是这样的时间格式: 2021-12-07T17:31:08.811045+08:00。无法使用 Format 函数对时间格式进行控制。

那么,如何控制结构体中的时间格式呢?请看如下示例:

  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
package main

import (
	"fmt"
	"strings"
	"time"
	"unsafe"
	"encoding/json"

	jsoniter "github.com/json-iterator/go"
)

func main() {
	var json2 = NewJsonTime()
	var d = struct {
		Title string `json:"title"`
		StartedAt time.Time `json:"time"`
	}{
		Title: "this is title",
		StartedAt: time.Now(),
	}
	t1, _ := json.Marshal(d)
	fmt.Println(string(t1))
	t2, _ := json2.Marshal(d)
	fmt.Println(string(t2))
}

func NewJsonTime() jsoniter.API {
	var jt = jsoniter.ConfigCompatibleWithStandardLibrary
	jt.RegisterExtension(&CustomTimeExtension{})
	return jt
}

type CustomTimeExtension struct {
	jsoniter.DummyExtension
}

func (extension *CustomTimeExtension) UpdateStructDescriptor(structDescriptor *jsoniter.StructDescriptor) {
	for _, binding := range structDescriptor.Fields {
		var typeErr error
		var isPtr bool
		name := strings.ToLower(binding.Field.Name())
		if name == "startedat" {
			isPtr = false
		} else if name == "finishedat" {
			isPtr = false
		} else {
			continue
		}

		timeFormat := time.RFC1123Z
		locale, _ := time.LoadLocation("Asia/Shanghai")

		binding.Encoder = &funcEncoder{fun: func(ptr unsafe.Pointer, stream *jsoniter.Stream) {
			if typeErr != nil {
				stream.Error = typeErr
				return
			}

			var tp *time.Time
			if isPtr {
				tpp := (**time.Time)(ptr)
				tp = *(tpp)
			} else {
				tp = (*time.Time)(ptr)
			}

			if tp != nil {
				lt := tp.In(locale)
				str := lt.Format(timeFormat)
				stream.WriteString(str)
			} else {
				stream.Write([]byte("null"))
			}
		}}
		binding.Decoder = &funcDecoder{fun: func(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
			if typeErr != nil {
				iter.Error = typeErr
				return
			}

			str := iter.ReadString()
			var t *time.Time
			if str != "" {
				var err error
				tmp, err := time.ParseInLocation(timeFormat, str, locale)
				if err != nil {
					iter.Error = err
					return
				}
				t = &tmp
			} else {
				t = nil
			}

			if isPtr {
				tpp := (**time.Time)(ptr)
				*tpp = t
			} else {
				tp := (*time.Time)(ptr)
				if tp != nil && t != nil {
					*tp = *t
				}
			}
		}}
	}
}

type funcDecoder struct {
	fun jsoniter.DecoderFunc
}

func (decoder *funcDecoder) Decode(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
	decoder.fun(ptr, iter)
}

type funcEncoder struct {
	fun         jsoniter.EncoderFunc
	isEmptyFunc func(ptr unsafe.Pointer) bool
}

func (encoder *funcEncoder) Encode(ptr unsafe.Pointer, stream *jsoniter.Stream) {
	encoder.fun(ptr, stream)
}

func (encoder *funcEncoder) IsEmpty(ptr unsafe.Pointer) bool {
	if encoder.isEmptyFunc == nil {
		return false
	}
	return encoder.isEmptyFunc(ptr)
}

输出结果:

1
2
{"title":"this is title","time":"2021-12-07T17:31:08.811045+08:00"}
{"title":"this is title","time":"Tue, 07 Dec 2021 17:31:08 +0800"}

这里主要是使用 “github.com/json-iterator/go” 包控制 Go 对时间字段的序列化,通过其提供的扩展指定 key 为 startedat、finishedat 的时间字段,指定序列化时使用 timeFormat := time.RFC1123Z 格式和 locale, _ := time.LoadLocation("Asia/Shanghai") 时区。

3. 各种环境下设置时区

3.1 在 Linux 中

执行命令:

1
timedatectl set-timezone Asia/Shanghai

或者设置 TZ 环境变量:

1
2
TZ='Asia/Shanghai'
export TZ

都可以设置时区。

3.1 在 Docker 中

在制作镜像时,直接在 Dockerfile 设置 TZ 变量,可能会碰到问题:

1
2
3
FROM alpine
ENV TZ='Asia/Shanghai'
COPY ./time.go .

报错: panic: time: missing Location in call to Time.In

原因: 我们常用的 Linux 系统,例如 Ubuntu、CentOS,在 /usr/share/zoneinfo/ 目录下存放了各个时区而 alpine 镜像没有。

因此 alpine 镜像需要安装一些额外的包。

1
2
3
4
5
FROM alpine

RUN apk add tzdata && \
    cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
    echo "Asia/Shanghai" > /etc/timezone

在运行容器时,可以直接挂载主机的时区描述文件:

1
docker run -it --rm -v /etc/localtime:/etc/localtime:ro nginx

3.2 在 Kubernetes 中

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
apiVersion: v1
kind: Pod
metadata:
  name: test
  namespace: default
spec:
  restartPolicy: OnFailure
  containers:
    - name: nginx
      image: nginx-test
      imagePullPolicy: IfNotPresent
      volumeMounts:
        - name: date-config
          mountPath: /etc/localtime
      command: ["sleep", "60000"]
  volumes:
    - name: date-config
      hostPath:
        path: /etc/localtime

这里将主机上的时区文件挂载到 Pod 中。

4. 参考


微信公众号