// Package timex provides time formatting and computation helpers on top of gtime. package timex import ( "github.com/gogf/gf/v2/os/gtime" ) const ( // LayoutDateTime is the conventional datetime layout: 2006-01-02 15:04:05. LayoutDateTime = "2006-01-02 15:04:05" // LayoutDate is the conventional date layout: 2006-01-02. LayoutDate = "2006-01-02" ) // Now returns the current time. func Now() *gtime.Time { return gtime.Now() } // Format returns t formatted with the given Go layout. // When layout is empty, LayoutDateTime is used. // // Note: gtime v2.10.2's Format() takes PHP-style format ("Y-m-d H:i:s"), // so this helper uses the Layout() method which accepts Go layouts. func Format(t *gtime.Time, layout ...string) string { ly := LayoutDateTime if len(layout) > 0 && layout[0] != "" { ly = layout[0] } return t.Layout(ly) } // Timestamp returns the current Unix timestamp in seconds. func Timestamp() int64 { return gtime.Now().Timestamp() } // StartOfDay returns the beginning (00:00:00) of the day containing t. func StartOfDay(t *gtime.Time) *gtime.Time { return gtime.NewFromStr(t.Layout(LayoutDate) + " 00:00:00") } // EndOfDay returns the end (23:59:59) of the day containing t. func EndOfDay(t *gtime.Time) *gtime.Time { return gtime.NewFromStr(t.Layout(LayoutDate) + " 23:59:59") }