Go时区设置

Python026

Go时区设置,第1张

全球以英国伦敦格林威治作为零度经线的起点,每隔15经度为一个时区,15度经线为该时区的中央经线,共分为24个时区。由西向东每隔15经度增加一个时区,相反的,每向西15经度减少一个时区。中国所在时区为东8区。

当前时间 time.Now() 返回的是当地时区的时间:

CST可以代表如下四个不同的时区:

time.Now() 返回的 +0800 CST 表示的就是中国标准时间,与UTC时间有如下的转化:

Wall Clocks表示挂钟时间,存储的是自1970 年 1 月 1 日 0 时 0 分 0 秒以来的时间戳,当系统和授时服务器进行校准时间时间操作时,有可能造成这一秒是2018-1-1 00:00:00,而下一秒变成了2017-12-31 23:59:59的情况。

Monotonic Clocks,意思是单调时间的,所谓单调,就是只会不停的往前增长,不受校时操作的影响,这个时间是自进程启动以来的秒数。

time.Now() 返回的 m=+0.004002201 就是表示Monotonic Clocks

go语言中如果不设置指定的时区,通过 time.Now() 获取到的就是本地时区

设置时区有两种方式:

固定时区到东八区。但这种不是对程序的全局设置,每次获取时都需要固定时区

加载指定时区。但如果没有go环境使用这种方式就会加载失败,因为时区信息是放在go的安装包中的。

如果你用第二种方式加载时区,在打docker镜像时就需要进行时区相关的配置,配置文件如下:

参考文章:

https://zhuanlan.zhihu.com/p/47754783

https://blog.mimvp.com/article/11887.html

是Go语言吗?

Go 编译过程 九个步骤

第一步. all.bash

% cd $GOROOT/src

% ./all.bash

第一步 all.bash 只是调用了另外两个 shell 脚本:make.bash 和run.bash。若使用 Windows 或 Plan9,其过程也基本类似,只是脚本分别以 .bat 或 .rc 结尾。在文章的其他部分,请用适当的操作系统对应的扩展来补全命令。

第二步. make.bash

. ./make.bash --no-banner

make.bash 作为 all.bash 内容的一部分,如果它退出也会中断构建过程

第三步. cmd/dist

gcc -O2 -Wall -Werror -ggdb -o cmd/dist/dist -Icmd/dist cmd/dist/*.c

当健全检查完成后,make.bash 开始编译 cmd/dist。

第四步. go_bootstrap

现在 go_bootstrap 已经构建完成,make.bash 的最后一步是使用 go_bootstrap 编译完整的 Go 标准库,包括一个完整的 go 工具用以替换。

echo "# Building packages and commands for $GOOS/$GOARCH."

"$GOTOOLDIR"/go_bootstrap install -gcflags "$GO_GCFLAGS" \

-ldflags "$GO_LDFLAGS" -v std

第五步. run.bash

现在 make.bash 已经完成,回到 all.bash 的执行,这会调用 run.bash。run.bash 的任务是编译和测试标准库、运行时以及语言测试集。

bash run.bash --no-rebuild

由于 make.bash 和 run.bash 都会调用 go install -a std,因此需要使用 –no-rebuild 标志来避免重复前面的步骤,–no-rebuild 跳过了第二个 go install。

# allow all.bash to avoid double-build of everythingrebuild=trueif [ "$1" = "--no-rebuild" ]then shiftelse echo '# Building packages and commands.' time go install -a -v std echofi

第六步. go test -a std

echo '# Testing packages.'

time go test std -short -timeout=$(expr 120 \* $timeout_scale)s

echo

接下来 run.bash 会在标准库里所有的包上来运行用 testing 包编写的单元测试。由于 $GOPATH 和 $GOROOT 中有着相同的命名空间,所以不能直接使用 go test … 否则 $GOPATH 中的每个包也会被逐一测试,因此创建了一个用于标准库中的包的别名:std。由于一些测试需要比较长的时间,且会消耗大量内存,因此用 -short 标志对一些测试进行了过滤。

第七步. runtime 和 cgo 测试

run.bash 接下来的部分会运行平台对 cgo 支持的测试,执行一些性能测试,并且编译一些伴随 Go 发行版一起的杂项程序。随着时间的流逝,这些杂项程序的清单会越来越长,那么它们也就会不可避免的被从编译过程中悄悄剥离出去。

第八步. go run test

(xcd ../test

unset GOMAXPROCS

time go run run.go

) || exit $?

run.bash 的倒数第二步会调用在 $GOROOT 下的 test 目录里的编译器和运行时的测试。他们是对于编译器和运行时自身的,较为低级细节的测试。会执行语言规格测试,test/bugs 和 test/fixedbugs 子目录保存有那些已经被发现并被修复的问题的独立的测试。驱动测试的是一个小 Go 程序 $GOROOT/test/run.go,会执行 test 目录里的每个 .go 文件。一些 .go 文件的首行包含了指导 run.go 对结果作出判断的指令,例如,程序将会失败,或提供一个确定的输出队列。

第九步. go tool api

echo '# Checking API compatibility.'

go tool api -c $GOROOT/api/go1.txt,$GOROOT/api/go1.1.txt \

-next $GOROOT/api/next.txt -except $GOROOT/api/except.txt

run.bash 的最后一步调用了 api 工具。

1、解压压缩包到go工作目录,如解压到E:\opensource\go\go,解压后的目录结构如下:

E:\opensource\go\go

├─api

├─bin

│ ├─go.exe

│ ├─godoc.exe

│ └─gofmt.exe

├─doc

├─include

├─lib

├─misc

├─pkg

├─src

└─test

2、增加环境变量GOROOT,取值为上面的go工作目录

3、Path环境变量中添加"%GOROOT%\bin",以便能够直接调用go命令来编译go代码,至此go编译环境就配置好了

注:如果不想手动设置系统环境变量,也可下载go启动环境批处理附件,

修改goenv.bat文件中的GOROOT值为上面的go工作目录后直接双击该bat文件,go编译环境变量即设置完成。

4、测试go编译环境,启动一个cmd窗口,直接输入go,看到下面的提示就是搭建成功了

E:\opensource\go\go>go

Go is a tool for managing Go source code.

Usage:

go command [arguments]

The commands are:

build compile packages and dependencies

clean remove object files

doc run godoc on package sources

env print Go environment information

fix run go tool fix on packages

fmt run gofmt on package sources

get download and install packages and dependencies

install compile and install packages and dependencies

listlist packages

run compile and run Go program

testtest packages

toolrun specified go tool

version print Go version

vet run go tool vet on packages

Use "go help [command]" for more information about a command.

Additional help topics:

gopath GOPATH environment variable

packagesdescription of package lists

remote remote import path syntax

testflagdescription of testing flags

testfuncdescription of testing functions

Use "go help [topic]" for more information about that topic.

5、编译helloworld测试程序,go语言包中test目录带有helloworld.go测试程序,源码见"附一 helloworld.go",

直接调用"go build helloworld.go"就生成了"helloworld.exe"可执行程序,运行一下这个程序看到了我们期望的hello,wolrd。

E:\opensource\go\go\test>go build helloworld.go

E:\opensource\go\go\test>helloworld.exe

hello, world

E:\opensource\go\go\test>

附一 helloworld.go

// cmpout

// Copyright 2009 The Go Authors. All rights reserved.

// Use of this source code is governed by a BSD-style

// license that can be found in the LICENSE file.

// Test that we can do page 1 of the C book.

package main

func main() {

print("hello, world\n")

}