2021-10-05 14:29:27 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2021-10-05 15:25:57 +08:00
|
|
|
func Parse(format string, timeStr string) time.Time {
|
|
|
|
// 根据 本机时区 转换 时间对象
|
|
|
|
localTime, _ := time.ParseInLocation(format, timeStr, time.Local)
|
|
|
|
return localTime
|
|
|
|
}
|
|
|
|
|
|
|
|
func ParseDuration(duration time.Duration) (Y int, M int, D int, H int, m int, s int) {
|
|
|
|
Y = int(duration.Seconds() / (60 * 60 * 24 * 30 * 12))
|
|
|
|
M = int(duration.Seconds()/(60*60*24*30)) - (Y * 12)
|
|
|
|
D = int(duration.Seconds()/(60*60*24)) - (M * 30) - (Y * 30 * 12)
|
|
|
|
H = int(duration.Seconds()/(60*60)) - (D * 24) - (M * 24 * 30) - (Y * 24 * 30 * 12)
|
|
|
|
m = int(duration.Seconds()/60) - (H * 60) - (D * 60 * 24) - (M * 60 * 24 * 30) - (Y * 60 * 24 * 30 * 12)
|
|
|
|
s = int(duration.Seconds()) - (m * 60) - (H * 60 * 60) - (D * 60 * 60 * 24) - (M * 60 * 60 * 24 * 30) - (Y * 60 * 60 * 24 * 30 * 12)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-05 14:29:27 +08:00
|
|
|
// 时间
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
now := time.Now()
|
|
|
|
fmt.Println(now)
|
|
|
|
fmt.Printf("%04d年%02d月%02d日 %02d:%02d:%02d\n", now.Year(), now.Month(), now.Day(), now.Hour(), now.Minute(), now.Second())
|
|
|
|
// 时间戳 (秒)
|
|
|
|
fmt.Printf("%d\n", now.Unix())
|
|
|
|
// 时间戳 (纳秒)
|
|
|
|
fmt.Printf("%d\n", now.UnixNano())
|
|
|
|
|
|
|
|
// 时间戳转时间对象 (秒)
|
|
|
|
unixT := time.Unix(now.Unix(), 0)
|
|
|
|
fmt.Println(unixT)
|
|
|
|
// 时间戳转时间对象 (纳秒)
|
|
|
|
unixNanoT := time.Unix(0, now.UnixNano())
|
|
|
|
fmt.Println(unixNanoT)
|
|
|
|
|
|
|
|
// 时间间隔
|
|
|
|
fmt.Printf("1 秒 = %d 纳秒 \n", time.Second)
|
|
|
|
// 时间计算
|
|
|
|
fmt.Printf("一小时前: %s\n现 在: %s\n一小时后: %s\n", now.Add(-1*time.Hour), now, now.Add(1*time.Hour))
|
|
|
|
|
|
|
|
// 定时器
|
|
|
|
tick := time.Tick(time.Millisecond)
|
|
|
|
var t uint
|
|
|
|
for i := range tick {
|
|
|
|
// 需要执行的定时任务
|
|
|
|
if t >= 3 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
fmt.Println(i)
|
|
|
|
t++
|
|
|
|
}
|
|
|
|
|
|
|
|
// 时间格式化
|
|
|
|
// Golang 中 时间格式化不是 常见的 yyyy-MM-DD HH:mm:ss
|
|
|
|
// 而是 2006-01-02 15:04:05 的形式
|
|
|
|
// Mon Jan 2 15:04:05 -0700 MST 2006
|
|
|
|
// 2006-01-02 15:04:05.999999999 -0700 MST
|
|
|
|
// 2006/06 年份
|
|
|
|
// 01 月份
|
|
|
|
// 02 某天
|
|
|
|
// 03/15 12/24制 小时
|
|
|
|
// 04 分
|
|
|
|
// 05 秒
|
|
|
|
// 99 毫秒/微妙/纳秒
|
|
|
|
fmt.Println(now.Format("2006-01-02 15:04:05.99"))
|
|
|
|
|
|
|
|
// 字符串 转 时间对象
|
|
|
|
// 默认为 UTC 时区 需手动指定 或 使用 ParseInLocation
|
|
|
|
fmt.Println(time.Parse("2006-01-02 15:04:05.99 MST", "1970-01-01 08:00:00.00 CST"))
|
2021-10-05 15:25:57 +08:00
|
|
|
|
2021-10-05 14:29:27 +08:00
|
|
|
// 根据 本机时区 转换 时间对象
|
2021-10-05 15:25:57 +08:00
|
|
|
//localTime, _ := time.ParseInLocation("2006-01-02 15:04:05.99", "1970-01-01 08:00:00.00", time.Local)
|
|
|
|
fmt.Println(Parse("2006-01-02 15:04:05.99", "1970-01-01 08:00:00.00"))
|
|
|
|
|
|
|
|
// 时间相减
|
|
|
|
duration := now.Sub(now.Add(-1 * time.Hour))
|
|
|
|
Y, M, D, H, m, s := ParseDuration(duration)
|
|
|
|
fmt.Printf("相差 %02d年%02d月%02d日 %02d时%02d分%02d秒\n", Y, M, D, H, m, s)
|
|
|
|
|
|
|
|
// 程序 休眠/等待 1 秒
|
|
|
|
fmt.Println("休眠 1 秒")
|
|
|
|
time.Sleep(1 * time.Second)
|
2021-10-05 14:29:27 +08:00
|
|
|
}
|