阅读golang文档
This commit is contained in:
@@ -58,6 +58,11 @@
|
|||||||
- [golang time](#golang-time)
|
- [golang time](#golang-time)
|
||||||
- [get current time](#get-current-time)
|
- [get current time](#get-current-time)
|
||||||
- [monotonic clock](#monotonic-clock)
|
- [monotonic clock](#monotonic-clock)
|
||||||
|
- [获取年月日时分秒](#获取年月日时分秒)
|
||||||
|
- [time.Date](#timedate)
|
||||||
|
- [format time](#format-time)
|
||||||
|
- [使用预定义的time Format](#使用预定义的time-format)
|
||||||
|
- [字符串转日期/时间](#字符串转日期时间)
|
||||||
- [syntax](#syntax)
|
- [syntax](#syntax)
|
||||||
- [iota](#iota)
|
- [iota](#iota)
|
||||||
|
|
||||||
@@ -1163,6 +1168,139 @@ fmt.Println(time.Now())
|
|||||||
|
|
||||||
通过使用monotonic clock,如果在两次调用`time.Now`间隔5min的场景下,即使在调用的5分钟间隙内认为修改系统时间,两次调用`time.Now`返回值的差额仍然为5min。
|
通过使用monotonic clock,如果在两次调用`time.Now`间隔5min的场景下,即使在调用的5分钟间隙内认为修改系统时间,两次调用`time.Now`返回值的差额仍然为5min。
|
||||||
|
|
||||||
|
#### 获取年月日时分秒
|
||||||
|
golang `time.Time`类提供多个方法用于获取时间中的各个部分,示例如下:
|
||||||
|
```go
|
||||||
|
...
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
currentTime := time.Now()
|
||||||
|
fmt.Println("The time is", currentTime)
|
||||||
|
|
||||||
|
fmt.Println("The year is", currentTime.Year())
|
||||||
|
fmt.Println("The month is", currentTime.Month())
|
||||||
|
fmt.Println("The day is", currentTime.Day())
|
||||||
|
fmt.Println("The hour is", currentTime.Hour())
|
||||||
|
fmt.Println("The minute is", currentTime.Hour())
|
||||||
|
fmt.Println("The second is", currentTime.Second())
|
||||||
|
}
|
||||||
|
```
|
||||||
|
上述示例的运行结果为
|
||||||
|
```
|
||||||
|
The time is 2025-01-16 19:01:50.6266158 +0800 CST m=+0.000000001
|
||||||
|
The year is 2025
|
||||||
|
The month is January
|
||||||
|
The day is 16
|
||||||
|
The hour is 19
|
||||||
|
The minute is 19
|
||||||
|
The second is 50
|
||||||
|
```
|
||||||
|
|
||||||
|
其中,`time.Month()`方法的打印结果为字符串而不是数字,实际上`Month`方法的返回值为`time.Month`类型。
|
||||||
|
|
||||||
|
`time.Month`类型的定义如下:
|
||||||
|
```go
|
||||||
|
type Month int
|
||||||
|
```
|
||||||
|
|
||||||
|
#### time.Date
|
||||||
|
除了通过time.Now获取当前时间外,time包还支持通过time.Date获取指定时间,该方法会返回一个`time.Time`类型,使用示例如下:
|
||||||
|
```go
|
||||||
|
...
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
theTime := time.Date(2021, 8, 15, 14, 30, 45, 100, time.Local)
|
||||||
|
fmt.Println("The time is", theTime)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
`time.Date`方法接收参数如下
|
||||||
|
- 年
|
||||||
|
- 月
|
||||||
|
- 日
|
||||||
|
- 时
|
||||||
|
- 分
|
||||||
|
- 秒
|
||||||
|
- `nanos`
|
||||||
|
- 该时间表示的时区
|
||||||
|
|
||||||
|
### format time
|
||||||
|
`time.Time`类型提供了`Format`方法,该方法接收一个string类型的layout,layout用于定义日期和时间如何展示。
|
||||||
|
|
||||||
|
在golang中,指定time格式的方式和其他语言不同。在go中,通过`表达指定时间的字符串`来定义日期时间格式。
|
||||||
|
|
||||||
|
例如在定义四位的年部分时,可以使用`2006`。根据该种格式表示方法,表达格式的字符串和实际输出的字符串相同。
|
||||||
|
|
||||||
|
> 在golang中,日期和时间表示的特定时间点为`01/02 03:04:05PM '06 -0700`。
|
||||||
|
|
||||||
|
该指定时间各段的值依次递增,分别为
|
||||||
|
- month 01
|
||||||
|
- day of month 02
|
||||||
|
- hour 03`PM` (如果想要展示24小时制而不是12小时制,可以使用15代替03)
|
||||||
|
- minute 04
|
||||||
|
- second 05
|
||||||
|
- year 06 `2006`
|
||||||
|
- time zone 07
|
||||||
|
|
||||||
|
示例如下所示:
|
||||||
|
```go
|
||||||
|
func main() {
|
||||||
|
fmt.Println(time.Date(1997, 10, 25, 16, 2, 28, 100, time.Local).Format("2006-01-02 15:04:05"))
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
代码运行输出如下:
|
||||||
|
```go
|
||||||
|
1997-10-25 16:02:28
|
||||||
|
```
|
||||||
|
如果想要使用12小时制,可以按照如下示例
|
||||||
|
```go
|
||||||
|
func main() {
|
||||||
|
fmt.Println(time.Date(1997, 10, 25, 4, 2, 28, 100, time.Local).Format("2006-01-02 03:04:05 pm"))
|
||||||
|
fmt.Println(time.Date(1997, 10, 25, 16, 2, 28, 100, time.Local).Format("2006-01-02 03:04:05 pm"))
|
||||||
|
}
|
||||||
|
```
|
||||||
|
其输出为
|
||||||
|
```go
|
||||||
|
1997-10-25 04:02:28 am
|
||||||
|
1997-10-25 04:02:28 pm
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 使用预定义的time Format
|
||||||
|
golang的time包中定义了`RFC 3339`的格式,`RFC 3339`定义了internet中被广泛使用的时间戳格式。
|
||||||
|
|
||||||
|
每个预定义的格式都是一个string类型字符串,对于`RFC 3339`,time包中存在两种格式:
|
||||||
|
- `time.RFC3339`: `2006-01-02T15:04:05Z07:00`
|
||||||
|
- `time.RFC3339Nano` : `2006-01-02T15:04:05.999999999Z07:00`
|
||||||
|
|
||||||
|
`time.RFC3339Nano`相比`time.RFC3339`,其在格式中包含了nanos
|
||||||
|
|
||||||
|
|
||||||
|
### 字符串转日期/时间
|
||||||
|
golang提供了`time.Parse`方法将时间转化为`time.Time`类型。
|
||||||
|
|
||||||
|
`time.Parse`的使用示例如下所示:
|
||||||
|
```go
|
||||||
|
...
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
timeString := "2021-08-15 02:30:45"
|
||||||
|
theTime, err := time.Parse("2006-01-02 03:04:05", timeString)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Could not parse time:", err)
|
||||||
|
}
|
||||||
|
fmt.Println("The time is", theTime)
|
||||||
|
|
||||||
|
fmt.Println(theTime.Format(time.RFC3339Nano))
|
||||||
|
}
|
||||||
|
```
|
||||||
|
time.Parse除了返回time.Time类型的值之外,还返回一个error对象。
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## syntax
|
## syntax
|
||||||
|
|||||||
Reference in New Issue
Block a user