完善gtime示例程序

This commit is contained in:
John
2018-07-22 11:58:38 +08:00
parent f45cc075e6
commit 7935edcc4f
4 changed files with 37 additions and 6 deletions

View File

@ -9,15 +9,11 @@ func main() {
formats := []string{
"Y-m-d H:i:s.u",
"D M d H:i:s T O Y",
// 可以使用转义字符转移有意义的格式字母
"\\T\\i\\m\\e \\i\\s: h:i:s a",
// format格式不支持标准库格式混合相互隔离
"2006-01-02T15:04:05.000000000Z07:00",
}
t := gtime.Now()
for _, f := range formats {
fmt.Println(f)
fmt.Println(t.Format(f))
fmt.Println()
}
}

View File

@ -0,0 +1,15 @@
package main
import (
"fmt"
"gitee.com/johng/gf/g/os/gtime"
)
func main() {
fmt.Println("Date :", gtime.Date())
fmt.Println("Datetime :", gtime.Datetime())
fmt.Println("Second :", gtime.Second())
fmt.Println("Millisecond:", gtime.Millisecond())
fmt.Println("Microsecond:", gtime.Microsecond())
fmt.Println("Nanosecond :", gtime.Nanosecond())
}

View File

@ -14,8 +14,6 @@ func main() {
}
t := gtime.Now()
for _, f := range formats {
fmt.Println(f)
fmt.Println(t.Layout(f))
fmt.Println()
}
}

View File

@ -0,0 +1,22 @@
package main
import (
"fmt"
"gitee.com/johng/gf/g/os/gtime"
"time"
)
func main() {
// 去年今日
fmt.Println(gtime.Now().AddDate(-1, 0, 0).Format("Y-m-d"))
// 去年今日UTC时间
fmt.Println(gtime.Now().AddDate(-1, 0, 0).Format("Y-m-d H:i:s T"))
fmt.Println(gtime.Now().AddDate(-1, 0, 0).UTC().Format("Y-m-d H:i:s T"))
// 下个月1号凌晨0点整
fmt.Println(gtime.Now().AddDate(0, 1, 0).Format("Y-m-d 00:00:00"))
// 2个小时前
fmt.Println(gtime.Now().Add(-time.Hour).Format("Y-m-d H:i:s"))
}