初始版本,工具基础类
This commit is contained in:
56
examples/datetime_example.go
Normal file
56
examples/datetime_example.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/go-common/datetime"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// 设置默认时区
|
||||
err := datetime.SetDefaultTimeZone(datetime.AsiaShanghai)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// 获取当前时间
|
||||
now := datetime.Now()
|
||||
fmt.Printf("Current time: %s\n", datetime.FormatDateTime(now))
|
||||
|
||||
// 解析时间字符串
|
||||
t, err := datetime.ParseDateTime("2024-01-01 12:00:00")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Printf("Parsed time: %s\n", datetime.FormatDateTime(t))
|
||||
|
||||
// 时区转换
|
||||
t2, err := datetime.ToTimezone(t, datetime.AmericaNewYork)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Printf("Time in New York: %s\n", datetime.FormatDateTime(t2))
|
||||
|
||||
// Unix时间戳
|
||||
unix := datetime.ToUnix(now)
|
||||
fmt.Printf("Unix timestamp: %d\n", unix)
|
||||
t3 := datetime.FromUnix(unix)
|
||||
fmt.Printf("From Unix: %s\n", datetime.FormatDateTime(t3))
|
||||
|
||||
// 时间计算
|
||||
tomorrow := datetime.AddDays(now, 1)
|
||||
fmt.Printf("Tomorrow: %s\n", datetime.FormatDate(tomorrow))
|
||||
|
||||
// 时间范围
|
||||
startOfDay := datetime.StartOfDay(now)
|
||||
endOfDay := datetime.EndOfDay(now)
|
||||
fmt.Printf("Start of day: %s\n", datetime.FormatDateTime(startOfDay))
|
||||
fmt.Printf("End of day: %s\n", datetime.FormatDateTime(endOfDay))
|
||||
|
||||
// 时间差
|
||||
diff := datetime.DiffDays(now, tomorrow)
|
||||
fmt.Printf("Days difference: %d\n", diff)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user