mirror of
https://gitee.com/johng/gf
synced 2026-06-06 02:25:47 +08:00
update gtime example
This commit is contained in:
@ -7,117 +7,116 @@
|
||||
package gtime_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/v2/os/gtime"
|
||||
"time"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/v2/os/gtime"
|
||||
)
|
||||
|
||||
// New creates and returns a Time object with given parameter.
|
||||
// The optional parameter can be type of: time.Time/*time.Time, string or integer.
|
||||
func ExampleSetTimeZone() {
|
||||
gtime.SetTimeZone("Asia/Shanghai")
|
||||
fmt.Println(gtime.Datetime())
|
||||
gtime.SetTimeZone("Asia/Shanghai")
|
||||
fmt.Println(gtime.Datetime())
|
||||
|
||||
gtime.SetTimeZone("Asia/Tokyo")
|
||||
fmt.Println(gtime.Datetime())
|
||||
// May Output:
|
||||
// 2018-08-08 08:08:08
|
||||
// 2018-08-08 09:08:08
|
||||
gtime.SetTimeZone("Asia/Tokyo")
|
||||
fmt.Println(gtime.Datetime())
|
||||
// May Output:
|
||||
// 2018-08-08 08:08:08
|
||||
// 2018-08-08 09:08:08
|
||||
}
|
||||
|
||||
func ExampleTimestamp() {
|
||||
fmt.Println(gtime.Timestamp())
|
||||
fmt.Println(gtime.Timestamp())
|
||||
|
||||
// May Output:
|
||||
// 1636359252
|
||||
// May Output:
|
||||
// 1636359252
|
||||
}
|
||||
|
||||
func ExampleTimestampMilli() {
|
||||
fmt.Println(gtime.TimestampMilli())
|
||||
fmt.Println(gtime.TimestampMilli())
|
||||
|
||||
// May Output:
|
||||
// 1636359252000
|
||||
// May Output:
|
||||
// 1636359252000
|
||||
}
|
||||
|
||||
func ExampleTimestampMicro() {
|
||||
fmt.Println(gtime.TimestampMicro())
|
||||
fmt.Println(gtime.TimestampMicro())
|
||||
|
||||
// May Output:
|
||||
// 1636359252000000
|
||||
// May Output:
|
||||
// 1636359252000000
|
||||
}
|
||||
|
||||
func ExampleTimestampNano() {
|
||||
fmt.Println(gtime.TimestampNano())
|
||||
fmt.Println(gtime.TimestampNano())
|
||||
|
||||
// May Output:
|
||||
// 1636359252000000000
|
||||
// May Output:
|
||||
// 1636359252000000000
|
||||
}
|
||||
|
||||
func ExampleTimestampStr() {
|
||||
fmt.Println(gtime.TimestampStr())
|
||||
fmt.Println(gtime.TimestampStr())
|
||||
|
||||
// May Output:
|
||||
// 1636359252
|
||||
// May Output:
|
||||
// 1636359252
|
||||
}
|
||||
|
||||
func ExampleDate() {
|
||||
fmt.Println(gtime.Date())
|
||||
fmt.Println(gtime.Date())
|
||||
|
||||
// May Output:
|
||||
// 2006-01-02
|
||||
// May Output:
|
||||
// 2006-01-02
|
||||
}
|
||||
|
||||
func ExampleDatetime() {
|
||||
fmt.Println(gtime.Datetime())
|
||||
fmt.Println(gtime.Datetime())
|
||||
|
||||
// May Output:
|
||||
// 2006-01-02 15:04:05
|
||||
// May Output:
|
||||
// 2006-01-02 15:04:05
|
||||
}
|
||||
|
||||
func ExampleISO8601() {
|
||||
fmt.Println(gtime.ISO8601())
|
||||
fmt.Println(gtime.ISO8601())
|
||||
|
||||
// May Output:
|
||||
// 2006-01-02T15:04:05-07:00
|
||||
// May Output:
|
||||
// 2006-01-02T15:04:05-07:00
|
||||
}
|
||||
|
||||
func ExampleRFC822() {
|
||||
fmt.Println(gtime.RFC822())
|
||||
fmt.Println(gtime.RFC822())
|
||||
|
||||
// May Output:
|
||||
// Mon, 02 Jan 06 15:04 MST
|
||||
// May Output:
|
||||
// Mon, 02 Jan 06 15:04 MST
|
||||
}
|
||||
|
||||
func ExampleStrToTime() {
|
||||
res, _ := gtime.StrToTime("2006-01-02T15:04:05-07:00", "Y-m-d H:i:s")
|
||||
fmt.Println(res)
|
||||
res, _ := gtime.StrToTime("2006-01-02T15:04:05-07:00", "Y-m-d H:i:s")
|
||||
fmt.Println(res)
|
||||
|
||||
// May Output:
|
||||
// 2006-01-02 15:04:05
|
||||
// May Output:
|
||||
// 2006-01-02 15:04:05
|
||||
}
|
||||
|
||||
func ExampleConvertZone() {
|
||||
res, _ := gtime.ConvertZone("2006-01-02 15:04:05", "Asia/Tokyo", "Asia/Shanghai")
|
||||
fmt.Println(res)
|
||||
res, _ := gtime.ConvertZone("2006-01-02 15:04:05", "Asia/Tokyo", "Asia/Shanghai")
|
||||
fmt.Println(res)
|
||||
|
||||
// Output:
|
||||
// 2006-01-02 16:04:05
|
||||
// Output:
|
||||
// 2006-01-02 16:04:05
|
||||
}
|
||||
|
||||
func ExampleStrToTimeFormat() {
|
||||
res, _ := gtime.StrToTimeFormat("2006-01-02 15:04:05", "Y-m-d H:i:s")
|
||||
fmt.Println(res)
|
||||
res, _ := gtime.StrToTimeFormat("2006-01-02 15:04:05", "Y-m-d H:i:s")
|
||||
fmt.Println(res)
|
||||
|
||||
// Output:
|
||||
// 2006-01-02 15:04:05
|
||||
// Output:
|
||||
// 2006-01-02 15:04:05
|
||||
}
|
||||
|
||||
func ExampleStrToTimeLayout() {
|
||||
res, _ := gtime.StrToTimeLayout("2018-08-08", "2006-01-02")
|
||||
fmt.Println(res)
|
||||
res, _ := gtime.StrToTimeLayout("2018-08-08", "2006-01-02")
|
||||
fmt.Println(res)
|
||||
|
||||
// Output:
|
||||
// 2018-08-08 00:00:00
|
||||
// Output:
|
||||
// 2018-08-08 00:00:00
|
||||
}
|
||||
|
||||
// ParseDuration parses a duration string.
|
||||
@ -128,145 +127,127 @@ func ExampleStrToTimeLayout() {
|
||||
//
|
||||
// Very note that it supports unit "d" more than function time.ParseDuration.
|
||||
func ExampleParseDuration() {
|
||||
res, _ := gtime.ParseDuration("+10h")
|
||||
fmt.Println(res)
|
||||
res, _ := gtime.ParseDuration("+10h")
|
||||
fmt.Println(res)
|
||||
|
||||
// Output:
|
||||
// 10h0m0s
|
||||
// Output:
|
||||
// 10h0m0s
|
||||
}
|
||||
|
||||
func ExampleTime_Format() {
|
||||
timeLayout := "2006-01-02 15:04:05"
|
||||
timer1, _ := time.Parse(timeLayout, "2018-08-08 08:08:08")
|
||||
gt1 := gtime.New(timer1)
|
||||
gt1 := gtime.New("2018-08-08 08:08:08")
|
||||
|
||||
fmt.Println(gt1.Format("Y-m-d"))
|
||||
fmt.Println(gt1.Format("Y-m-d"))
|
||||
fmt.Println(gt1.Format("l"))
|
||||
fmt.Println(gt1.Format("F j, Y, g:i a"))
|
||||
fmt.Println(gt1.Format("j, n, Y"))
|
||||
fmt.Println(gt1.Format("h-i-s, j-m-y, it is w Day z"))
|
||||
fmt.Println(gt1.Format("D M j G:i:s T Y"))
|
||||
|
||||
// Output:
|
||||
// 2018-08-08
|
||||
// Output:
|
||||
// 2018-08-08
|
||||
// Wednesday
|
||||
// August 8, 2018, 8:08 am
|
||||
// 8, 8, 2018
|
||||
// 08-08-08, 8-08-18, 0831 0808 3 Wedam18 219
|
||||
// Wed Aug 8 8:08:08 CST 2018
|
||||
}
|
||||
|
||||
func ExampleTime_FormatNew() {
|
||||
timeLayout := "2006-01-02 15:04:05"
|
||||
timer1, _ := time.Parse(timeLayout, "2018-08-08 08:08:08")
|
||||
gt1 := gtime.New(timer1)
|
||||
gt1 := gtime.New("2018-08-08 08:08:08")
|
||||
|
||||
fmt.Println(gt1.FormatNew("Y-m-d"))
|
||||
fmt.Println(gt1.FormatNew("Y-m-d"))
|
||||
fmt.Println(gt1.FormatNew("Y-m-d H:i"))
|
||||
|
||||
// Output:
|
||||
// 2018-08-08 00:00:00
|
||||
// Output:
|
||||
// 2018-08-08 00:00:00
|
||||
// 2018-08-08 08:08:00
|
||||
}
|
||||
|
||||
func ExampleTime_FormatTo() {
|
||||
timeLayout := "2006-01-02 15:04:05"
|
||||
timer1, _ := time.Parse(timeLayout, "2018-08-08 08:08:08")
|
||||
gt1 := gtime.New(timer1)
|
||||
gt1 := gtime.New("2018-08-08 08:08:08")
|
||||
|
||||
fmt.Println(gt1.FormatTo("Y-m-d"))
|
||||
fmt.Println(gt1.FormatTo("Y-m-d"))
|
||||
|
||||
// Output:
|
||||
// 2018-08-08 00:00:00
|
||||
// Output:
|
||||
// 2018-08-08 00:00:00
|
||||
}
|
||||
|
||||
func ExampleTime_Layout() {
|
||||
timeLayout := "2006-01-02 15:04:05"
|
||||
timer1, _ := time.Parse(timeLayout, "2018-08-08 08:08:08")
|
||||
gt1 := gtime.New(timer1)
|
||||
gt1 := gtime.New("2018-08-08 08:08:08")
|
||||
|
||||
fmt.Println(gt1.Layout("2006-01-02"))
|
||||
fmt.Println(gt1.Layout("2006-01-02"))
|
||||
|
||||
// Output:
|
||||
// 2018-08-08
|
||||
// Output:
|
||||
// 2018-08-08
|
||||
}
|
||||
|
||||
func ExampleTime_LayoutNew() {
|
||||
timeLayout := "2006-01-02 15:04:05"
|
||||
timer1, _ := time.Parse(timeLayout, "2018-08-08 08:08:08")
|
||||
gt1 := gtime.New(timer1)
|
||||
gt1 := gtime.New("2018-08-08 08:08:08")
|
||||
|
||||
fmt.Println(gt1.LayoutNew("2006-01-02"))
|
||||
fmt.Println(gt1.LayoutNew("2006-01-02"))
|
||||
|
||||
// Output:
|
||||
// 2018-08-08 00:00:00
|
||||
// Output:
|
||||
// 2018-08-08 00:00:00
|
||||
}
|
||||
|
||||
func ExampleTime_LayoutTo() {
|
||||
timeLayout := "2006-01-02 15:04:05"
|
||||
timer1, _ := time.Parse(timeLayout, "2018-08-08 08:08:08")
|
||||
gt1 := gtime.New(timer1)
|
||||
gt1 := gtime.New("2018-08-08 08:08:08")
|
||||
|
||||
fmt.Println(gt1.LayoutTo("2006-01-02"))
|
||||
fmt.Println(gt1.LayoutTo("2006-01-02"))
|
||||
|
||||
// Output:
|
||||
// 2018-08-08 00:00:00
|
||||
// Output:
|
||||
// 2018-08-08 00:00:00
|
||||
}
|
||||
|
||||
func ExampleTime_IsLeapYear() {
|
||||
timeLayout := "2006-01-02 15:04:05"
|
||||
timer1, _ := time.Parse(timeLayout, "2018-08-08 08:08:08")
|
||||
gt1 := gtime.New(timer1)
|
||||
gt1 := gtime.New("2018-08-08 08:08:08")
|
||||
|
||||
fmt.Println(gt1.IsLeapYear())
|
||||
fmt.Println(gt1.IsLeapYear())
|
||||
|
||||
// Output:
|
||||
// false
|
||||
// Output:
|
||||
// false
|
||||
}
|
||||
|
||||
func ExampleTime_DayOfYear() {
|
||||
timeLayout := "2006-01-02 15:04:05"
|
||||
timer1, _ := time.Parse(timeLayout, "2018-01-08 08:08:08")
|
||||
gt1 := gtime.New(timer1)
|
||||
gt1 := gtime.New("2018-01-08 08:08:08")
|
||||
|
||||
fmt.Println(gt1.DayOfYear())
|
||||
fmt.Println(gt1.DayOfYear())
|
||||
|
||||
// Output:
|
||||
// 7
|
||||
// Output:
|
||||
// 7
|
||||
}
|
||||
|
||||
// DaysInMonth returns the day count of current month.
|
||||
func ExampleTime_DaysInMonth() {
|
||||
timeLayout := "2006-01-02 15:04:05"
|
||||
timer1, _ := time.Parse(timeLayout, "2018-08-08 08:08:08")
|
||||
gt1 := gtime.New(timer1)
|
||||
gt1 := gtime.New("2018-08-08 08:08:08")
|
||||
|
||||
fmt.Println(gt1.DaysInMonth())
|
||||
fmt.Println(gt1.DaysInMonth())
|
||||
|
||||
// Output:
|
||||
// 31
|
||||
// Output:
|
||||
// 31
|
||||
}
|
||||
|
||||
// WeeksOfYear returns the point of current week for the year.
|
||||
func ExampleTime_WeeksOfYear() {
|
||||
timeLayout := "2006-01-02 15:04:05"
|
||||
timer1, _ := time.Parse(timeLayout, "2018-01-08 08:08:08")
|
||||
gt1 := gtime.New(timer1)
|
||||
gt1 := gtime.New("2018-01-08 08:08:08")
|
||||
|
||||
fmt.Println(gt1.WeeksOfYear())
|
||||
fmt.Println(gt1.WeeksOfYear())
|
||||
|
||||
// Output:
|
||||
// 2
|
||||
}
|
||||
|
||||
func ExampleTime_String() {
|
||||
timeLayout := "2006-01-02 15:04:05"
|
||||
timer1, _ := time.Parse(timeLayout, "2018-01-08 08:08:08")
|
||||
gt1 := gtime.New(timer1)
|
||||
|
||||
fmt.Println(gt1.String())
|
||||
|
||||
// Output:
|
||||
// 2018-01-08 08:08:08
|
||||
// Output:
|
||||
// 2
|
||||
}
|
||||
|
||||
func ExampleTime_ToZone() {
|
||||
gt1 := gtime.Now()
|
||||
gt2, _ := gt1.ToZone("Asia/Shanghai")
|
||||
gt3, _ := gt1.ToZone("Asia/Tokyo")
|
||||
gt1 := gtime.Now()
|
||||
gt2, _ := gt1.ToZone("Asia/Shanghai")
|
||||
gt3, _ := gt1.ToZone("Asia/Tokyo")
|
||||
|
||||
fmt.Println(gt2)
|
||||
fmt.Println(gt3)
|
||||
fmt.Println(gt2)
|
||||
fmt.Println(gt3)
|
||||
|
||||
// May Output:
|
||||
// 2021-11-11 17:10:10
|
||||
// 2021-11-11 18:10:10
|
||||
// May Output:
|
||||
// 2021-11-11 17:10:10
|
||||
// 2021-11-11 18:10:10
|
||||
}
|
||||
|
||||
|
||||
@ -7,262 +7,240 @@
|
||||
package gtime_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/v2/os/gtime"
|
||||
"reflect"
|
||||
"time"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/v2/os/gtime"
|
||||
"reflect"
|
||||
"time"
|
||||
)
|
||||
|
||||
// New creates and returns a Time object with given parameter.
|
||||
// The optional parameter can be type of: time.Time/*time.Time, string or integer.
|
||||
func ExampleNew() {
|
||||
timeLayout := "2006-01-02 15:04:05"
|
||||
curTime := "2018-08-08 08:08:08"
|
||||
timer, _ := time.Parse(timeLayout, curTime)
|
||||
t1 := gtime.New(timer)
|
||||
t2 := gtime.New(&timer)
|
||||
t3 := gtime.New(curTime, "Y-m-d H:i:s")
|
||||
t4 := gtime.New(curTime)
|
||||
t5 := gtime.New(1533686888)
|
||||
curTime := "2018-08-08 08:08:08"
|
||||
timer, _ := time.Parse("2006-01-02 15:04:05", curTime)
|
||||
t1 := gtime.New(&timer)
|
||||
t2 := gtime.New(curTime)
|
||||
t3 := gtime.New(curTime, "Y-m-d H:i:s")
|
||||
t4 := gtime.New(curTime)
|
||||
t5 := gtime.New(1533686888)
|
||||
|
||||
fmt.Println(t1)
|
||||
fmt.Println(t2)
|
||||
fmt.Println(t3)
|
||||
fmt.Println(t4)
|
||||
fmt.Println(t5)
|
||||
fmt.Println(t1)
|
||||
fmt.Println(t2)
|
||||
fmt.Println(t3)
|
||||
fmt.Println(t4)
|
||||
fmt.Println(t5)
|
||||
|
||||
// Output:
|
||||
// 2018-08-08 08:08:08
|
||||
// 2018-08-08 08:08:08
|
||||
// 2018-08-08 08:08:08
|
||||
// 2018-08-08 08:08:08
|
||||
// 2018-08-08 08:08:08
|
||||
// Output:
|
||||
// 2018-08-08 08:08:08
|
||||
// 2018-08-08 08:08:08
|
||||
// 2018-08-08 08:08:08
|
||||
// 2018-08-08 08:08:08
|
||||
// 2018-08-08 08:08:08
|
||||
}
|
||||
|
||||
// Now creates and returns a time object of now.
|
||||
func ExampleNow() {
|
||||
t := gtime.Now()
|
||||
fmt.Println(t)
|
||||
t := gtime.Now()
|
||||
fmt.Println(t)
|
||||
|
||||
// May Output:
|
||||
// 2021-11-06 13:41:08
|
||||
// May Output:
|
||||
// 2021-11-06 13:41:08
|
||||
}
|
||||
|
||||
// NewFromTime creates and returns a Time object with given time.Time object.
|
||||
func ExampleNewFromTime() {
|
||||
timeLayout := "2006-01-02 15:04:05"
|
||||
curTime := "2018-08-08 08:08:08"
|
||||
timer, _ := time.Parse(timeLayout, curTime)
|
||||
nTime := gtime.NewFromTime(timer)
|
||||
timer, _ := time.Parse("2006-01-02 15:04:05", "2018-08-08 08:08:08")
|
||||
nTime := gtime.NewFromTime(timer)
|
||||
|
||||
fmt.Println(nTime)
|
||||
fmt.Println(nTime)
|
||||
|
||||
// Output:
|
||||
// 2018-08-08 08:08:08
|
||||
// Output:
|
||||
// 2018-08-08 08:08:08
|
||||
}
|
||||
|
||||
// NewFromStr creates and returns a Time object with given string.
|
||||
// Note that it returns nil if there's error occurs.
|
||||
func ExampleNewFromStr() {
|
||||
t := gtime.NewFromStr("2018-08-08 08:08:08")
|
||||
t := gtime.NewFromStr("2018-08-08 08:08:08")
|
||||
|
||||
fmt.Println(t)
|
||||
fmt.Println(t)
|
||||
|
||||
// Output:
|
||||
// 2018-08-08 08:08:08
|
||||
// Output:
|
||||
// 2018-08-08 08:08:08
|
||||
}
|
||||
|
||||
// NewFromStrFormat creates and returns a Time object with given string and
|
||||
// custom format like: Y-m-d H:i:s.
|
||||
// Note that it returns nil if there's error occurs.
|
||||
func ExampleNewFromStrFormat() {
|
||||
t := gtime.NewFromStrFormat("2018-08-08 08:08:08", "Y-m-d H:i:s")
|
||||
fmt.Println(t)
|
||||
t := gtime.NewFromStrFormat("2018-08-08 08:08:08", "Y-m-d H:i:s")
|
||||
fmt.Println(t)
|
||||
|
||||
// Output:
|
||||
// 2018-08-08 08:08:08
|
||||
// Output:
|
||||
// 2018-08-08 08:08:08
|
||||
}
|
||||
|
||||
// NewFromStrLayout creates and returns a Time object with given string and
|
||||
// stdlib layout like: 2006-01-02 15:04:05.
|
||||
// Note that it returns nil if there's error occurs.
|
||||
func ExampleNewFromStrLayout() {
|
||||
t := gtime.NewFromStrLayout("2018-08-08 08:08:08", "2006-01-02 15:04:05")
|
||||
fmt.Println(t)
|
||||
t := gtime.NewFromStrLayout("2018-08-08 08:08:08", "2006-01-02 15:04:05")
|
||||
fmt.Println(t)
|
||||
|
||||
// Output:
|
||||
// 2018-08-08 08:08:08
|
||||
// Output:
|
||||
// 2018-08-08 08:08:08
|
||||
}
|
||||
|
||||
// NewFromTimeStamp creates and returns a Time object with given timestamp,
|
||||
// which can be in seconds to nanoseconds.
|
||||
// Eg: 1600443866 and 1600443866199266000 are both considered as valid timestamp number.
|
||||
func ExampleNewFromTimeStamp() {
|
||||
t1 := gtime.NewFromTimeStamp(1533686888)
|
||||
t2 := gtime.NewFromTimeStamp(1533686888000)
|
||||
t1 := gtime.NewFromTimeStamp(1533686888)
|
||||
t2 := gtime.NewFromTimeStamp(1533686888000)
|
||||
|
||||
fmt.Println(t1.String() == t2.String())
|
||||
fmt.Println(t1)
|
||||
fmt.Println(t1.String() == t2.String())
|
||||
fmt.Println(t1)
|
||||
|
||||
// Output:
|
||||
// true
|
||||
// 2018-08-08 08:08:08
|
||||
// Output:
|
||||
// true
|
||||
// 2018-08-08 08:08:08
|
||||
}
|
||||
|
||||
// Timestamp returns the timestamp in seconds.
|
||||
func ExampleTime_Timestamp() {
|
||||
t := gtime.Timestamp()
|
||||
t := gtime.Timestamp()
|
||||
|
||||
fmt.Println(t)
|
||||
fmt.Println(t)
|
||||
|
||||
// May output:
|
||||
// 1533686888
|
||||
// May output:
|
||||
// 1533686888
|
||||
}
|
||||
|
||||
// Timestamp returns the timestamp in milliseconds.
|
||||
func ExampleTime_TimestampMilli() {
|
||||
t := gtime.TimestampMilli()
|
||||
t := gtime.TimestampMilli()
|
||||
|
||||
fmt.Println(t)
|
||||
fmt.Println(t)
|
||||
|
||||
// May output:
|
||||
// 1533686888000
|
||||
// May output:
|
||||
// 1533686888000
|
||||
}
|
||||
|
||||
// Timestamp returns the timestamp in microseconds.
|
||||
func ExampleTime_TimestampMicro() {
|
||||
t := gtime.TimestampMicro()
|
||||
t := gtime.TimestampMicro()
|
||||
|
||||
fmt.Println(t)
|
||||
fmt.Println(t)
|
||||
|
||||
// May output:
|
||||
// 1533686888000000
|
||||
// May output:
|
||||
// 1533686888000000
|
||||
}
|
||||
|
||||
// Timestamp returns the timestamp in nanoseconds.
|
||||
func ExampleTime_TimestampNano() {
|
||||
t := gtime.TimestampNano()
|
||||
t := gtime.TimestampNano()
|
||||
|
||||
fmt.Println(t)
|
||||
fmt.Println(t)
|
||||
|
||||
// May output:
|
||||
// 1533686888000000
|
||||
// May output:
|
||||
// 1533686888000000
|
||||
}
|
||||
|
||||
// TimestampStr is a convenience method which retrieves and returns
|
||||
// the timestamp in seconds as string.
|
||||
func ExampleTime_TimestampStr() {
|
||||
t := gtime.TimestampStr()
|
||||
t := gtime.TimestampStr()
|
||||
|
||||
fmt.Println(reflect.TypeOf(t))
|
||||
fmt.Println(reflect.TypeOf(t))
|
||||
|
||||
// Output:
|
||||
// string
|
||||
// Output:
|
||||
// string
|
||||
}
|
||||
|
||||
// Month returns the month of the year specified by t.
|
||||
func ExampleTime_Month() {
|
||||
timeLayout := "2006-01-02 15:04:05"
|
||||
curTime := "2018-08-08 08:08:08"
|
||||
timer, _ := time.Parse(timeLayout, curTime)
|
||||
gt := gtime.New(timer)
|
||||
gt := gtime.New("2018-08-08 08:08:08")
|
||||
t1 := gt.Month()
|
||||
|
||||
t1 := gt.Month()
|
||||
fmt.Println(t1)
|
||||
|
||||
fmt.Println(t1)
|
||||
|
||||
// Output:
|
||||
// 8
|
||||
// Output:
|
||||
// 8
|
||||
}
|
||||
|
||||
// Second returns the second offset within the minute specified by t,
|
||||
// in the range [0, 59].
|
||||
func ExampleTime_Second() {
|
||||
timeLayout := "2006-01-02 15:04:05"
|
||||
curTime := "2018-08-08 08:08:08"
|
||||
timer, _ := time.Parse(timeLayout, curTime)
|
||||
gt := gtime.New(timer)
|
||||
gt := gtime.New("2018-08-08 08:08:08")
|
||||
t1 := gt.Second()
|
||||
|
||||
t1 := gt.Second()
|
||||
fmt.Println(t1)
|
||||
|
||||
fmt.Println(t1)
|
||||
|
||||
// Output:
|
||||
// 8
|
||||
// Output:
|
||||
// 8
|
||||
}
|
||||
|
||||
// String returns current time object as string.
|
||||
func ExampleTime_String() {
|
||||
timeLayout := "2006-01-02 15:04:05"
|
||||
curTime := "2018-08-08 08:08:08"
|
||||
timer, _ := time.Parse(timeLayout, curTime)
|
||||
gt := gtime.New(timer)
|
||||
gt := gtime.New("2018-08-08 08:08:08")
|
||||
t1 := gt.String()
|
||||
|
||||
t1 := gt.String()
|
||||
fmt.Println(t1)
|
||||
fmt.Println(reflect.TypeOf(t1))
|
||||
|
||||
fmt.Println(t1)
|
||||
fmt.Println(reflect.TypeOf(t1))
|
||||
|
||||
// Output:
|
||||
// 2018-08-08 08:08:08
|
||||
// string
|
||||
// Output:
|
||||
// 2018-08-08 08:08:08
|
||||
// string
|
||||
}
|
||||
|
||||
// IsZero reports whether t represents the zero time instant,
|
||||
// January 1, year 1, 00:00:00 UTC.
|
||||
func ExampleTime_IsZero() {
|
||||
timeLayout := "2006-01-02 15:04:05"
|
||||
curTime := "2018-08-08 08:08:08"
|
||||
timer, _ := time.Parse(timeLayout, curTime)
|
||||
gt := gtime.New(timer)
|
||||
gt := gtime.New("2018-08-08 08:08:08")
|
||||
|
||||
fmt.Println(gt.IsZero())
|
||||
fmt.Println(gt.IsZero())
|
||||
|
||||
// Output:
|
||||
// false
|
||||
// Output:
|
||||
// false
|
||||
}
|
||||
|
||||
// Add adds the duration to current time.
|
||||
func ExampleTime_Add() {
|
||||
timeLayout := "2006-01-02 15:04:05"
|
||||
curTime := "2018-08-08 08:08:08"
|
||||
timer, _ := time.Parse(timeLayout, curTime)
|
||||
gt := gtime.New(timer)
|
||||
gt1 := gt.Add(time.Duration(10) * time.Second)
|
||||
gt := gtime.New("2018-08-08 08:08:08")
|
||||
gt1 := gt.Add(time.Duration(10) * time.Second)
|
||||
|
||||
fmt.Println(gt1)
|
||||
fmt.Println(gt1)
|
||||
|
||||
// Output:
|
||||
// 2018-08-08 08:08:18
|
||||
// Output:
|
||||
// 2018-08-08 08:08:18
|
||||
}
|
||||
|
||||
// AddStr parses the given duration as string and adds it to current time.
|
||||
// Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
|
||||
func ExampleTime_AddStr() {
|
||||
timeLayout := "2006-01-02 15:04:05"
|
||||
curTime := "2018-08-08 08:08:08"
|
||||
timer, _ := time.Parse(timeLayout, curTime)
|
||||
gt := gtime.New(timer)
|
||||
gt1, _ := gt.AddStr("10s")
|
||||
gt := gtime.New("2018-08-08 08:08:08")
|
||||
gt1, _ := gt.AddStr("10s")
|
||||
|
||||
fmt.Println(gt1)
|
||||
fmt.Println(gt1)
|
||||
|
||||
// Output:
|
||||
// 2018-08-08 08:08:18
|
||||
// Output:
|
||||
// 2018-08-08 08:08:18
|
||||
}
|
||||
|
||||
// AddDate adds year, month and day to the time.
|
||||
func ExampleTime_AddDate() {
|
||||
timeLayout := "2006-01-02"
|
||||
curTime := "2018-08-08"
|
||||
timer, _ := time.Parse(timeLayout, curTime)
|
||||
gt := gtime.New(timer)
|
||||
gt = gt.AddDate(1, 2, 3)
|
||||
var (
|
||||
year = 1
|
||||
month = 2
|
||||
day = 3
|
||||
)
|
||||
gt := gtime.New("2018-08-08 08:08:08")
|
||||
gt = gt.AddDate(year, month, day)
|
||||
|
||||
fmt.Println(gt)
|
||||
fmt.Println(gt)
|
||||
|
||||
// Output:
|
||||
// 2019-10-11 00:00:00
|
||||
// Output:
|
||||
// 2019-10-11 08:08:08
|
||||
}
|
||||
|
||||
// Round returns the result of rounding t to the nearest multiple of d (since the zero time).
|
||||
@ -274,16 +252,13 @@ func ExampleTime_AddDate() {
|
||||
// time. Thus, Round(Hour) may return a time with a non-zero
|
||||
// minute, depending on the time's Location.
|
||||
func ExampleTime_Round() {
|
||||
timeLayout := "2006-01-02"
|
||||
curTime := "2018-08-08"
|
||||
timer, _ := time.Parse(timeLayout, curTime)
|
||||
gt := gtime.New(timer)
|
||||
t := gt.Round(time.Duration(10) * time.Second)
|
||||
gt := gtime.New("2018-08-08 08:08:08")
|
||||
t := gt.Round(time.Duration(10) * time.Second)
|
||||
|
||||
fmt.Println(t)
|
||||
fmt.Println(t)
|
||||
|
||||
// Output:
|
||||
// 2018-08-08 00:00:00
|
||||
// Output:
|
||||
// 2018-08-08 08:08:10
|
||||
}
|
||||
|
||||
// Truncate returns the result of rounding t down to a multiple of d (since the zero time).
|
||||
@ -294,16 +269,13 @@ func ExampleTime_Round() {
|
||||
// time. Thus, Truncate(Hour) may return a time with a non-zero
|
||||
// minute, depending on the time's Location.
|
||||
func ExampleTime_Truncate() {
|
||||
timeLayout := "2006-01-02 15:04:05"
|
||||
curTime := "2018-08-08 08:08:08"
|
||||
timer, _ := time.Parse(timeLayout, curTime)
|
||||
gt := gtime.New(timer)
|
||||
t := gt.Truncate(time.Duration(10) * time.Second)
|
||||
gt := gtime.New("2018-08-08 08:08:08")
|
||||
t := gt.Truncate(time.Duration(10) * time.Second)
|
||||
|
||||
fmt.Println(t)
|
||||
fmt.Println(t)
|
||||
|
||||
// Output:
|
||||
// 2018-08-08 08:08:00
|
||||
// Output:
|
||||
// 2018-08-08 08:08:00
|
||||
}
|
||||
|
||||
// Equal reports whether t and u represent the same time instant.
|
||||
@ -312,44 +284,35 @@ func ExampleTime_Truncate() {
|
||||
// See the documentation on the Time type for the pitfalls of using == with
|
||||
// Time values; most code should use Equal instead.
|
||||
func ExampleTime_Equal() {
|
||||
timeLayout := "2006-01-02 15:04:05"
|
||||
curTime := "2018-08-08 08:08:08"
|
||||
timer, _ := time.Parse(timeLayout, curTime)
|
||||
gt1 := gtime.New(timer)
|
||||
gt2 := gtime.New(timer)
|
||||
gt1 := gtime.New("2018-08-08 08:08:08")
|
||||
gt2 := gtime.New("2018-08-08 08:08:08")
|
||||
|
||||
fmt.Println(gt1.Equal(gt2))
|
||||
fmt.Println(gt1.Equal(gt2))
|
||||
|
||||
// Output:
|
||||
// true
|
||||
// Output:
|
||||
// true
|
||||
}
|
||||
|
||||
// Before reports whether the time instant t is before u.
|
||||
func ExampleTime_Before() {
|
||||
timeLayout := "2006-01-02"
|
||||
timer1, _ := time.Parse(timeLayout, "2018-08-07")
|
||||
timer2, _ := time.Parse(timeLayout, "2018-08-08")
|
||||
gt1 := gtime.New(timer1)
|
||||
gt2 := gtime.New(timer2)
|
||||
gt1 := gtime.New("2018-08-07")
|
||||
gt2 := gtime.New("2018-08-08")
|
||||
|
||||
fmt.Println(gt1.Before(gt2))
|
||||
fmt.Println(gt1.Before(gt2))
|
||||
|
||||
// Output:
|
||||
// true
|
||||
// Output:
|
||||
// true
|
||||
}
|
||||
|
||||
// After reports whether the time instant t is after u.
|
||||
func ExampleTime_After() {
|
||||
timeLayout := "2006-01-02"
|
||||
timer1, _ := time.Parse(timeLayout, "2018-08-07")
|
||||
timer2, _ := time.Parse(timeLayout, "2018-08-08")
|
||||
gt1 := gtime.New(timer1)
|
||||
gt2 := gtime.New(timer2)
|
||||
gt1 := gtime.New("2018-08-07")
|
||||
gt2 := gtime.New("2018-08-08")
|
||||
|
||||
fmt.Println(gt1.After(gt2))
|
||||
fmt.Println(gt1.After(gt2))
|
||||
|
||||
// Output:
|
||||
// false
|
||||
// Output:
|
||||
// false
|
||||
}
|
||||
|
||||
// Sub returns the duration t-u. If the result exceeds the maximum (or minimum)
|
||||
@ -357,192 +320,158 @@ func ExampleTime_After() {
|
||||
// will be returned.
|
||||
// To compute t-d for a duration d, use t.Add(-d).
|
||||
func ExampleTime_Sub() {
|
||||
timeLayout := "2006-01-02 15:04:05"
|
||||
timer1, _ := time.Parse(timeLayout, "2018-08-08 08:08:08")
|
||||
timer2, _ := time.Parse(timeLayout, "2018-08-08 08:08:10")
|
||||
gt1 := gtime.New(timer1)
|
||||
gt2 := gtime.New(timer2)
|
||||
gt1 := gtime.New("2018-08-08 08:08:08")
|
||||
gt2 := gtime.New("2018-08-08 08:08:10")
|
||||
|
||||
fmt.Println(gt2.Sub(gt1))
|
||||
fmt.Println(gt2.Sub(gt1))
|
||||
|
||||
// Output:
|
||||
// 2s
|
||||
// Output:
|
||||
// 2s
|
||||
}
|
||||
|
||||
// StartOfMinute clones and returns a new time of which the seconds is set to 0.
|
||||
func ExampleTime_StartOfMinute() {
|
||||
timeLayout := "2006-01-02 15:04:05"
|
||||
timer1, _ := time.Parse(timeLayout, "2018-08-08 08:08:08")
|
||||
gt1 := gtime.New(timer1)
|
||||
gt1 := gtime.New("2018-08-08 08:08:08")
|
||||
|
||||
fmt.Println(gt1.StartOfMinute())
|
||||
fmt.Println(gt1.StartOfMinute())
|
||||
|
||||
// Output:
|
||||
// 2018-08-08 08:08:00
|
||||
// Output:
|
||||
// 2018-08-08 08:08:00
|
||||
}
|
||||
|
||||
func ExampleTime_StartOfHour() {
|
||||
timeLayout := "2006-01-02 15:04:05"
|
||||
timer1, _ := time.Parse(timeLayout, "2018-08-08 08:08:08")
|
||||
gt1 := gtime.New(timer1)
|
||||
gt1 := gtime.New("2018-08-08 08:08:08")
|
||||
|
||||
fmt.Println(gt1.StartOfHour())
|
||||
fmt.Println(gt1.StartOfHour())
|
||||
|
||||
// Output:
|
||||
// 2018-08-08 08:00:00
|
||||
// Output:
|
||||
// 2018-08-08 08:00:00
|
||||
}
|
||||
|
||||
func ExampleTime_StartOfDay() {
|
||||
timeLayout := "2006-01-02 15:04:05"
|
||||
timer1, _ := time.Parse(timeLayout, "2018-08-08 08:08:08")
|
||||
gt1 := gtime.New(timer1)
|
||||
gt1 := gtime.New("2018-08-08 08:08:08")
|
||||
|
||||
fmt.Println(gt1.StartOfDay())
|
||||
fmt.Println(gt1.StartOfDay())
|
||||
|
||||
// Output:
|
||||
// 2018-08-08 00:00:00
|
||||
// Output:
|
||||
// 2018-08-08 00:00:00
|
||||
}
|
||||
|
||||
func ExampleTime_StartOfWeek() {
|
||||
timeLayout := "2006-01-02 15:04:05"
|
||||
timer1, _ := time.Parse(timeLayout, "2018-08-08 08:08:08")
|
||||
gt1 := gtime.New(timer1)
|
||||
gt1 := gtime.New("2018-08-08 08:08:08")
|
||||
|
||||
fmt.Println(gt1.StartOfWeek())
|
||||
fmt.Println(gt1.StartOfWeek())
|
||||
|
||||
// Output:
|
||||
// 2018-08-05 00:00:00
|
||||
// Output:
|
||||
// 2018-08-05 00:00:00
|
||||
}
|
||||
|
||||
func ExampleTime_StartOfQuarter() {
|
||||
timeLayout := "2006-01-02 15:04:05"
|
||||
timer1, _ := time.Parse(timeLayout, "2018-08-08 08:08:08")
|
||||
gt1 := gtime.New(timer1)
|
||||
gt1 := gtime.New("2018-08-08 08:08:08")
|
||||
|
||||
fmt.Println(gt1.StartOfQuarter())
|
||||
fmt.Println(gt1.StartOfQuarter())
|
||||
|
||||
// Output:
|
||||
// 2018-07-01 00:00:00
|
||||
// Output:
|
||||
// 2018-07-01 00:00:00
|
||||
}
|
||||
|
||||
func ExampleTime_StartOfHalf() {
|
||||
timeLayout := "2006-01-02 15:04:05"
|
||||
timer1, _ := time.Parse(timeLayout, "2018-08-08 08:08:08")
|
||||
gt1 := gtime.New(timer1)
|
||||
gt1 := gtime.New("2018-08-08 08:08:08")
|
||||
|
||||
fmt.Println(gt1.StartOfHalf())
|
||||
fmt.Println(gt1.StartOfHalf())
|
||||
|
||||
// Output:
|
||||
// 2018-07-01 00:00:00
|
||||
// Output:
|
||||
// 2018-07-01 00:00:00
|
||||
}
|
||||
|
||||
func ExampleTime_StartOfYear() {
|
||||
timeLayout := "2006-01-02 15:04:05"
|
||||
timer1, _ := time.Parse(timeLayout, "2018-08-08 08:08:08")
|
||||
gt1 := gtime.New(timer1)
|
||||
gt1 := gtime.New("2018-08-08 08:08:08")
|
||||
|
||||
fmt.Println(gt1.StartOfYear())
|
||||
fmt.Println(gt1.StartOfYear())
|
||||
|
||||
// Output:
|
||||
// 2018-01-01 00:00:00
|
||||
// Output:
|
||||
// 2018-01-01 00:00:00
|
||||
}
|
||||
|
||||
func ExampleTime_EndOfMinute() {
|
||||
timeLayout := "2006-01-02 15:04:05"
|
||||
timer1, _ := time.Parse(timeLayout, "2018-08-08 08:08:08")
|
||||
gt1 := gtime.New(timer1)
|
||||
gt1 := gtime.New("2018-08-08 08:08:08")
|
||||
|
||||
fmt.Println(gt1.EndOfMinute())
|
||||
fmt.Println(gt1.EndOfMinute())
|
||||
|
||||
// Output:
|
||||
// 2018-08-08 08:08:59
|
||||
// Output:
|
||||
// 2018-08-08 08:08:59
|
||||
}
|
||||
|
||||
func ExampleTime_EndOfHour() {
|
||||
timeLayout := "2006-01-02 15:04:05"
|
||||
timer1, _ := time.Parse(timeLayout, "2018-08-08 08:08:08")
|
||||
gt1 := gtime.New(timer1)
|
||||
gt1 := gtime.New("2018-08-08 08:08:08")
|
||||
|
||||
fmt.Println(gt1.EndOfHour())
|
||||
fmt.Println(gt1.EndOfHour())
|
||||
|
||||
// Output:
|
||||
// 2018-08-08 08:59:59
|
||||
// Output:
|
||||
// 2018-08-08 08:59:59
|
||||
}
|
||||
|
||||
func ExampleTime_EndOfDay() {
|
||||
timeLayout := "2006-01-02 15:04:05"
|
||||
timer1, _ := time.Parse(timeLayout, "2018-08-08 08:08:08")
|
||||
gt1 := gtime.New(timer1)
|
||||
gt1 := gtime.New("2018-08-08 08:08:08")
|
||||
|
||||
fmt.Println(gt1.EndOfDay())
|
||||
fmt.Println(gt1.EndOfDay())
|
||||
|
||||
// Output:
|
||||
// 2018-08-08 23:59:59
|
||||
// Output:
|
||||
// 2018-08-08 23:59:59
|
||||
}
|
||||
|
||||
func ExampleTime_EndOfWeek() {
|
||||
timeLayout := "2006-01-02 15:04:05"
|
||||
timer1, _ := time.Parse(timeLayout, "2018-08-08 08:08:08")
|
||||
gt1 := gtime.New(timer1)
|
||||
gt1 := gtime.New("2018-08-08 08:08:08")
|
||||
|
||||
fmt.Println(gt1.EndOfWeek())
|
||||
fmt.Println(gt1.EndOfWeek())
|
||||
|
||||
// Output:
|
||||
// 2018-08-11 23:59:59
|
||||
// Output:
|
||||
// 2018-08-11 23:59:59
|
||||
}
|
||||
|
||||
func ExampleTime_EndOfMonth() {
|
||||
timeLayout := "2006-01-02 15:04:05"
|
||||
timer1, _ := time.Parse(timeLayout, "2018-08-08 08:08:08")
|
||||
gt1 := gtime.New(timer1)
|
||||
gt1 := gtime.New("2018-08-08 08:08:08")
|
||||
|
||||
fmt.Println(gt1.EndOfMonth())
|
||||
fmt.Println(gt1.EndOfMonth())
|
||||
|
||||
// Output:
|
||||
// 2018-08-31 23:59:59
|
||||
// Output:
|
||||
// 2018-08-31 23:59:59
|
||||
}
|
||||
|
||||
func ExampleTime_EndOfQuarter() {
|
||||
timeLayout := "2006-01-02 15:04:05"
|
||||
timer1, _ := time.Parse(timeLayout, "2018-08-08 08:08:08")
|
||||
gt1 := gtime.New(timer1)
|
||||
gt1 := gtime.New("2018-08-08 08:08:08")
|
||||
|
||||
fmt.Println(gt1.EndOfQuarter())
|
||||
fmt.Println(gt1.EndOfQuarter())
|
||||
|
||||
// Output:
|
||||
// 2018-09-30 23:59:59
|
||||
// Output:
|
||||
// 2018-09-30 23:59:59
|
||||
}
|
||||
|
||||
func ExampleTime_EndOfHalf() {
|
||||
timeLayout := "2006-01-02 15:04:05"
|
||||
timer1, _ := time.Parse(timeLayout, "2018-08-08 08:08:08")
|
||||
gt1 := gtime.New(timer1)
|
||||
gt1 := gtime.New("2018-08-08 08:08:08")
|
||||
|
||||
fmt.Println(gt1.EndOfHalf())
|
||||
fmt.Println(gt1.EndOfHalf())
|
||||
|
||||
// Output:
|
||||
// 2018-12-31 23:59:59
|
||||
// Output:
|
||||
// 2018-12-31 23:59:59
|
||||
}
|
||||
|
||||
func ExampleTime_EndOfYear() {
|
||||
timeLayout := "2006-01-02 15:04:05"
|
||||
timer1, _ := time.Parse(timeLayout, "2018-08-08 08:08:08")
|
||||
gt1 := gtime.New(timer1)
|
||||
gt1 := gtime.New("2018-08-08 08:08:08")
|
||||
|
||||
fmt.Println(gt1.EndOfYear())
|
||||
fmt.Println(gt1.EndOfYear())
|
||||
|
||||
// Output:
|
||||
// 2018-12-31 23:59:59
|
||||
// Output:
|
||||
// 2018-12-31 23:59:59
|
||||
}
|
||||
|
||||
func ExampleTime_MarshalJSON() {
|
||||
timeLayout := "2006-01-02 15:04:05"
|
||||
timer1, _ := time.Parse(timeLayout, "2018-08-08 08:08:08")
|
||||
gt1 := gtime.New(timer1)
|
||||
gt1 := gtime.New("2018-08-08 08:08:08")
|
||||
|
||||
json, _ := gt1.MarshalJSON()
|
||||
fmt.Println(string(json))
|
||||
json, _ := gt1.MarshalJSON()
|
||||
fmt.Println(string(json))
|
||||
|
||||
// Output:
|
||||
// "2018-08-08 08:08:08"
|
||||
// Output:
|
||||
// "2018-08-08 08:08:08"
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user