Documentation ¶
Overview ¶
Package gtime provides functionality for measuring and displaying time.
This package should keep much less dependencies with other packages.
Index ¶
- Constants
- func Date() string
- func Datetime() string
- func FuncCost(f func()) time.Duration
- func ISO8601() string
- func ParseDuration(s string) (duration time.Duration, err error)
- func RFC822() string
- func SetTimeZone(zone string) (err error)
- func Timestamp() int64
- func TimestampMicro() int64
- func TimestampMicroStr() string
- func TimestampMilli() int64
- func TimestampMilliStr() string
- func TimestampNano() int64
- func TimestampNanoStr() string
- func TimestampStr() string
- type Time
- func ConvertZone(strTime string, toZone string, fromZone ...string) (*Time, error)
- func New(param ...interface{}) *Time
- func NewFromStr(str string) *Time
- func NewFromStrFormat(str string, format string) *Time
- func NewFromStrLayout(str string, layout string) *Time
- func NewFromTime(t time.Time) *Time
- func NewFromTimeStamp(timestamp int64) *Time
- func Now() *Time
- func ParseTimeFromContent(content string, format ...string) *Time
- func StrToTime(str string, format ...string) (*Time, error)
- func StrToTimeFormat(str string, format string) (*Time, error)
- func StrToTimeLayout(str string, layout string) (*Time, error)
- func (t *Time) Add(d time.Duration) *Time
- func (t *Time) AddDate(years int, months int, days int) *Time
- func (t *Time) AddStr(duration string) (*Time, error)
- func (t *Time) After(u *Time) bool
- func (t *Time) Before(u *Time) bool
- func (t *Time) Clone() *Time
- func (t *Time) DayOfYear() int
- func (t *Time) DaysInMonth() int
- func (t *Time) DeepCopy() interface{}
- func (t *Time) EndOfDay(withNanoPrecision ...bool) *Time
- func (t *Time) EndOfHalf(withNanoPrecision ...bool) *Time
- func (t *Time) EndOfHour(withNanoPrecision ...bool) *Time
- func (t *Time) EndOfMinute(withNanoPrecision ...bool) *Time
- func (t *Time) EndOfMonth(withNanoPrecision ...bool) *Time
- func (t *Time) EndOfQuarter(withNanoPrecision ...bool) *Time
- func (t *Time) EndOfWeek(withNanoPrecision ...bool) *Time
- func (t *Time) EndOfYear(withNanoPrecision ...bool) *Time
- func (t *Time) Equal(u *Time) bool
- func (t *Time) Format(format string) string
- func (t *Time) FormatNew(format string) *Time
- func (t *Time) FormatTo(format string) *Time
- func (t *Time) ISO8601() string
- func (t *Time) IsLeapYear() bool
- func (t *Time) IsZero() bool
- func (t *Time) Layout(layout string) string
- func (t *Time) LayoutNew(layout string) *Time
- func (t *Time) LayoutTo(layout string) *Time
- func (t *Time) Local() *Time
- func (t Time) MarshalJSON() ([]byte, error)
- func (t *Time) Microsecond() int
- func (t *Time) Millisecond() int
- func (t *Time) Month() int
- func (t *Time) Nanosecond() int
- func (t *Time) NoValidation()
- func (t *Time) RFC822() string
- func (t *Time) Round(d time.Duration) *Time
- func (t *Time) Scan(value interface{}) error
- func (t *Time) Second() int
- func (t *Time) StartOfDay() *Time
- func (t *Time) StartOfHalf() *Time
- func (t *Time) StartOfHour() *Time
- func (t *Time) StartOfMinute() *Time
- func (t *Time) StartOfMonth() *Time
- func (t *Time) StartOfQuarter() *Time
- func (t *Time) StartOfWeek() *Time
- func (t *Time) StartOfYear() *Time
- func (t *Time) String() string
- func (t *Time) Sub(u *Time) time.Duration
- func (t *Time) Timestamp() int64
- func (t *Time) TimestampMicro() int64
- func (t *Time) TimestampMicroStr() string
- func (t *Time) TimestampMilli() int64
- func (t *Time) TimestampMilliStr() string
- func (t *Time) TimestampNano() int64
- func (t *Time) TimestampNanoStr() string
- func (t *Time) TimestampStr() string
- func (t *Time) ToLocation(location *time.Location) *Time
- func (t *Time) ToZone(zone string) (*Time, error)
- func (t *Time) Truncate(d time.Duration) *Time
- func (t *Time) UTC() *Time
- func (t *Time) UnmarshalJSON(b []byte) error
- func (t *Time) UnmarshalText(data []byte) error
- func (t *Time) Value() (driver.Value, error)
- func (t *Time) WeeksOfYear() int
Examples ¶
- ConvertZone
- Date
- Datetime
- ISO8601
- NewFromStr
- NewFromStrFormat
- NewFromStrLayout
- NewFromTime
- NewFromTimeStamp
- Now
- ParseDuration
- RFC822
- SetTimeZone
- StrToTime
- StrToTimeFormat
- StrToTimeLayout
- Time.Add
- Time.AddDate
- Time.AddStr
- Time.After
- Time.Before
- Time.DayOfYear
- Time.DaysInMonth
- Time.EndOfDay
- Time.EndOfHalf
- Time.EndOfHour
- Time.EndOfMinute
- Time.EndOfMonth
- Time.EndOfQuarter
- Time.EndOfWeek
- Time.EndOfYear
- Time.Equal
- Time.Format
- Time.FormatNew
- Time.FormatTo
- Time.IsLeapYear
- Time.IsZero
- Time.Layout
- Time.LayoutNew
- Time.LayoutTo
- Time.MarshalJSON
- Time.Month
- Time.Round
- Time.Second
- Time.StartOfDay
- Time.StartOfHalf
- Time.StartOfHour
- Time.StartOfMinute
- Time.StartOfQuarter
- Time.StartOfWeek
- Time.StartOfYear
- Time.String
- Time.Sub
- Time.Timestamp
- Time.TimestampMicro
- Time.TimestampMilli
- Time.TimestampNano
- Time.TimestampStr
- Time.ToZone
- Time.Truncate
- Time.UnmarshalJSON
- Time.WeeksOfYear
- Timestamp
- TimestampMicro
- TimestampMilli
- TimestampNano
- TimestampStr
Constants ¶
const ( D = 24 * time.Hour H = time.Hour M = time.Minute S = time.Second MS = time.Millisecond US = time.Microsecond NS = time.Nanosecond )
Variables ¶
This section is empty.
Functions ¶
func Date ¶
func Date() string
Date returns current date in string like "2006-01-02".
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/os/gtime" ) func main() { fmt.Println(gtime.Date()) // May Output: // 2006-01-02 }
Output:
func Datetime ¶
func Datetime() string
Datetime returns current datetime in string like "2006-01-02 15:04:05".
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/os/gtime" ) func main() { fmt.Println(gtime.Datetime()) // May Output: // 2006-01-02 15:04:05 }
Output:
func ISO8601 ¶
func ISO8601() string
ISO8601 returns current datetime in ISO8601 format like "2006-01-02T15:04:05-07:00".
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/os/gtime" ) func main() { fmt.Println(gtime.ISO8601()) // May Output: // 2006-01-02T15:04:05-07:00 }
Output:
func ParseDuration ¶
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.
Example ¶
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.
package main import ( "fmt" "github.com/gogf/gf/v2/os/gtime" ) func main() { res, _ := gtime.ParseDuration("+10h") fmt.Println(res) }
Output: 10h0m0s
func RFC822 ¶
func RFC822() string
RFC822 returns current datetime in RFC822 format like "Mon, 02 Jan 06 15:04 MST".
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/os/gtime" ) func main() { fmt.Println(gtime.RFC822()) // May Output: // Mon, 02 Jan 06 15:04 MST }
Output:
func SetTimeZone ¶
SetTimeZone sets the time zone for current whole process. The parameter `zone` is an area string specifying corresponding time zone, eg: Asia/Shanghai.
PLEASE VERY NOTE THAT: 1. This should be called before package "time" import. 2. This function should be called once. 3. Please refer to issue: https://github.com/golang/go/issues/34814
Example ¶
New creates and returns a Time object with given parameter. The optional parameter can be type of: time.Time/*time.Time, string or integer.
package main import ( "fmt" "github.com/gogf/gf/v2/os/gtime" ) func main() { gtime.SetTimeZone("Asia/Shanghai") fmt.Println(gtime.Datetime()) // May Output: // 2018-08-08 08:08:08 }
Output:
func Timestamp ¶
func Timestamp() int64
Timestamp retrieves and returns the timestamp in seconds.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/os/gtime" ) func main() { fmt.Println(gtime.Timestamp()) // May Output: // 1636359252 }
Output:
func TimestampMicro ¶
func TimestampMicro() int64
TimestampMicro retrieves and returns the timestamp in microseconds.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/os/gtime" ) func main() { fmt.Println(gtime.TimestampMicro()) // May Output: // 1636359252000000 }
Output:
func TimestampMicroStr ¶
func TimestampMicroStr() string
TimestampMicroStr is a convenience method which retrieves and returns the timestamp in microseconds as string.
func TimestampMilli ¶
func TimestampMilli() int64
TimestampMilli retrieves and returns the timestamp in milliseconds.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/os/gtime" ) func main() { fmt.Println(gtime.TimestampMilli()) // May Output: // 1636359252000 }
Output:
func TimestampMilliStr ¶
func TimestampMilliStr() string
TimestampMilliStr is a convenience method which retrieves and returns the timestamp in milliseconds as string.
func TimestampNano ¶
func TimestampNano() int64
TimestampNano retrieves and returns the timestamp in nanoseconds.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/os/gtime" ) func main() { fmt.Println(gtime.TimestampNano()) // May Output: // 1636359252000000000 }
Output:
func TimestampNanoStr ¶
func TimestampNanoStr() string
TimestampNanoStr is a convenience method which retrieves and returns the timestamp in nanoseconds as string.
func TimestampStr ¶
func TimestampStr() string
TimestampStr is a convenience method which retrieves and returns the timestamp in seconds as string.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/os/gtime" ) func main() { fmt.Println(gtime.TimestampStr()) // May Output: // 1636359252 }
Output:
Types ¶
type Time ¶
type Time struct {
// contains filtered or unexported fields
}
Time is a wrapper for time.Time for additional features.
func ConvertZone ¶
ConvertZone converts time in string `strTime` from `fromZone` to `toZone`. The parameter `fromZone` is unnecessary, it is current time zone in default.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/os/gtime" ) func main() { res, _ := gtime.ConvertZone("2006-01-02 15:04:05", "Asia/Tokyo", "Asia/Shanghai") fmt.Println(res) }
Output: 2006-01-02 16:04:05
func New ¶
func New(param ...interface{}) *Time
New creates and returns a Time object with given parameter. The optional parameter is the time object which can be type of: time.Time/*time.Time, string or integer. Example: New("2024-10-29") New(1390876568) New(t) // The t is type of time.Time.
func NewFromStr ¶
NewFromStr creates and returns a Time object with given string. Note that it returns nil if there's error occurs.
Example ¶
NewFromStr creates and returns a Time object with given string. Note that it returns nil if there's error occurs.
package main import ( "fmt" "github.com/gogf/gf/v2/os/gtime" ) func main() { t := gtime.NewFromStr("2018-08-08 08:08:08") fmt.Println(t) }
Output: 2018-08-08 08:08:08
func NewFromStrFormat ¶
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.
Example ¶
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.
package main import ( "fmt" "github.com/gogf/gf/v2/os/gtime" ) func main() { 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
func NewFromStrLayout ¶
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.
Example ¶
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.
package main import ( "fmt" "github.com/gogf/gf/v2/os/gtime" ) func main() { 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
func NewFromTime ¶
NewFromTime creates and returns a Time object with given time.Time object.
Example ¶
NewFromTime creates and returns a Time object with given time.Time object.
package main import ( "fmt" "time" "github.com/gogf/gf/v2/os/gtime" ) func main() { timer, _ := time.Parse("2006-01-02 15:04:05", "2018-08-08 08:08:08") nTime := gtime.NewFromTime(timer) fmt.Println(nTime) }
Output: 2018-08-08 08:08:08
func NewFromTimeStamp ¶
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.
Example ¶
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.
package main import ( "fmt" "github.com/gogf/gf/v2/os/gtime" ) func main() { 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
func Now ¶
func Now() *Time
Now creates and returns a time object of now.
Example ¶
Now creates and returns a time object of now.
package main import ( "fmt" "github.com/gogf/gf/v2/os/gtime" ) func main() { t := gtime.Now() fmt.Println(t) // May Output: // 2021-11-06 13:41:08 }
Output:
func ParseTimeFromContent ¶
ParseTimeFromContent retrieves time information for content string, it then parses and returns it as *Time object. It returns the first time information if there are more than one time string in the content. It only retrieves and parses the time information with given first matched `format` if it's passed.
func StrToTime ¶
StrToTime converts string to *Time object. It also supports timestamp string. The parameter `format` is unnecessary, which specifies the format for converting like "Y-m-d H:i:s". If `format` is given, it acts as same as function StrToTimeFormat. If `format` is not given, it converts string as a "standard" datetime string. Note that, it fails and returns error if there's no date string in `str`.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/os/gtime" ) func main() { 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 }
Output:
func StrToTimeFormat ¶
StrToTimeFormat parses string `str` to *Time object with given format `format`. The parameter `format` is like "Y-m-d H:i:s".
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/os/gtime" ) func main() { 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 StrToTimeLayout ¶
StrToTimeLayout parses string `str` to *Time object with given format `layout`. The parameter `layout` is in stdlib format like "2006-01-02 15:04:05".
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/os/gtime" ) func main() { res, _ := gtime.StrToTimeLayout("2018-08-08", "2006-01-02") fmt.Println(res) }
Output: 2018-08-08 00:00:00
func (*Time) Add ¶
Add adds the duration to current time.
Example ¶
Add adds the duration to current time.
package main import ( "fmt" "time" "github.com/gogf/gf/v2/os/gtime" ) func main() { gt := gtime.New("2018-08-08 08:08:08") gt1 := gt.Add(time.Duration(10) * time.Second) fmt.Println(gt1) }
Output: 2018-08-08 08:08:18
func (*Time) AddDate ¶
AddDate adds year, month and day to the time.
Example ¶
AddDate adds year, month and day to the time.
package main import ( "fmt" "github.com/gogf/gf/v2/os/gtime" ) func main() { 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) }
Output: 2019-10-11 08:08:08
func (*Time) AddStr ¶
AddStr parses the given duration as string and adds it to current time.
Example ¶
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".
package main import ( "fmt" "github.com/gogf/gf/v2/os/gtime" ) func main() { gt := gtime.New("2018-08-08 08:08:08") gt1, _ := gt.AddStr("10s") fmt.Println(gt1) }
Output: 2018-08-08 08:08:18
func (*Time) After ¶
After reports whether the time instant t is after u.
Example ¶
After reports whether the time instant t is after u.
package main import ( "fmt" "github.com/gogf/gf/v2/os/gtime" ) func main() { gt1 := gtime.New("2018-08-07") gt2 := gtime.New("2018-08-08") fmt.Println(gt1.After(gt2)) }
Output: false
func (*Time) Before ¶
Before reports whether the time instant t is before u.
Example ¶
Before reports whether the time instant t is before u.
package main import ( "fmt" "github.com/gogf/gf/v2/os/gtime" ) func main() { gt1 := gtime.New("2018-08-07") gt2 := gtime.New("2018-08-08") fmt.Println(gt1.Before(gt2)) }
Output: true
func (*Time) DayOfYear ¶
DayOfYear checks and returns the position of the day for the year.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/os/gtime" ) func main() { gt1 := gtime.New("2018-01-08 08:08:08") fmt.Println(gt1.DayOfYear()) }
Output: 7
func (*Time) DaysInMonth ¶
DaysInMonth returns the day count of current month.
Example ¶
DaysInMonth returns the day count of current month.
package main import ( "fmt" "github.com/gogf/gf/v2/os/gtime" ) func main() { gt1 := gtime.New("2018-08-08 08:08:08") fmt.Println(gt1.DaysInMonth()) }
Output: 31
func (*Time) DeepCopy ¶
func (t *Time) DeepCopy() interface{}
DeepCopy implements interface for deep copy of current type.
func (*Time) EndOfDay ¶
EndOfDay clones and returns a new time which is the end of day the and its time is set to 23:59:59.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/os/gtime" ) func main() { gt1 := gtime.New("2018-08-08 08:08:08") fmt.Println(gt1.EndOfDay()) }
Output: 2018-08-08 23:59:59
func (*Time) EndOfHalf ¶
EndOfHalf clones and returns a new time which is the end of the half year and its time is set to 23:59:59.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/os/gtime" ) func main() { gt1 := gtime.New("2018-08-08 08:08:08") fmt.Println(gt1.EndOfHalf()) }
Output: 2018-12-31 23:59:59
func (*Time) EndOfHour ¶
EndOfHour clones and returns a new time of which the minutes and seconds are both set to 59.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/os/gtime" ) func main() { gt1 := gtime.New("2018-08-08 08:08:08") fmt.Println(gt1.EndOfHour()) }
Output: 2018-08-08 08:59:59
func (*Time) EndOfMinute ¶
EndOfMinute clones and returns a new time of which the seconds is set to 59.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/os/gtime" ) func main() { gt1 := gtime.New("2018-08-08 08:08:08") fmt.Println(gt1.EndOfMinute()) }
Output: 2018-08-08 08:08:59
func (*Time) EndOfMonth ¶
EndOfMonth clones and returns a new time which is the end of the month and its time is set to 23:59:59.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/os/gtime" ) func main() { gt1 := gtime.New("2018-08-08 08:08:08") fmt.Println(gt1.EndOfMonth()) }
Output: 2018-08-31 23:59:59
func (*Time) EndOfQuarter ¶
EndOfQuarter clones and returns a new time which is end of the quarter and its time is set to 23:59:59.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/os/gtime" ) func main() { gt1 := gtime.New("2018-08-08 08:08:08") fmt.Println(gt1.EndOfQuarter()) }
Output: 2018-09-30 23:59:59
func (*Time) EndOfWeek ¶
EndOfWeek clones and returns a new time which is the end of week and its time is set to 23:59:59.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/os/gtime" ) func main() { gt1 := gtime.New("2018-08-08 08:08:08") fmt.Println(gt1.EndOfWeek()) }
Output: 2018-08-11 23:59:59
func (*Time) EndOfYear ¶
EndOfYear clones and returns a new time which is the end of the year and its time is set to 23:59:59.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/os/gtime" ) func main() { gt1 := gtime.New("2018-08-08 08:08:08") fmt.Println(gt1.EndOfYear()) }
Output: 2018-12-31 23:59:59
func (*Time) Equal ¶
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.
Example ¶
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.
package main import ( "fmt" "github.com/gogf/gf/v2/os/gtime" ) func main() { gt1 := gtime.New("2018-08-08 08:08:08") gt2 := gtime.New("2018-08-08 08:08:08") fmt.Println(gt1.Equal(gt2)) }
Output: true
func (*Time) Format ¶
Format formats and returns the formatted result with custom `format`. Refer method Layout, if you want to follow stdlib layout.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/os/gtime" ) func main() { 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")) }
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 (*Time) FormatNew ¶
FormatNew formats and returns a new Time object with given custom `format`.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/os/gtime" ) func main() { 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")) }
Output: 2018-08-08 00:00:00 2018-08-08 08:08:00
func (*Time) FormatTo ¶
FormatTo formats `t` with given custom `format`.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/os/gtime" ) func main() { gt1 := gtime.New("2018-08-08 08:08:08") fmt.Println(gt1.FormatTo("Y-m-d")) }
Output: 2018-08-08 00:00:00
func (*Time) IsLeapYear ¶
IsLeapYear checks whether the time is leap year.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/os/gtime" ) func main() { gt1 := gtime.New("2018-08-08 08:08:08") fmt.Println(gt1.IsLeapYear()) }
Output: false
func (*Time) IsZero ¶
IsZero reports whether t represents the zero time instant, January 1, year 1, 00:00:00 UTC.
Example ¶
IsZero reports whether t represents the zero time instant, January 1, year 1, 00:00:00 UTC.
package main import ( "fmt" "github.com/gogf/gf/v2/os/gtime" ) func main() { gt := gtime.New("2018-08-08 08:08:08") fmt.Println(gt.IsZero()) }
Output: false
func (*Time) Layout ¶
Layout formats the time with stdlib layout and returns the formatted result.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/os/gtime" ) func main() { gt1 := gtime.New("2018-08-08 08:08:08") fmt.Println(gt1.Layout("2006-01-02")) }
Output: 2018-08-08
func (*Time) LayoutNew ¶
LayoutNew formats the time with stdlib layout and returns the new Time object.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/os/gtime" ) func main() { gt1 := gtime.New("2018-08-08 08:08:08") fmt.Println(gt1.LayoutNew("2006-01-02")) }
Output: 2018-08-08 00:00:00
func (*Time) LayoutTo ¶
LayoutTo formats `t` with stdlib layout.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/os/gtime" ) func main() { gt1 := gtime.New("2018-08-08 08:08:08") fmt.Println(gt1.LayoutTo("2006-01-02")) }
Output: 2018-08-08 00:00:00
func (Time) MarshalJSON ¶
MarshalJSON implements the interface MarshalJSON for json.Marshal. Note that, DO NOT use `(t *Time) MarshalJSON() ([]byte, error)` as it looses interface implement of `MarshalJSON` for struct of Time.
Example ¶
package main import ( "encoding/json" "fmt" "github.com/gogf/gf/v2/os/gtime" ) func main() { type Person struct { Name string `json:"name"` Birthday *gtime.Time `json:"birthday"` } p := new(Person) p.Name = "goframe" p.Birthday = gtime.New("2018-08-08 08:08:08") j, _ := json.Marshal(p) fmt.Println(string(j)) }
Output: {"name":"goframe","birthday":"2018-08-08 08:08:08"}
func (*Time) Microsecond ¶
Microsecond returns the microsecond offset within the second specified by t, in the range [0, 999999].
func (*Time) Millisecond ¶
Millisecond returns the millisecond offset within the second specified by t, in the range [0, 999].
func (*Time) Month ¶
Month returns the month of the year specified by t.
Example ¶
Month returns the month of the year specified by t.
package main import ( "fmt" "github.com/gogf/gf/v2/os/gtime" ) func main() { gt := gtime.New("2018-08-08 08:08:08") t1 := gt.Month() fmt.Println(t1) }
Output: 8
func (*Time) Nanosecond ¶
Nanosecond returns the nanosecond offset within the second specified by t, in the range [0, 999999999].
func (*Time) NoValidation ¶
func (t *Time) NoValidation()
NoValidation marks this struct object will not be validated by package gvalid.
func (*Time) Round ¶
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.
Example ¶
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.
package main import ( "fmt" "time" "github.com/gogf/gf/v2/os/gtime" ) func main() { gt := gtime.New("2018-08-08 08:08:08") t := gt.Round(time.Duration(10) * time.Second) fmt.Println(t) }
Output: 2018-08-08 08:08:10
func (*Time) Scan ¶
Scan implements interface used by Scan in package database/sql for Scanning value from database to local golang variable.
func (*Time) Second ¶
Second returns the second offset within the minute specified by t, in the range [0, 59].
Example ¶
Second returns the second offset within the minute specified by t, in the range [0, 59].
package main import ( "fmt" "github.com/gogf/gf/v2/os/gtime" ) func main() { gt := gtime.New("2018-08-08 08:08:08") t1 := gt.Second() fmt.Println(t1) }
Output: 8
func (*Time) StartOfDay ¶
StartOfDay clones and returns a new time which is the start of day, its time is set to 00:00:00.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/os/gtime" ) func main() { gt1 := gtime.New("2018-08-08 08:08:08") fmt.Println(gt1.StartOfDay()) }
Output: 2018-08-08 00:00:00
func (*Time) StartOfHalf ¶
StartOfHalf clones and returns a new time which is the first day of the half year and its time is set to 00:00:00.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/os/gtime" ) func main() { gt1 := gtime.New("2018-08-08 08:08:08") fmt.Println(gt1.StartOfHalf()) }
Output: 2018-07-01 00:00:00
func (*Time) StartOfHour ¶
StartOfHour clones and returns a new time of which the hour, minutes and seconds are set to 0.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/os/gtime" ) func main() { gt1 := gtime.New("2018-08-08 08:08:08") fmt.Println(gt1.StartOfHour()) }
Output: 2018-08-08 08:00:00
func (*Time) StartOfMinute ¶
StartOfMinute clones and returns a new time of which the seconds is set to 0.
Example ¶
StartOfMinute clones and returns a new time of which the seconds is set to 0.
package main import ( "fmt" "github.com/gogf/gf/v2/os/gtime" ) func main() { gt1 := gtime.New("2018-08-08 08:08:08") fmt.Println(gt1.StartOfMinute()) }
Output: 2018-08-08 08:08:00
func (*Time) StartOfMonth ¶
StartOfMonth clones and returns a new time which is the first day of the month and its is set to 00:00:00
func (*Time) StartOfQuarter ¶
StartOfQuarter clones and returns a new time which is the first day of the quarter and its time is set to 00:00:00.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/os/gtime" ) func main() { gt1 := gtime.New("2018-08-08 08:08:08") fmt.Println(gt1.StartOfQuarter()) }
Output: 2018-07-01 00:00:00
func (*Time) StartOfWeek ¶
StartOfWeek clones and returns a new time which is the first day of week and its time is set to 00:00:00.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/os/gtime" ) func main() { gt1 := gtime.New("2018-08-08 08:08:08") fmt.Println(gt1.StartOfWeek()) }
Output: 2018-08-05 00:00:00
func (*Time) StartOfYear ¶
StartOfYear clones and returns a new time which is the first day of the year and its time is set to 00:00:00.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/os/gtime" ) func main() { gt1 := gtime.New("2018-08-08 08:08:08") fmt.Println(gt1.StartOfYear()) }
Output: 2018-01-01 00:00:00
func (*Time) String ¶
String returns current time object as string.
Example ¶
String returns current time object as string.
package main import ( "fmt" "reflect" "github.com/gogf/gf/v2/os/gtime" ) func main() { gt := gtime.New("2018-08-08 08:08:08") t1 := gt.String() fmt.Println(t1) fmt.Println(reflect.TypeOf(t1)) }
Output: 2018-08-08 08:08:08 string
func (*Time) Sub ¶
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).
Example ¶
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).
package main import ( "fmt" "github.com/gogf/gf/v2/os/gtime" ) func main() { gt1 := gtime.New("2018-08-08 08:08:08") gt2 := gtime.New("2018-08-08 08:08:10") fmt.Println(gt2.Sub(gt1)) }
Output: 2s
func (*Time) Timestamp ¶
Timestamp returns the timestamp in seconds.
Example ¶
Timestamp returns the timestamp in seconds.
package main import ( "fmt" "github.com/gogf/gf/v2/os/gtime" ) func main() { t := gtime.Timestamp() fmt.Println(t) // May output: // 1533686888 }
Output:
func (*Time) TimestampMicro ¶
TimestampMicro returns the timestamp in microseconds.
Example ¶
Timestamp returns the timestamp in microseconds.
package main import ( "fmt" "github.com/gogf/gf/v2/os/gtime" ) func main() { t := gtime.TimestampMicro() fmt.Println(t) // May output: // 1533686888000000 }
Output:
func (*Time) TimestampMicroStr ¶
TimestampMicroStr is a convenience method which retrieves and returns the timestamp in microseconds as string.
func (*Time) TimestampMilli ¶
TimestampMilli returns the timestamp in milliseconds.
Example ¶
Timestamp returns the timestamp in milliseconds.
package main import ( "fmt" "github.com/gogf/gf/v2/os/gtime" ) func main() { t := gtime.TimestampMilli() fmt.Println(t) // May output: // 1533686888000 }
Output:
func (*Time) TimestampMilliStr ¶
TimestampMilliStr is a convenience method which retrieves and returns the timestamp in milliseconds as string.
func (*Time) TimestampNano ¶
TimestampNano returns the timestamp in nanoseconds.
Example ¶
Timestamp returns the timestamp in nanoseconds.
package main import ( "fmt" "github.com/gogf/gf/v2/os/gtime" ) func main() { t := gtime.TimestampNano() fmt.Println(t) // May output: // 1533686888000000 }
Output:
func (*Time) TimestampNanoStr ¶
TimestampNanoStr is a convenience method which retrieves and returns the timestamp in nanoseconds as string.
func (*Time) TimestampStr ¶
TimestampStr is a convenience method which retrieves and returns the timestamp in seconds as string.
Example ¶
TimestampStr is a convenience method which retrieves and returns the timestamp in seconds as string.
package main import ( "fmt" "reflect" "github.com/gogf/gf/v2/os/gtime" ) func main() { t := gtime.TimestampStr() fmt.Println(reflect.TypeOf(t)) }
Output: string
func (*Time) ToLocation ¶
ToLocation converts current time to specified location.
func (*Time) ToZone ¶
ToZone converts current time to specified zone like: Asia/Shanghai.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/os/gtime" ) func main() { 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 }
Output:
func (*Time) Truncate ¶
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.
Example ¶
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.
package main import ( "fmt" "time" "github.com/gogf/gf/v2/os/gtime" ) func main() { gt := gtime.New("2018-08-08 08:08:08") t := gt.Truncate(time.Duration(10) * time.Second) fmt.Println(t) }
Output: 2018-08-08 08:08:00
func (*Time) UnmarshalJSON ¶
UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.
Example ¶
package main import ( "encoding/json" "fmt" "github.com/gogf/gf/v2/os/gtime" ) func main() { type Person struct { Name string `json:"name"` Birthday *gtime.Time `json:"birthday"` } p := new(Person) src := `{"name":"goframe","birthday":"2018-08-08 08:08:08"}` json.Unmarshal([]byte(src), p) fmt.Println(p) // Output // &{goframe 2018-08-08 08:08:08} }
Output:
func (*Time) UnmarshalText ¶
UnmarshalText implements the encoding.TextUnmarshaler interface. Note that it overwrites the same implementer of `time.Time`.
func (*Time) Value ¶
Value is the interface providing the Value method for package database/sql/driver for retrieving value from golang variable to database.
func (*Time) WeeksOfYear ¶
WeeksOfYear returns the point of current week for the year.
Example ¶
WeeksOfYear returns the point of current week for the year.
package main import ( "fmt" "github.com/gogf/gf/v2/os/gtime" ) func main() { gt1 := gtime.New("2018-01-08 08:08:08") fmt.Println(gt1.WeeksOfYear()) }
Output: 2