阅读golang time文档

This commit is contained in:
asahi
2025-01-17 14:27:00 +08:00
parent 07ac8271a7
commit 69df0c7a09

View File

@@ -63,6 +63,13 @@
- [format time](#format-time) - [format time](#format-time)
- [使用预定义的time Format](#使用预定义的time-format) - [使用预定义的time Format](#使用预定义的time-format)
- [字符串转日期/时间](#字符串转日期时间) - [字符串转日期/时间](#字符串转日期时间)
- [timeZone](#timezone)
- [ParseInLocation](#parseinlocation)
- [获取UTC时间](#获取utc时间)
- [获取Local时间](#获取local时间)
- [compare time](#compare-time)
- [Sub](#sub)
- [time.Duration](#timeduration)
- [syntax](#syntax) - [syntax](#syntax)
- [iota](#iota) - [iota](#iota)
@@ -1296,11 +1303,177 @@ func main() {
time.Parse除了返回time.Time类型的值之外还返回一个error对象。 time.Parse除了返回time.Time类型的值之外还返回一个error对象。
### timeZone
当formatLayout中不包含时区且被转换的日期字符串中也不包含时区时`time.Parse`方法在转换日期字符串时会使用默认的时区。
> #### 默认时区
> 默认时区为"+0000`时区也被称为UTC时区。
示例如下:
```go
t, err := time.Parse("2006-01-02 15:04:05", "2024-10-25 12:13:14")
if err != nil {
panic(any(err))
}
fmt.Println(t)
```
上述示例输出内容为
```
2024-10-25 12:13:14 +0000 UTC
```
其在未指定时区时默认使用UTC时区。
#### ParseInLocation
如果在转换未包含时区信息的字符串中,使用指定时区,可以调用`ParseInLocation`方法,示例如下
```go
var (
t time.Time
cst *time.Location
err error
)
cst, err = time.LoadLocation("Asia/Shanghai")
if err != nil {
panic(any(err))
}
t, err = time.ParseInLocation("2006-01-02 15:04:05", "2024-10-25 12:13:14", cst)
if err != nil {
panic(any(err))
}
fmt.Println(t)
```
#### 获取UTC时间
通过调用`time.UTC`方法可以将当前时间转换为UTC时间示例如下
```go
now := time.Now()
fmt.Println("current time is ", now)
fmt.Println("current utc time is ", now.UTC())
```
上述示例输出为
```
current time is 2025-01-17 13:46:20.6897373 +0800 CST m=+0.000000001
current utc time is 2025-01-17 05:46:20.6897373 +0000 UTC
```
可见将cst时区转化为utc时区时小时部分`-8`
#### 获取Local时间
相对的将UTC或其他时区的时间转化为本地时区的时间时可以调用`Local`方法
```go
var (
t time.Time
err error
)
t, err = time.ParseInLocation("2006-01-02 15:04:05", "2010-12-18 19:03:32", time.UTC)
if err != nil {
panic(any(err))
}
fmt.Println("utc time is ", t)
fmt.Println("local time is ", t.Local())
```
其输出结果如下:
```
utc time is 2010-12-18 19:03:32 +0000 UTC
local time is 2010-12-19 03:03:32 +0800 CST
```
### compare time
对于日期的比较golang提供了`Before`, `After`, `Equal`方法,
其使用示例如下:
```go
var (
t1 time.Time
t2 time.Time
err error
)
t1, err = time.ParseInLocation("2006-01-02 15:04:05", "2010-12-18 19:03:32", time.UTC)
if err != nil {
panic(any(err))
}
t2, err = time.ParseInLocation("2006-01-02 15:04:05", "2010-12-19 03:03:33", time.Local)
fmt.Println("t1 utc time is ", t1.UTC())
fmt.Println("t2 utc time is ", t2.UTC())
fmt.Println(t1.Equal(t2))
fmt.Println(t1.Before(t2))
fmt.Println(t1.After(t2))
```
其输出为
```
t1 utc time is 2010-12-18 19:03:32 +0000 UTC
t2 utc time is 2010-12-18 19:03:33 +0000 UTC
false
true
false
```
### Sub
除了通过上述方法进行比较外,针对两个`time.Time`类型还支持`Sub`方法,该方法会返回`time.Duration`类型的值。
`Sub`方法的使用示例如下:
```go
var (
t1 time.Time
t2 time.Time
err error
)
t1, err = time.ParseInLocation("2006-01-02 15:04:05", "2010-12-18 19:03:32", time.UTC)
if err != nil {
panic(any(err))
}
t2, err = time.ParseInLocation("2006-01-02 15:04:05", "2010-12-19 03:03:33", time.Local)
fmt.Println("t1 utc time is ", t1.UTC())
fmt.Println("t2 utc time is ", t2.UTC())
fmt.Println("t2 - t1 = ", t2.Sub(t1))
```
上述代码输出为
```
t1 utc time is 2010-12-18 19:03:32 +0000 UTC
t2 utc time is 2010-12-18 19:03:33 +0000 UTC
t2 - t1 = 1s
```
### time.Duration
在golang中`time.Duration`类型定义如下:
```go
type Duration int64
```
在time包中还定义了诸多`time.Duration`类型的常量,示例如下:
```go
const (
Nanosecond Duration = 1
Microsecond = 1000 * Nanosecond
Millisecond = 1000 * Microsecond
Second = 1000 * Millisecond
Minute = 60 * Second
Hour = 60 * Minute
)
```
如上所示,`time.Duration`类型的底层类型为int64并且最小单位为`nanos`
故而,使用`10 * time.Minute`, `3 * time.Hour`等表达式可以定义自己的time.Duration值。
对于`time.Time`类型,支持通过`Add`方法来对时间进行加减,`Add`方法接收一个`time.Duration`类型的参数,返回值为`time.Time`类型
```go
var (
t1 time.Time
err error
)
t1, err = time.ParseInLocation("2006-01-02 15:04:05", "2010-12-18 19:03:32", time.UTC)
if err != nil {
panic(any(err))
}
t2 := t1.Add(time.Hour * 24)
t3 := t1.Add(time.Hour * -24)
fmt.Println(t1)
fmt.Println(t2)
fmt.Println(t3)
```
上述示例的返回结果为
```
2010-12-18 19:03:32 +0000 UTC
2010-12-19 19:03:32 +0000 UTC
2010-12-17 19:03:32 +0000 UTC
```
## syntax ## syntax