gf/os/gtime/gtime_z_example_basic_test.go

254 lines
4.8 KiB
Go
Raw Normal View History

2021-11-11 17:41:31 +08:00
// 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 (
2021-11-13 23:48:47 +08:00
"fmt"
2021-11-14 15:37:24 +08:00
2021-11-13 23:48:47 +08:00
"github.com/gogf/gf/v2/os/gtime"
2021-11-11 17:41:31 +08:00
)
// 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() {
2021-11-13 23:48:47 +08:00
gtime.SetTimeZone("Asia/Shanghai")
fmt.Println(gtime.Datetime())
2021-11-13 23:16:18 +08:00
2021-11-13 23:48:47 +08:00
gtime.SetTimeZone("Asia/Tokyo")
fmt.Println(gtime.Datetime())
// May Output:
// 2018-08-08 08:08:08
// 2018-08-08 09:08:08
2021-11-11 17:41:31 +08:00
}
func ExampleTimestamp() {
2021-11-13 23:48:47 +08:00
fmt.Println(gtime.Timestamp())
2021-11-11 17:41:31 +08:00
2021-11-13 23:48:47 +08:00
// May Output:
// 1636359252
2021-11-11 17:41:31 +08:00
}
func ExampleTimestampMilli() {
2021-11-13 23:48:47 +08:00
fmt.Println(gtime.TimestampMilli())
2021-11-11 17:41:31 +08:00
2021-11-13 23:48:47 +08:00
// May Output:
// 1636359252000
2021-11-11 17:41:31 +08:00
}
func ExampleTimestampMicro() {
2021-11-13 23:48:47 +08:00
fmt.Println(gtime.TimestampMicro())
2021-11-11 17:41:31 +08:00
2021-11-13 23:48:47 +08:00
// May Output:
// 1636359252000000
2021-11-11 17:41:31 +08:00
}
func ExampleTimestampNano() {
2021-11-13 23:48:47 +08:00
fmt.Println(gtime.TimestampNano())
2021-11-11 17:41:31 +08:00
2021-11-13 23:48:47 +08:00
// May Output:
// 1636359252000000000
2021-11-11 17:41:31 +08:00
}
func ExampleTimestampStr() {
2021-11-13 23:48:47 +08:00
fmt.Println(gtime.TimestampStr())
2021-11-11 17:41:31 +08:00
2021-11-13 23:48:47 +08:00
// May Output:
// 1636359252
2021-11-11 17:41:31 +08:00
}
func ExampleDate() {
2021-11-13 23:48:47 +08:00
fmt.Println(gtime.Date())
2021-11-11 17:41:31 +08:00
2021-11-13 23:48:47 +08:00
// May Output:
// 2006-01-02
2021-11-11 17:41:31 +08:00
}
func ExampleDatetime() {
2021-11-13 23:48:47 +08:00
fmt.Println(gtime.Datetime())
2021-11-11 17:41:31 +08:00
2021-11-13 23:48:47 +08:00
// May Output:
// 2006-01-02 15:04:05
2021-11-11 17:41:31 +08:00
}
func ExampleISO8601() {
2021-11-13 23:48:47 +08:00
fmt.Println(gtime.ISO8601())
2021-11-11 17:41:31 +08:00
2021-11-13 23:48:47 +08:00
// May Output:
// 2006-01-02T15:04:05-07:00
2021-11-11 17:41:31 +08:00
}
func ExampleRFC822() {
2021-11-13 23:48:47 +08:00
fmt.Println(gtime.RFC822())
2021-11-11 17:41:31 +08:00
2021-11-13 23:48:47 +08:00
// May Output:
// Mon, 02 Jan 06 15:04 MST
2021-11-11 17:41:31 +08:00
}
func ExampleStrToTime() {
2021-11-13 23:48:47 +08:00
res, _ := gtime.StrToTime("2006-01-02T15:04:05-07:00", "Y-m-d H:i:s")
fmt.Println(res)
2021-11-11 17:41:31 +08:00
2021-11-13 23:48:47 +08:00
// May Output:
// 2006-01-02 15:04:05
2021-11-11 17:41:31 +08:00
}
func ExampleConvertZone() {
2021-11-13 23:48:47 +08:00
res, _ := gtime.ConvertZone("2006-01-02 15:04:05", "Asia/Tokyo", "Asia/Shanghai")
fmt.Println(res)
2021-11-11 17:41:31 +08:00
2021-11-13 23:48:47 +08:00
// Output:
// 2006-01-02 16:04:05
2021-11-11 17:41:31 +08:00
}
func ExampleStrToTimeFormat() {
2021-11-13 23:48:47 +08:00
res, _ := gtime.StrToTimeFormat("2006-01-02 15:04:05", "Y-m-d H:i:s")
fmt.Println(res)
2021-11-11 17:41:31 +08:00
2021-11-13 23:48:47 +08:00
// Output:
// 2006-01-02 15:04:05
2021-11-11 17:41:31 +08:00
}
func ExampleStrToTimeLayout() {
2021-11-13 23:48:47 +08:00
res, _ := gtime.StrToTimeLayout("2018-08-08", "2006-01-02")
fmt.Println(res)
2021-11-11 17:41:31 +08:00
2021-11-13 23:48:47 +08:00
// Output:
// 2018-08-08 00:00:00
2021-11-11 17:41:31 +08: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() {
2021-11-13 23:48:47 +08:00
res, _ := gtime.ParseDuration("+10h")
fmt.Println(res)
2021-11-11 17:41:31 +08:00
2021-11-13 23:48:47 +08:00
// Output:
// 10h0m0s
2021-11-11 17:41:31 +08:00
}
func ExampleTime_Format() {
2021-11-13 23:48:47 +08:00
gt1 := gtime.New("2018-08-08 08:08:08")
2021-11-11 17:41:31 +08:00
2021-11-13 23:48:47 +08:00
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"))
2021-11-11 17:41:31 +08:00
2021-11-13 23:48:47 +08:00
// 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
2021-11-11 17:41:31 +08:00
}
func ExampleTime_FormatNew() {
2021-11-13 23:48:47 +08:00
gt1 := gtime.New("2018-08-08 08:08:08")
2021-11-11 17:41:31 +08:00
2021-11-13 23:48:47 +08:00
fmt.Println(gt1.FormatNew("Y-m-d"))
fmt.Println(gt1.FormatNew("Y-m-d H:i"))
2021-11-11 17:41:31 +08:00
2021-11-13 23:48:47 +08:00
// Output:
// 2018-08-08 00:00:00
// 2018-08-08 08:08:00
2021-11-11 17:41:31 +08:00
}
func ExampleTime_FormatTo() {
2021-11-13 23:48:47 +08:00
gt1 := gtime.New("2018-08-08 08:08:08")
2021-11-11 17:41:31 +08:00
2021-11-13 23:48:47 +08:00
fmt.Println(gt1.FormatTo("Y-m-d"))
2021-11-11 17:41:31 +08:00
2021-11-13 23:48:47 +08:00
// Output:
// 2018-08-08 00:00:00
2021-11-11 17:41:31 +08:00
}
func ExampleTime_Layout() {
2021-11-13 23:48:47 +08:00
gt1 := gtime.New("2018-08-08 08:08:08")
2021-11-11 17:41:31 +08:00
2021-11-13 23:48:47 +08:00
fmt.Println(gt1.Layout("2006-01-02"))
2021-11-11 17:41:31 +08:00
2021-11-13 23:48:47 +08:00
// Output:
// 2018-08-08
2021-11-11 17:41:31 +08:00
}
func ExampleTime_LayoutNew() {
2021-11-13 23:48:47 +08:00
gt1 := gtime.New("2018-08-08 08:08:08")
2021-11-11 17:41:31 +08:00
2021-11-13 23:48:47 +08:00
fmt.Println(gt1.LayoutNew("2006-01-02"))
2021-11-11 17:41:31 +08:00
2021-11-13 23:48:47 +08:00
// Output:
// 2018-08-08 00:00:00
2021-11-11 17:41:31 +08:00
}
func ExampleTime_LayoutTo() {
2021-11-13 23:48:47 +08:00
gt1 := gtime.New("2018-08-08 08:08:08")
2021-11-11 17:41:31 +08:00
2021-11-13 23:48:47 +08:00
fmt.Println(gt1.LayoutTo("2006-01-02"))
2021-11-11 17:41:31 +08:00
2021-11-13 23:48:47 +08:00
// Output:
// 2018-08-08 00:00:00
2021-11-11 17:41:31 +08:00
}
func ExampleTime_IsLeapYear() {
2021-11-13 23:48:47 +08:00
gt1 := gtime.New("2018-08-08 08:08:08")
2021-11-11 17:41:31 +08:00
2021-11-13 23:48:47 +08:00
fmt.Println(gt1.IsLeapYear())
2021-11-11 17:41:31 +08:00
2021-11-13 23:48:47 +08:00
// Output:
// false
2021-11-11 17:41:31 +08:00
}
func ExampleTime_DayOfYear() {
2021-11-13 23:48:47 +08:00
gt1 := gtime.New("2018-01-08 08:08:08")
2021-11-11 17:41:31 +08:00
2021-11-13 23:48:47 +08:00
fmt.Println(gt1.DayOfYear())
2021-11-11 17:41:31 +08:00
2021-11-13 23:48:47 +08:00
// Output:
// 7
2021-11-11 17:41:31 +08:00
}
// DaysInMonth returns the day count of current month.
func ExampleTime_DaysInMonth() {
2021-11-13 23:48:47 +08:00
gt1 := gtime.New("2018-08-08 08:08:08")
2021-11-11 17:41:31 +08:00
2021-11-13 23:48:47 +08:00
fmt.Println(gt1.DaysInMonth())
2021-11-11 17:41:31 +08:00
2021-11-13 23:48:47 +08:00
// Output:
// 31
2021-11-11 17:41:31 +08:00
}
// WeeksOfYear returns the point of current week for the year.
func ExampleTime_WeeksOfYear() {
2021-11-13 23:48:47 +08:00
gt1 := gtime.New("2018-01-08 08:08:08")
2021-11-11 17:41:31 +08:00
2021-11-13 23:48:47 +08:00
fmt.Println(gt1.WeeksOfYear())
2021-11-11 17:41:31 +08:00
2021-11-13 23:48:47 +08:00
// Output:
// 2
2021-11-11 17:41:31 +08:00
}
func ExampleTime_ToZone() {
2021-11-13 23:48:47 +08:00
gt1 := gtime.Now()
gt2, _ := gt1.ToZone("Asia/Shanghai")
gt3, _ := gt1.ToZone("Asia/Tokyo")
2021-11-11 17:41:31 +08:00
2021-11-13 23:48:47 +08:00
fmt.Println(gt2)
fmt.Println(gt3)
2021-11-11 17:41:31 +08:00
2021-11-13 23:48:47 +08:00
// May Output:
// 2021-11-11 17:10:10
// 2021-11-11 18:10:10
2021-11-11 17:41:31 +08:00
}