首次提交:初始化项目代码
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// 字符串解析为时间对象
|
||||
timeStr := "2026-06-03 20:08:09"
|
||||
parsedTime, err := time.Parse("2006-01-02 15:04:05", timeStr)
|
||||
if err != nil {
|
||||
fmt.Println("解析失败:", err)
|
||||
return
|
||||
}
|
||||
fmt.Println("解析后的时间:", parsedTime)
|
||||
|
||||
// 格式化输出
|
||||
formattedTime := parsedTime.Format("2006年01月02日 15:04:05")
|
||||
fmt.Println("格式化后的时间:", formattedTime)
|
||||
|
||||
// RFC3339 标准格式
|
||||
rfc3339Time := parsedTime.Format(time.RFC3339)
|
||||
fmt.Println("RFC3339格式:", rfc3339Time)
|
||||
|
||||
// Unix 时间戳互转
|
||||
unixTimestamp := int64(1780918245)
|
||||
fromUnix := time.Unix(unixTimestamp, 0)
|
||||
fmt.Println("从时间戳解析:", fromUnix)
|
||||
|
||||
unixTs := parsedTime.Unix()
|
||||
fmt.Println("转为时间戳:", unixTs)
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
now := time.Now()
|
||||
fmt.Println("本地时间:", now)
|
||||
|
||||
// UTC 时区
|
||||
utcLoc, _ := time.LoadLocation("UTC")
|
||||
utcTime := now.In(utcLoc)
|
||||
fmt.Println("UTC时间:", utcTime)
|
||||
|
||||
// 纽约时区
|
||||
nyLoc, _ := time.LoadLocation("America/New_York")
|
||||
nyTime := now.In(nyLoc)
|
||||
fmt.Println("纽约时间:", nyTime)
|
||||
|
||||
// 时区偏移
|
||||
_, offset := now.Zone()
|
||||
fmt.Printf("本地时区偏移: %d 小时\n", offset/3600)
|
||||
|
||||
// 固定偏移时区(不依赖IANA数据)
|
||||
estLoc := time.FixedZone("EST", -5*3600)
|
||||
estTime := now.In(estLoc)
|
||||
fmt.Println("EST时间 (FixedZone):", estTime)
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Println("等待 1 秒...")
|
||||
timer := time.NewTimer(1 * time.Second)
|
||||
defer timer.Stop()
|
||||
|
||||
<-timer.C
|
||||
fmt.Println("Timer 触发")
|
||||
|
||||
// 超时控制
|
||||
done := make(chan bool)
|
||||
go func() {
|
||||
time.Sleep(2 * time.Second)
|
||||
done <- true
|
||||
}()
|
||||
|
||||
select {
|
||||
case <-done:
|
||||
fmt.Println("任务完成")
|
||||
case <-time.After(1 * time.Second):
|
||||
fmt.Println("任务超时")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
ticker := time.NewTicker(500 * time.Millisecond)
|
||||
defer ticker.Stop()
|
||||
done := make(chan bool)
|
||||
count := 0
|
||||
|
||||
go func() {
|
||||
for {
|
||||
select {
|
||||
case t := <-ticker.C:
|
||||
count++
|
||||
fmt.Println("触发:", t.Format("15:04:05"))
|
||||
if count >= 5 {
|
||||
done <- true
|
||||
return
|
||||
}
|
||||
case <-done:
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
<-done
|
||||
fmt.Println("Ticker 已停止")
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
base := time.Date(2026, 6, 3, 18, 30, 45, 0, time.UTC)
|
||||
|
||||
// 加减
|
||||
oneDayLater := base.Add(24 * time.Hour)
|
||||
oneHourBefore := base.Add(-time.Hour)
|
||||
|
||||
// 输出使用变量,解决未使用报错
|
||||
fmt.Println("基准时间:", base)
|
||||
fmt.Println("加一天后:", oneDayLater)
|
||||
fmt.Println("减一小时后:", oneHourBefore)
|
||||
|
||||
// 差值
|
||||
diff := oneDayLater.Sub(base)
|
||||
fmt.Println("时间差:", diff)
|
||||
|
||||
// 截断 & 舍入
|
||||
trunc := base.Truncate(time.Hour)
|
||||
round := base.Round(time.Minute)
|
||||
|
||||
fmt.Println("截断到小时:", trunc)
|
||||
fmt.Println("四舍五入到分钟:", round)
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
t1 := time.Date(2026, 6, 3, 18, 30, 0, 0, time.UTC)
|
||||
t2 := time.Date(2026, 6, 3, 18, 31, 0, 0, time.UTC)
|
||||
|
||||
fmt.Println("t1 在 t2 前:", t1.Before(t2))
|
||||
fmt.Println("t2 在 t1 后:", t2.After(t1))
|
||||
fmt.Println("t1 == t2:", t1.Equal(t2))
|
||||
|
||||
// 跨时区比较(同一时刻)
|
||||
utc, _ := time.LoadLocation("UTC")
|
||||
cn, _ := time.LoadLocation("Asia/Shanghai")
|
||||
|
||||
timeUTC := time.Date(2026, 6, 3, 10, 0, 0, 0, utc)
|
||||
timeCN := time.Date(2026, 6, 3, 18, 0, 0, 0, cn)
|
||||
|
||||
fmt.Println("是否同一时刻:", timeUTC.Equal(timeCN)) // true
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// 仅日期
|
||||
dateOnly, _ := time.Parse("2006-01-02", "2026-06-03")
|
||||
fmt.Println("仅日期:", dateOnly)
|
||||
|
||||
// 带纳秒
|
||||
nanos, _ := time.Parse("2006-01-02 15:04:05.999999999", "2026-06-03 18:30:45.123456789")
|
||||
fmt.Println("带纳秒:", nanos)
|
||||
|
||||
// 带时区解析
|
||||
tzTime, _ := time.ParseInLocation("2006-01-02 15:04:05 MST", "2026-06-03 18:30:45 CST", time.Local)
|
||||
fmt.Println("带时区解析:", tzTime)
|
||||
}
|
||||
Reference in New Issue
Block a user