Go by Example - Time, Epoch, Time Formatting / Parsing and Random Numbers

Go by Example - Time, Epoch, Time Formatting / Parsing and Random Numbers

介绍go中的时间, 纪元, 时间格式化/解析和随机数

Time

Go 提供了对时间和持续时间的广泛支持;下面是一些例子。

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

import (
"fmt"
"time"
)

func main() {
p := fmt.Println

// 我们先从获取当前时间开始
now := time.Now()
p(now)

// 您可以通过提供年、月、日等信息来构建时间结构
// 时间总是与地点(即时区)相关联
then := time.Date(
2009, 11, 17, 20, 34, 58, 651387237, time.UTC)
p(then)

// 您可以按照预期提取时间值的各个组成部分
p(then.Year())
p(then.Month())
p(then.Day())
p(then.Hour())
p(then.Minute())
p(then.Second())
p(then.Nanosecond())
p(then.Location())

// 周一至周日平日也可使用
p(then.Weekday())

// 这些方法对两个时间进行比较,分别测试第一个时间是否发生在第二个时间之前、之后或同时
p(then.Before(now))
p(then.After(now))
p(then.Equal(now))

// Sub 方法返回表示两个时间间隔的 Duration
diff := now.Sub(then)
p(diff)

// 我们可以计算出不同单位的持续时间长度
p(diff.Hours())
p(diff.Minutes())
p(diff.Seconds())
p(diff.Nanoseconds())

// 您可以使用Add将时间提前给定的持续时间,或者使用-将时间向后移动持续时间
p(then.Add(diff))
p(then.Add(-diff))
}
log
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
$ go run time.go
2012-10-31 15:50:13.793654 +0000 UTC
2009-11-17 20:34:58.651387237 +0000 UTC
2009
November
17
20
34
58
651387237
UTC
Tuesday
true
false
false
25891h15m15.142266763s
25891.25420618521
1.5534752523711128e+06
9.320851514226677e+07
93208515142266763
2012-10-31 15:50:13.793654 +0000 UTC
2006-12-05 01:19:43.509120474 +0000 UTC

接下来,我们将研究与 Unix 纪元相关的时间概念。

Epoch

程序中的一个常见需求是获取自 Unix 纪元起的秒数、毫秒数或纳秒数。下面介绍如何用 Go 实现这一功能。

epoch.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package main

import (
"fmt"
"time"
)

func main() {
// 使用带有 Unix、UnixMilli 或 UnixNano 的 time.Now,可分别以秒、毫秒或纳秒为单位获取自 Unix 元年以来的经过时间
now := time.Now()
fmt.Println(now)

fmt.Println(now.Unix())
fmt.Println(now.UnixMilli())
fmt.Println(now.UnixNano())

// 您还可以将从纪元开始的整数秒或纳秒转换成相应的时间
fmt.Println(time.Unix(now.Unix(), 0))
fmt.Println(time.Unix(0, now.UnixNano()))
}
log
1
2
3
4
5
6
7
$ go run epoch.go
2012-10-31 16:13:58.292387 +0000 UTC
1351700038
1351700038292
1351700038292387000
2012-10-31 16:13:58 +0000 UTC
2012-10-31 16:13:58.292387 +0000 UTC

接下来我们来看看另一项与时间相关的任务:时间解析和格式化。

Time Formatting / Parsing

Go 支持通过基于模式的布局进行时间格式化和解析。

time-formatting-parsing.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
package main

import (
"fmt"
"time"
)

func main() {
p := fmt.Println

// 下面是一个根据 RFC3339 使用相应布局常量格式化时间的基本示例
t := time.Now()
p(t.Format(time.RFC3339))

// 时间解析使用与Format相同的布局值
t1, e := time.Parse(
time.RFC3339,
"2012-11-01T22:08:41+00:00")
p(t1)

// 格式化和解析使用基于示例的布局
// 通常,这些布局会使用时间常量,但也可以提供自定义布局
// 布局必须使用参考时间 Mon Jan 2 15:04:05 MST 2006 来显示格式化/解析给定时间/字符串的模式
// 示例时间必须与所示时间完全一致:2006 年,15 表示小时,星期一表示星期几,等等
p(t.Format("3:04PM"))
p(t.Format("Mon Jan _2 15:04:05 2006"))
p(t.Format("2006-01-02T15:04:05.999999-07:00"))
form := "3 04 PM"
t2, e := time.Parse(form, "8 41 PM")
p(t2)

// 对于纯粹的数字表示,您也可以使用标准字符串格式化提取时间值的组成部分
fmt.Printf("%d-%02d-%02dT%02d:%02d:%02d-00:00\n",
t.Year(), t.Month(), t.Day(),
t.Hour(), t.Minute(), t.Second())

// Parse 会在畸形输入时返回错误信息,解释解析问题
ansic := "Mon Jan _2 15:04:05 2006"
_, e = time.Parse(ansic, "8:41PM")
p(e)
}
log
1
2
3
4
5
6
7
8
9
$ go run time-formatting-parsing.go
2014-04-15T18:00:15-07:00
2012-11-01 22:08:41 +0000 +0000
6:00PM
Tue Apr 15 18:00:15 2014
2014-04-15T18:00:15.161182-07:00
0000-01-01 20:41:00 +0000 UTC
2014-04-15T18:00:15-00:00
parsing time "8:41PM" as "Mon Jan _2 15:04:05 2006": cannot parse "8:41PM" as "Mon"

Random Numbers

Go 的 math/rand/v2 软件包提供伪随机数生成功能。

random-numbers.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
package main

import (
"fmt"
"math/rand/v2"
)

func main() {
// 例如,rand.IntN 返回随机 int n,0 <= n < 100
fmt.Print(rand.IntN(100), ",")
fmt.Print(rand.IntN(100))
fmt.Println()

// rand.Float64 返回 float64 f,0.0 <= f <1.0
fmt.Println(rand.Float64())

// 这可用于生成其他范围的随机浮点数,例如 5.0 <= f' < 10.0
fmt.Print((rand.Float64()*5)+5, ",")
fmt.Print((rand.Float64() * 5) + 5)
fmt.Println()

// 如果需要一个已知的种子,可以创建一个新的 rand.Source 并将其传递给 New 构造函数
// NewPCG 会创建一个新的 PCG 源,需要两个 uint64 数字作为种子
s2 := rand.NewPCG(42, 1024)
r2 := rand.New(s2)
fmt.Print(r2.IntN(100), ",")
fmt.Print(r2.IntN(100))
fmt.Println()

s3 := rand.NewPCG(42, 1024)
r3 := rand.New(s3)
fmt.Print(r3.IntN(100), ",")
fmt.Print(r3.IntN(100))
fmt.Println()
}

运行示例时,生成的某些数字可能会有所不同。

log
1
2
3
4
5
6
$ go run random-numbers.go
68,56
0.8090228139659177
5.840125017402497,6.937056298890035
94,49
94,49

有关 Go 可以提供的其他随机量,请参阅 math/rand/v2 软件包文档

参考链接

Go by Example - Time, Epoch, Time Formatting / Parsing and Random Numbers

https://blog.wty.cool/2024/04/27/go_by_example/Time-Epoch-Time_Formatting_Parsing-Random_Numbers/

作者

孤独小狼

发布于

2024-04-27

更新于

2024-04-27

许可协议

评论