From da659817c58976a940cf3663e6b7e56ded4c83a7 Mon Sep 17 00:00:00 2001 From: ansionfor <77931774@qq.com> Date: Thu, 11 Nov 2021 17:41:31 +0800 Subject: [PATCH 1/5] complete gtime example --- os/gtime/gtime_z_example_basic_test.go | 274 +++++++++++++ os/gtime/gtime_z_example_time_test.go | 548 +++++++++++++++++++++++++ 2 files changed, 822 insertions(+) create mode 100644 os/gtime/gtime_z_example_basic_test.go create mode 100644 os/gtime/gtime_z_example_time_test.go diff --git a/os/gtime/gtime_z_example_basic_test.go b/os/gtime/gtime_z_example_basic_test.go new file mode 100644 index 00000000000..57b50b486e6 --- /dev/null +++ b/os/gtime/gtime_z_example_basic_test.go @@ -0,0 +1,274 @@ +// Copyright GoFrame Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + +package gtime_test + +import ( + "fmt" + "time" + "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/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()) + + // May Output: + // 1636359252 +} + +func ExampleTimestampMilli() { + fmt.Println(gtime.TimestampMilli()) + + // May Output: + // 1636359252000 +} + +func ExampleTimestampMicro() { + fmt.Println(gtime.TimestampMicro()) + + // May Output: + // 1636359252000000 +} + +func ExampleTimestampNano() { + fmt.Println(gtime.TimestampNano()) + + // May Output: + // 1636359252000000000 +} + + +func ExampleTimestampStr() { + fmt.Println(gtime.TimestampStr()) + + // May Output: + // 1636359252 +} + +func ExampleDate() { + fmt.Println(gtime.Date()) + + // May Output: + // 2006-01-02 +} + +func ExampleDatetime() { + fmt.Println(gtime.Datetime()) + + // May Output: + // 2006-01-02 15:04:05 +} + +func ExampleISO8601() { + fmt.Println(gtime.ISO8601()) + + // May Output: + // 2006-01-02T15:04:05-07:00 +} + +func ExampleRFC822() { + fmt.Println(gtime.RFC822()) + + // 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) + + // 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) + + // 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) + + // Output: + // 2006-01-02 15:04:05 +} + +func ExampleStrToTimeLayout() { + res, _ := gtime.StrToTimeLayout("2018-08-08", "2006-01-02") + fmt.Println(res) + + // Output: + // 2018-08-08 00:00:00 +} + +// ParseDuration parses a duration string. +// A duration string is a possibly signed sequence of +// decimal numbers, each with optional fraction and a unit suffix, +// such as "300ms", "-1.5h", "1d" or "2h45m". +// Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h", "d". +// +// Very note that it supports unit "d" more than function time.ParseDuration. +func ExampleParseDuration() { + res, _ := gtime.ParseDuration("+10h") + fmt.Println(res) + + // 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) + + fmt.Println(gt1.Format("Y-m-d")) + + // Output: + // 2018-08-08 +} + +func ExampleTime_FormatNew() { + timeLayout := "2006-01-02 15:04:05" + timer1, _ := time.Parse(timeLayout, "2018-08-08 08:08:08") + gt1 := gtime.New(timer1) + + fmt.Println(gt1.FormatNew("Y-m-d")) + + // Output: + // 2018-08-08 00:00: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) + + fmt.Println(gt1.FormatTo("Y-m-d")) + + // 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) + + fmt.Println(gt1.Layout("2006-01-02")) + + // 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) + + fmt.Println(gt1.LayoutNew("2006-01-02")) + + // 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) + + fmt.Println(gt1.LayoutTo("2006-01-02")) + + // 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) + + fmt.Println(gt1.IsLeapYear()) + + // 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) + + fmt.Println(gt1.DayOfYear()) + + // 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) + + fmt.Println(gt1.DaysInMonth()) + + // 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) + + 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 +} + +func ExampleTime_ToZone() { + gt1 := gtime.Now() + gt2, _ := gt1.ToZone("Asia/Shanghai") + gt3, _ := gt1.ToZone("Asia/Tokyo") + + fmt.Println(gt2) + fmt.Println(gt3) + + // May Output: + // 2021-11-11 17:10:10 + // 2021-11-11 18:10:10 +} diff --git a/os/gtime/gtime_z_example_time_test.go b/os/gtime/gtime_z_example_time_test.go new file mode 100644 index 00000000000..0ee0093cfbc --- /dev/null +++ b/os/gtime/gtime_z_example_time_test.go @@ -0,0 +1,548 @@ +// Copyright GoFrame Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + +package gtime_test + +import ( + "fmt" + "time" + "reflect" + "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 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) + + 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 +} + +// Now creates and returns a time object of now. +func ExampleNow() { + t := gtime.Now() + fmt.Println(t) + + // 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) + + fmt.Println(nTime) + + // 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") + + fmt.Println(t) + + // 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) + + // 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) + + // 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) + + fmt.Println(t1.String() == t2.String()) + fmt.Println(t1) + + // Output: + // true + // 2018-08-08 08:08:08 +} + +// Timestamp returns the timestamp in seconds. +func ExampleTime_Timestamp() { + t := gtime.Timestamp() + + fmt.Println(t) + + // May output: + // 1533686888 +} + +// Timestamp returns the timestamp in milliseconds. +func ExampleTime_TimestampMilli() { + t := gtime.TimestampMilli() + + fmt.Println(t) + + // May output: + // 1533686888000 +} + +// Timestamp returns the timestamp in microseconds. +func ExampleTime_TimestampMicro() { + t := gtime.TimestampMicro() + + fmt.Println(t) + + // May output: + // 1533686888000000 +} + +// Timestamp returns the timestamp in nanoseconds. +func ExampleTime_TimestampNano() { + t := gtime.TimestampNano() + + fmt.Println(t) + + // May output: + // 1533686888000000 +} + +// TimestampStr is a convenience method which retrieves and returns +// the timestamp in seconds as string. +func ExampleTime_TimestampStr() { + t := gtime.TimestampStr() + + fmt.Println(reflect.TypeOf(t)) + + // 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) + + t1 := gt.Month() + + fmt.Println(t1) + + // 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) + + t1 := gt.Second() + + fmt.Println(t1) + + // 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) + + t1 := gt.String() + + fmt.Println(t1) + fmt.Println(reflect.TypeOf(t1)) + + // 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) + + fmt.Println(gt.IsZero()) + + // 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) + + fmt.Println(gt1) + + // 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") + + fmt.Println(gt1) + + // 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) + + fmt.Println(gt) + + // Output: + // 2019-10-11 00:00:00 +} + +// Round returns the result of rounding t to the nearest multiple of d (since the zero time). +// The rounding behavior for halfway values is to round up. +// If d <= 0, Round returns t stripped of any monotonic clock reading but otherwise unchanged. +// +// Round operates on the time as an absolute duration since the +// zero time; it does not operate on the presentation form of the +// 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) + + fmt.Println(t) + + // Output: + // 2018-08-08 00:00:00 +} + +// Truncate returns the result of rounding t down to a multiple of d (since the zero time). +// If d <= 0, Truncate returns t stripped of any monotonic clock reading but otherwise unchanged. +// +// Truncate operates on the time as an absolute duration since the +// zero time; it does not operate on the presentation form of the +// 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) + + fmt.Println(t) + + // Output: + // 2018-08-08 08:08:00 +} + +// Equal reports whether t and u represent the same time instant. +// Two times can be equal even if they are in different locations. +// For example, 6:00 +0200 CEST and 4:00 UTC are Equal. +// 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) + + fmt.Println(gt1.Equal(gt2)) + + // 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) + + fmt.Println(gt1.Before(gt2)) + + // 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) + + fmt.Println(gt1.After(gt2)) + + // Output: + // false +} + +// Sub returns the duration t-u. If the result exceeds the maximum (or minimum) +// value that can be stored in a Duration, the maximum (or minimum) duration +// 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) + + fmt.Println(gt2.Sub(gt1)) + + // 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) + + fmt.Println(gt1.StartOfMinute()) + + // 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) + + fmt.Println(gt1.StartOfHour()) + + // 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) + + fmt.Println(gt1.StartOfDay()) + + // 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) + + fmt.Println(gt1.StartOfWeek()) + + // 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) + + fmt.Println(gt1.StartOfQuarter()) + + // 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) + + fmt.Println(gt1.StartOfHalf()) + + // 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) + + fmt.Println(gt1.StartOfYear()) + + // 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) + + fmt.Println(gt1.EndOfMinute()) + + // 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) + + fmt.Println(gt1.EndOfHour()) + + // 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) + + fmt.Println(gt1.EndOfDay()) + + // 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) + + fmt.Println(gt1.EndOfWeek()) + + // 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) + + fmt.Println(gt1.EndOfMonth()) + + // 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) + + fmt.Println(gt1.EndOfQuarter()) + + // 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) + + fmt.Println(gt1.EndOfHalf()) + + // 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) + + fmt.Println(gt1.EndOfYear()) + + // 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) + + json, _ := gt1.MarshalJSON() + fmt.Println(string(json)) + + // Output: + // "2018-08-08 08:08:08" +} From 50ad3f0f267d473c92a47829d0bd79c92d938cca Mon Sep 17 00:00:00 2001 From: ansionfor <77931774@qq.com> Date: Thu, 11 Nov 2021 17:49:43 +0800 Subject: [PATCH 2/5] go fmt --- os/gtime/gtime_z_example_basic_test.go | 266 ++++++------ os/gtime/gtime_z_example_time_test.go | 554 ++++++++++++------------- 2 files changed, 409 insertions(+), 411 deletions(-) diff --git a/os/gtime/gtime_z_example_basic_test.go b/os/gtime/gtime_z_example_basic_test.go index 57b50b486e6..a50d720c132 100644 --- a/os/gtime/gtime_z_example_basic_test.go +++ b/os/gtime/gtime_z_example_basic_test.go @@ -7,119 +7,117 @@ package gtime_test import ( - "fmt" - "time" - "github.com/gogf/gf/v2/os/gtime" + "fmt" + "github.com/gogf/gf/v2/os/gtime" + "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 ExampleSetTimeZone() { - 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/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 } 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. @@ -130,145 +128,145 @@ 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) + timeLayout := "2006-01-02 15:04:05" + timer1, _ := time.Parse(timeLayout, "2018-08-08 08:08:08") + gt1 := gtime.New(timer1) - fmt.Println(gt1.Format("Y-m-d")) + fmt.Println(gt1.Format("Y-m-d")) - // Output: - // 2018-08-08 + // Output: + // 2018-08-08 } func ExampleTime_FormatNew() { - timeLayout := "2006-01-02 15:04:05" - timer1, _ := time.Parse(timeLayout, "2018-08-08 08:08:08") - gt1 := gtime.New(timer1) + timeLayout := "2006-01-02 15:04:05" + timer1, _ := time.Parse(timeLayout, "2018-08-08 08:08:08") + gt1 := gtime.New(timer1) - fmt.Println(gt1.FormatNew("Y-m-d")) + fmt.Println(gt1.FormatNew("Y-m-d")) - // Output: - // 2018-08-08 00:00:00 + // Output: + // 2018-08-08 00:00: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) + timeLayout := "2006-01-02 15:04:05" + timer1, _ := time.Parse(timeLayout, "2018-08-08 08:08:08") + gt1 := gtime.New(timer1) - 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) + timeLayout := "2006-01-02 15:04:05" + timer1, _ := time.Parse(timeLayout, "2018-08-08 08:08:08") + gt1 := gtime.New(timer1) - 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) + timeLayout := "2006-01-02 15:04:05" + timer1, _ := time.Parse(timeLayout, "2018-08-08 08:08:08") + gt1 := gtime.New(timer1) - 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) + timeLayout := "2006-01-02 15:04:05" + timer1, _ := time.Parse(timeLayout, "2018-08-08 08:08:08") + gt1 := gtime.New(timer1) - 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) + timeLayout := "2006-01-02 15:04:05" + timer1, _ := time.Parse(timeLayout, "2018-08-08 08:08:08") + gt1 := gtime.New(timer1) - 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) + timeLayout := "2006-01-02 15:04:05" + timer1, _ := time.Parse(timeLayout, "2018-01-08 08:08:08") + gt1 := gtime.New(timer1) - 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) + timeLayout := "2006-01-02 15:04:05" + timer1, _ := time.Parse(timeLayout, "2018-08-08 08:08:08") + gt1 := gtime.New(timer1) - 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) + timeLayout := "2006-01-02 15:04:05" + timer1, _ := time.Parse(timeLayout, "2018-01-08 08:08:08") + gt1 := gtime.New(timer1) - fmt.Println(gt1.WeeksOfYear()) + fmt.Println(gt1.WeeksOfYear()) - // Output: - // 2 + // 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) + 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()) + fmt.Println(gt1.String()) - // Output: - // 2018-01-08 08:08:08 + // Output: + // 2018-01-08 08:08:08 } 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 } diff --git a/os/gtime/gtime_z_example_time_test.go b/os/gtime/gtime_z_example_time_test.go index 0ee0093cfbc..ecd14b41eed 100644 --- a/os/gtime/gtime_z_example_time_test.go +++ b/os/gtime/gtime_z_example_time_test.go @@ -7,262 +7,262 @@ package gtime_test import ( - "fmt" - "time" - "reflect" - "github.com/gogf/gf/v2/os/gtime" + "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) - - 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 + 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) + + 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 } // 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) + timeLayout := "2006-01-02 15:04:05" + curTime := "2018-08-08 08:08:08" + timer, _ := time.Parse(timeLayout, curTime) + 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) + timeLayout := "2006-01-02 15:04:05" + curTime := "2018-08-08 08:08:08" + timer, _ := time.Parse(timeLayout, curTime) + gt := gtime.New(timer) - 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) + timeLayout := "2006-01-02 15:04:05" + curTime := "2018-08-08 08:08:08" + timer, _ := time.Parse(timeLayout, curTime) + gt := gtime.New(timer) - 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) + timeLayout := "2006-01-02 15:04:05" + curTime := "2018-08-08 08:08:08" + timer, _ := time.Parse(timeLayout, curTime) + gt := gtime.New(timer) - 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) + timeLayout := "2006-01-02 15:04:05" + curTime := "2018-08-08 08:08:08" + timer, _ := time.Parse(timeLayout, curTime) + gt := gtime.New(timer) - 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) + 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) - 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") + 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") - 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) + timeLayout := "2006-01-02" + curTime := "2018-08-08" + timer, _ := time.Parse(timeLayout, curTime) + gt := gtime.New(timer) + gt = gt.AddDate(1, 2, 3) - fmt.Println(gt) + fmt.Println(gt) - // Output: - // 2019-10-11 00:00:00 + // Output: + // 2019-10-11 00:00:00 } // Round returns the result of rounding t to the nearest multiple of d (since the zero time). @@ -274,16 +274,16 @@ 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) + 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) - fmt.Println(t) + fmt.Println(t) - // Output: - // 2018-08-08 00:00:00 + // Output: + // 2018-08-08 00:00:00 } // Truncate returns the result of rounding t down to a multiple of d (since the zero time). @@ -294,16 +294,16 @@ 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) + 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) - 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 +312,44 @@ 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) + 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) - 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) + 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) - 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) + 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) - 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 +357,192 @@ 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) + 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) - 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) + timeLayout := "2006-01-02 15:04:05" + timer1, _ := time.Parse(timeLayout, "2018-08-08 08:08:08") + gt1 := gtime.New(timer1) - 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) + timeLayout := "2006-01-02 15:04:05" + timer1, _ := time.Parse(timeLayout, "2018-08-08 08:08:08") + gt1 := gtime.New(timer1) - 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) + timeLayout := "2006-01-02 15:04:05" + timer1, _ := time.Parse(timeLayout, "2018-08-08 08:08:08") + gt1 := gtime.New(timer1) - 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) + timeLayout := "2006-01-02 15:04:05" + timer1, _ := time.Parse(timeLayout, "2018-08-08 08:08:08") + gt1 := gtime.New(timer1) - 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) + timeLayout := "2006-01-02 15:04:05" + timer1, _ := time.Parse(timeLayout, "2018-08-08 08:08:08") + gt1 := gtime.New(timer1) - 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) + timeLayout := "2006-01-02 15:04:05" + timer1, _ := time.Parse(timeLayout, "2018-08-08 08:08:08") + gt1 := gtime.New(timer1) - 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) + timeLayout := "2006-01-02 15:04:05" + timer1, _ := time.Parse(timeLayout, "2018-08-08 08:08:08") + gt1 := gtime.New(timer1) - 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) + timeLayout := "2006-01-02 15:04:05" + timer1, _ := time.Parse(timeLayout, "2018-08-08 08:08:08") + gt1 := gtime.New(timer1) - 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) + timeLayout := "2006-01-02 15:04:05" + timer1, _ := time.Parse(timeLayout, "2018-08-08 08:08:08") + gt1 := gtime.New(timer1) - 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) + timeLayout := "2006-01-02 15:04:05" + timer1, _ := time.Parse(timeLayout, "2018-08-08 08:08:08") + gt1 := gtime.New(timer1) - 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) + timeLayout := "2006-01-02 15:04:05" + timer1, _ := time.Parse(timeLayout, "2018-08-08 08:08:08") + gt1 := gtime.New(timer1) - 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) + timeLayout := "2006-01-02 15:04:05" + timer1, _ := time.Parse(timeLayout, "2018-08-08 08:08:08") + gt1 := gtime.New(timer1) - 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) + timeLayout := "2006-01-02 15:04:05" + timer1, _ := time.Parse(timeLayout, "2018-08-08 08:08:08") + gt1 := gtime.New(timer1) - 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) + timeLayout := "2006-01-02 15:04:05" + timer1, _ := time.Parse(timeLayout, "2018-08-08 08:08:08") + gt1 := gtime.New(timer1) - 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) + timeLayout := "2006-01-02 15:04:05" + timer1, _ := time.Parse(timeLayout, "2018-08-08 08:08:08") + gt1 := gtime.New(timer1) - 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) + timeLayout := "2006-01-02 15:04:05" + timer1, _ := time.Parse(timeLayout, "2018-08-08 08:08:08") + gt1 := gtime.New(timer1) - 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" } From 89d2a896f1741af148aece61d2761a3e95b3ca17 Mon Sep 17 00:00:00 2001 From: Anson <77931774@qq.com> Date: Fri, 12 Nov 2021 12:16:08 +0800 Subject: [PATCH 3/5] Update gtime_z_example_basic_test.go --- os/gtime/gtime_z_example_basic_test.go | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/os/gtime/gtime_z_example_basic_test.go b/os/gtime/gtime_z_example_basic_test.go index a50d720c132..555f4b8a37a 100644 --- a/os/gtime/gtime_z_example_basic_test.go +++ b/os/gtime/gtime_z_example_basic_test.go @@ -247,17 +247,6 @@ func ExampleTime_WeeksOfYear() { // 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 -} - func ExampleTime_ToZone() { gt1 := gtime.Now() gt2, _ := gt1.ToZone("Asia/Shanghai") From cfa3d817afc2404dda1d52f8ca9e5d0b2f7a9f44 Mon Sep 17 00:00:00 2001 From: ansionfor <77931774@qq.com> Date: Sat, 13 Nov 2021 23:16:18 +0800 Subject: [PATCH 4/5] update gtime example --- os/gtime/gtime_z_example_basic_test.go | 255 ++++++------- os/gtime/gtime_z_example_time_test.go | 489 +++++++++++-------------- 2 files changed, 327 insertions(+), 417 deletions(-) diff --git a/os/gtime/gtime_z_example_basic_test.go b/os/gtime/gtime_z_example_basic_test.go index a50d720c132..36efeb6cc35 100644 --- a/os/gtime/gtime_z_example_basic_test.go +++ b/os/gtime/gtime_z_example_basic_test.go @@ -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/Tokyo") - fmt.Println(gtime.Datetime()) - // May Output: - // 2018-08-08 08:08:08 - // 2018-08-08 09:08:08 + 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 } 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) - - 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) + gt1 := gtime.New("2018-01-08 08:08:08") - fmt.Println(gt1.String()) + fmt.Println(gt1.WeeksOfYear()) - // 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 } + diff --git a/os/gtime/gtime_z_example_time_test.go b/os/gtime/gtime_z_example_time_test.go index ecd14b41eed..3740e932886 100644 --- a/os/gtime/gtime_z_example_time_test.go +++ b/os/gtime/gtime_z_example_time_test.go @@ -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) - - 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 + 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) + + // 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) - - t1 := gt.Second() + gt := gtime.New("2018-08-08 08:08:08") + 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" } + From 15deeb19b0d17e58b955bce4dc8b6dcd02cfded3 Mon Sep 17 00:00:00 2001 From: ansionfor <77931774@qq.com> Date: Sat, 13 Nov 2021 23:48:47 +0800 Subject: [PATCH 5/5] go fmt --- os/gtime/gtime_z_example_basic_test.go | 233 +++++++------- os/gtime/gtime_z_example_time_test.go | 417 ++++++++++++------------- 2 files changed, 324 insertions(+), 326 deletions(-) diff --git a/os/gtime/gtime_z_example_basic_test.go b/os/gtime/gtime_z_example_basic_test.go index 36efeb6cc35..a8656363388 100644 --- a/os/gtime/gtime_z_example_basic_test.go +++ b/os/gtime/gtime_z_example_basic_test.go @@ -7,116 +7,116 @@ package gtime_test import ( - "fmt" - "github.com/gogf/gf/v2/os/gtime" + "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. @@ -127,127 +127,126 @@ 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() { - gt1 := gtime.New("2018-08-08 08:08:08") + gt1 := gtime.New("2018-08-08 08:08:08") - 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")) + 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 - // 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 + // 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() { - gt1 := gtime.New("2018-08-08 08:08:08") + gt1 := gtime.New("2018-08-08 08:08:08") - fmt.Println(gt1.FormatNew("Y-m-d")) - fmt.Println(gt1.FormatNew("Y-m-d H:i")) + fmt.Println(gt1.FormatNew("Y-m-d")) + fmt.Println(gt1.FormatNew("Y-m-d H:i")) - // Output: - // 2018-08-08 00:00:00 - // 2018-08-08 08:08:00 + // Output: + // 2018-08-08 00:00:00 + // 2018-08-08 08:08:00 } func ExampleTime_FormatTo() { - gt1 := gtime.New("2018-08-08 08:08:08") + 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() { - gt1 := gtime.New("2018-08-08 08:08:08") + 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() { - gt1 := gtime.New("2018-08-08 08:08:08") + 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() { - gt1 := gtime.New("2018-08-08 08:08:08") + 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() { - gt1 := gtime.New("2018-08-08 08:08:08") + gt1 := gtime.New("2018-08-08 08:08:08") - fmt.Println(gt1.IsLeapYear()) + fmt.Println(gt1.IsLeapYear()) - // Output: - // false + // Output: + // false } func ExampleTime_DayOfYear() { - gt1 := gtime.New("2018-01-08 08:08:08") + 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() { - gt1 := gtime.New("2018-08-08 08:08:08") + 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() { - gt1 := gtime.New("2018-01-08 08:08:08") + gt1 := gtime.New("2018-01-08 08:08:08") - fmt.Println(gt1.WeeksOfYear()) + fmt.Println(gt1.WeeksOfYear()) - // Output: - // 2 + // 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 } - diff --git a/os/gtime/gtime_z_example_time_test.go b/os/gtime/gtime_z_example_time_test.go index 3740e932886..e88f7157cbd 100644 --- a/os/gtime/gtime_z_example_time_test.go +++ b/os/gtime/gtime_z_example_time_test.go @@ -7,240 +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() { - 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) - - // 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 + 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) + + // 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() { - timer, _ := time.Parse("2006-01-02 15:04:05", "2018-08-08 08:08:08") - 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() { - gt := gtime.New("2018-08-08 08:08:08") - t1 := gt.Month() + gt := gtime.New("2018-08-08 08:08:08") + 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() { - gt := gtime.New("2018-08-08 08:08:08") - t1 := gt.Second() + gt := gtime.New("2018-08-08 08:08:08") + t1 := gt.Second() - fmt.Println(t1) + fmt.Println(t1) - // Output: - // 8 + // Output: + // 8 } // String returns current time object as string. func ExampleTime_String() { - gt := gtime.New("2018-08-08 08:08:08") - t1 := gt.String() + gt := gtime.New("2018-08-08 08:08:08") + 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() { - gt := gtime.New("2018-08-08 08:08:08") + 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() { - gt := gtime.New("2018-08-08 08:08:08") - 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() { - gt := gtime.New("2018-08-08 08:08:08") - 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() { - var ( - year = 1 - month = 2 - day = 3 - ) - gt := gtime.New("2018-08-08 08:08:08") - gt = gt.AddDate(year, month, day) + 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 08:08:08 + // Output: + // 2019-10-11 08:08:08 } // Round returns the result of rounding t to the nearest multiple of d (since the zero time). @@ -252,13 +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() { - gt := gtime.New("2018-08-08 08:08:08") - 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 08:08:10 + // Output: + // 2018-08-08 08:08:10 } // Truncate returns the result of rounding t down to a multiple of d (since the zero time). @@ -269,13 +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() { - gt := gtime.New("2018-08-08 08:08:08") - 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. @@ -284,35 +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() { - gt1 := gtime.New("2018-08-08 08:08:08") - gt2 := gtime.New("2018-08-08 08:08:08") + 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() { - gt1 := gtime.New("2018-08-07") - gt2 := gtime.New("2018-08-08") + 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() { - gt1 := gtime.New("2018-08-07") - gt2 := gtime.New("2018-08-08") + 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) @@ -320,158 +320,157 @@ func ExampleTime_After() { // will be returned. // To compute t-d for a duration d, use t.Add(-d). func ExampleTime_Sub() { - gt1 := gtime.New("2018-08-08 08:08:08") - gt2 := gtime.New("2018-08-08 08:08:10") + 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() { - gt1 := gtime.New("2018-08-08 08:08:08") + 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() { - gt1 := gtime.New("2018-08-08 08:08:08") + 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() { - gt1 := gtime.New("2018-08-08 08:08:08") + 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() { - gt1 := gtime.New("2018-08-08 08:08:08") + 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() { - gt1 := gtime.New("2018-08-08 08:08:08") + 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() { - gt1 := gtime.New("2018-08-08 08:08:08") + 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() { - gt1 := gtime.New("2018-08-08 08:08:08") + 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() { - gt1 := gtime.New("2018-08-08 08:08:08") + 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() { - gt1 := gtime.New("2018-08-08 08:08:08") + 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() { - gt1 := gtime.New("2018-08-08 08:08:08") + 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() { - gt1 := gtime.New("2018-08-08 08:08:08") + 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() { - gt1 := gtime.New("2018-08-08 08:08:08") + 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() { - gt1 := gtime.New("2018-08-08 08:08:08") + 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() { - gt1 := gtime.New("2018-08-08 08:08:08") + 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() { - gt1 := gtime.New("2018-08-08 08:08:08") + 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() { - gt1 := gtime.New("2018-08-08 08:08:08") + 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" } -