[TOC]
## time包
### 時間格式化格式
~~~
format := "2006-01-02 15:04:05"
~~~
### 獲取當前時間
~~~
now := time.Now()
fmt.Println(now)
//2024-02-29 11:3 4.41257 +0800 CST m=+0.000129751
//時間戳
timeStamp := now.Unix()
fmt.Println(timeStamp) // 1709176339
fmt.Printf("%T \n", timeStamp) //int64
//毫秒、微妙、納秒
fmt.Println(now.UnixMilli(), now.UnixMicro(), now.UnixNano())
//1709176339194 1709176339194819 1709176339194819000
~~~
### 年月日時分秒、星期、一年的某天
~~~
//獲取年月日
year,month,day := now.Date()
//可以單獨獲取年月日
year1 := now.Year()
month1 := now.Month()
day1 := now.Day()
//獲取時分秒
hour,minute,second := now.Clock()
//可以單獨獲取時分秒
hour1 := now.Hour()
minute1 := now.Minute()
second1 := now.Second()
//一年中的第幾天
yeadDay := now.YearDay()
fmt.Println(yeadDay) //60
//一周中的第幾天
weekDay := now.Weekday()
fmt.Println(weekDay) // Thursday 英語上的周一到周日
// 轉化數字
weekDayInt := int(weekDay)
fmt.Println(weekDayInt) // 4 周日是0 范圍0-6
~~~
### 格式化時間
~~~
now := time.Now()
fmt.Println(now.Format("2006-01-02 15:03:04")) //2024-02-29 12:12:07
fmt.Println(now.Format("2006-01-02")) //2024-02-29
fmt.Println(now.Format("15:03:04")) //12:12:07
fmt.Println(now.Format("2006/01/02 15:04")) //2024/02/29 12:07
fmt.Println(now.Format("15:04 2006/01/02")) //12:07 2024/02/29
~~~
### 設置時區獲取時間
~~~
//加載時區
loc, _ := time.LoadLocation("Asia/Shanghai")
t, _ := time.ParseInLocation(format, nowTimeStr, loc)
fmt.Println(now) //2024-02-29 11:49:17 +0800 CST
~~~
### 指定時間返回
~~~
local, _ := time.LoadLocation("Asia/Shanghai") //local := time.Local
//分別指定年,月,日,時,分,秒,納秒,時區
onlyDay := time.Date(2024, 3, 3, 23, 0, 0, 0, local)
~~~
### 設置時間大于24小時
~~~
//分別指定年,月,日 ,可以設置負數
now := time.Now()//2024-02-29 11:49:17 +0800 CST
addDate := now.AddDate(0,0,1);
fmt.Println(addDate)//2024-03-01 11:49:17 +0800 CST
~~~
### 設置時間小于24小時
~~~
now := time.Now()
//加法 1小時之后
later := now.Add(time.Hour)
fmt.Println(now.Unix(), later.Unix()) //1709187802 1709191402
//減法
sub1 := later.Add(-time.Hour)
sub2 := later.Sub(now)
fmt.Println(sub1.Unix(), sub2) //1707740816 1h0m0s
//判斷兩個時間是否相同,會考慮時區的影響,因此不同時區標準的時間也可以正確比較
isEqual := later.Equal(later)
fmt.Println(isEqual) // true
isAfer := later.After(now)
fmt.Println(isAfer) // true
isBefore := later.Before(now)
fmt.Println(isBefore) // false
//使用ParseDuration
t1, _ := time.ParseDuration("13h4m5s")
after2 := now.Add(t1)
//2024-03-01 03:24:07.560044 +0800 CST m=+47045.000031876
~~~
### time.since
~~~
now := time.Now()
time.Sleep(3 * time.Second)//睡眠3秒
dua := time.Since(now) // 可以用于統計代碼執行時長 3.00147325s
fmt.Println(dua)
~~~
### timer
~~~
tim := time.NewTimer(1 * time.Second)
<-tim.C
fmt.Println(time.Now().Unix())
tim.Stop()
~~~
### ticker
~~~
//本質上是一個channel
ticker := time.Tick(time.Second) //定義一個1秒間隔的定時器
for i := range ticker {
fmt.Println(i) //每秒都會執行的任務
}
~~~
## 封裝
### 日期-->時間戳 調用時注意fmtStr與valueStr格式相同
~~~
func Date2TimeStamp(fmtStr, valueStr, locStr string) int64 {
loc := time.Local
if locStr != "" {
loc, _ = time.LoadLocation(locStr) // 設置時區
}
if fmtStr == "" {
fmtStr = "2006-01-02 15:04:05" //設置時間格式
}
t, _ := time.ParseInLocation(fmtStr, valueStr, loc)
return t.Unix()
}
~~~
### 時間戳-->日期
~~~
func TimeStamp2Date(sec int64, fmtStr string) string {
if fmtStr == "" {
fmtStr = "2006-01-02 15:04:05"
}
return time.Unix(sec, 0).Format(fmtStr)
}
~~~
### 時間日期Time.time轉換

- Go準備工作
- 依賴管理
- Go基礎
- 1、變量和常量
- 2、基本數據類型
- 3、運算符
- 4、流程控制
- 5、數組
- 數組聲明和初始化
- 遍歷
- 數組是值類型
- 6、切片
- 定義
- slice其他內容
- 7、map
- 8、函數
- 函數基礎
- 函數進階
- 9、指針
- 10、結構體
- 類型別名和自定義類型
- 結構體
- 11、接口
- 12、反射
- 13、并發
- 14、網絡編程
- 15、單元測試
- Go常用庫/包
- Context
- time
- strings/strconv
- file
- http
- Go常用第三方包
- Go優化
- Go問題排查
- Go框架
- 基礎知識點的思考
- 面試題
- 八股文
- 操作系統
- 整理一份資料
- interface
- array
- slice
- map
- MUTEX
- RWMUTEX
- Channel
- waitGroup
- context
- reflect
- gc
- GMP和CSP
- Select
- Docker
- 基本命令
- dockerfile
- docker-compose
- rpc和grpc
- consul和etcd
- ETCD
- consul
- gin
- 一些小點
- 樹
- K8s
- ES
- pprof
- mycat
- nginx
- 整理后的面試題
- 基礎
- Map
- Chan
- GC
- GMP
- 并發
- 內存
- 算法
- docker