mirror of
https://gitee.com/shikong-sk/golang-study
synced 2025-02-22 23:12:15 +08:00
docs: time
This commit is contained in:
parent
33bd7657d7
commit
2dff96f6c2
@ -5,6 +5,22 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
// 时间
|
||||
|
||||
func main() {
|
||||
@ -57,7 +73,17 @@ func main() {
|
||||
// 字符串 转 时间对象
|
||||
// 默认为 UTC 时区 需手动指定 或 使用 ParseInLocation
|
||||
fmt.Println(time.Parse("2006-01-02 15:04:05.99 MST", "1970-01-01 08:00:00.00 CST"))
|
||||
|
||||
// 根据 本机时区 转换 时间对象
|
||||
localTime, _ := time.ParseInLocation("2006-01-02 15:04:05.99", "1970-01-01 08:00:00.00", time.Local)
|
||||
fmt.Println(localTime)
|
||||
//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)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user