2025-03-19 14:36:09 +08:00

137 lines
4.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

在 Go 语言中,`time` 包提供了丰富的时间和日期处理功能,广泛应用于时间的获取、格式化、解析以及时间间隔的操作。以下是一些常用的 `time` 包函数及其使用方法:
---
### **1. 获取当前时间**
- **`Now() Time`**
返回当前的本地时间。
示例:
```go
now := time.Now()
fmt.Println("当前时间:", now) // 输出: 当前时间: 2025-03-17 14:30:00.xxx
```
---
### **2. 时间的创建**
- **`Date(year int, month Month, day, hour, min, sec, nsec int, loc *Location) Time`**
根据指定的年、月、日、时、分、秒、纳秒和时区创建一个时间对象。
示例:
```go
t := time.Date(2025, time.March, 17, 14, 30, 0, 0, time.Local)
fmt.Println("指定时间:", t) // 输出: 指定时间: 2025-03-17 14:30:00 +0800 CST
```
- **`Unix(sec int64, nsec int64) Time`**
根据 Unix 时间戳(秒数和纳秒数)创建时间对象。
示例:
```go
t := time.Unix(1710651000, 0)
fmt.Println("Unix时间对应时间:", t) // 输出: Unix时间对应时间: 2025-03-17 14:30:00 +0800 CST
```
---
### **3. 时间格式化与解析**
- **`Format(layout string) string`**
将时间对象格式化为字符串,`layout` 是格式模板,必须使用固定的参考时间 `2006-01-02 15:04:05` 的形式。
示例:
```go
t := time.Now()
formatted := t.Format("2006-01-02 15:04:05")
fmt.Println("格式化时间:", formatted) // 输出: 格式化时间: 2025-03-17 14:30:00
```
- **`Parse(layout, value string) (Time, error)`**
将字符串解析为时间对象,`layout` 是格式模板。
示例:
```go
t, err := time.Parse("2006-01-02 15:04:05", "2025-03-17 14:30:00")
if err != nil {
fmt.Println("解析错误:", err)
} else {
fmt.Println("解析时间:", t) // 输出: 解析时间: 2025-03-17 14:30:00 +0000 UTC
}
```
- **`ParseInLocation(layout, value string, loc *Location) (Time, error)`**
类似于 `Parse`,但允许指定时区。
示例:
```go
loc, _ := time.LoadLocation("Asia/Shanghai")
t, _ := time.ParseInLocation("2006-01-02 15:04:05", "2025-03-17 14:30:00", loc)
fmt.Println("解析时间:", t) // 输出: 解析时间: 2025-03-17 14:30:00 +0800 CST
```
---
### **4. 时间计算**
- **`Add(d Duration) Time`**
在当前时间上增加一个时间间隔(`Duration`)。
示例:
```go
t := time.Now()
future := t.Add(24 * time.Hour)
fmt.Println("一天后的时间:", future)
```
- **`Sub(u Time) Duration`**
计算两个时间之间的差值,返回一个 `Duration`。
示例:
```go
t1 := time.Now()
t2 := t1.Add(2 * time.Hour)
duration := t2.Sub(t1)
fmt.Println("时间差:", duration) // 输出: 时间差: 2h0m0s
```
- **`Since(t Time) Duration`**
返回从指定时间到当前时间的时间间隔。
示例:
```go
t := time.Date(2025, time.March, 16, 14, 30, 0, 0, time.Local)
duration := time.Since(t)
fmt.Println("距离现在的时间:", duration) // 输出: 距离现在的时间: 24h0m0s
```
---
### **5. 时间间隔Duration**
- **`time.Duration`**
表示时间间隔,通常以纳秒为单位。可以通过常量如 `time.Second`、`time.Minute` 等表示固定的时间间隔。
示例:
```go
duration := 2*time.Hour + 30*time.Minute
fmt.Println("时间间隔:", duration) // 输出: 时间间隔: 2h30m0s
```
---
### **6. 定时器与休眠**
- **`Sleep(d Duration)`**
让当前 Goroutine 进入休眠状态,持续指定的时间间隔。
示例:
```go
fmt.Println("开始休眠...")
time.Sleep(2 * time.Second)
fmt.Println("休眠结束!")
```
- **`After(d Duration) <-chan Time`**
返回一个通道,在指定的时间间隔后发送当前时间。
示例:
```go
fmt.Println("等待2秒...")
<-time.After(2 * time.Second)
fmt.Println("2秒已过!")
```
---
### **7. 时间相关常量**
- **`time.Second`、`time.Minute`、`time.Hour` 等**
提供了常用的时间单位常量,便于时间间隔的计算。
示例:
```go
fmt.Println("一小时有多少秒:", time.Hour.Seconds()) // 输出: 一小时有多少秒: 3600
```