gtime

package
v2.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 8, 2022 License: MIT Imports: 15 Imported by: 405

Documentation

Overview

Package gtime provides functionality for measuring and displaying time.

This package should keep much less dependencies with other packages.

Index

Examples

Constants

View Source
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 FuncCost

func FuncCost(f func()) int64

FuncCost calculates the cost time of function `f` in nanoseconds.

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

func ParseDuration(s string) (duration time.Duration, err error)

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

func SetTimeZone(zone string) (err error)

SetTimeZone sets the time zone for current whole process. The parameter `zone` is an area string specifying corresponding time zone, eg: Asia/Shanghai.

This should be called before package "time" import. 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())

	gtime.SetTimeZone("Asia/Tokyo")
	fmt.Println(gtime.Datetime())
	// May Output:
	// 2018-08-08 08:08:08
	// 2018-08-08 09: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

func ConvertZone(strTime string, toZone string, fromZone ...string) (*Time, error)

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 can be type of: time.Time/*time.Time, string or integer.

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"
	"time"

	"github.com/gogf/gf/v2/os/gtime"
)

func main() {
	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

func NewFromStr

func NewFromStr(str string) *Time

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

func NewFromStrFormat(str string, format string) *Time

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

func NewFromStrLayout(str string, layout string) *Time

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

func NewFromTime(t time.Time) *Time

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

func NewFromTimeStamp(timestamp int64) *Time

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

func ParseTimeFromContent(content string, format ...string) *Time

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 `format` if it's passed.

func StrToTime

func StrToTime(str string, format ...string) (*Time, error)

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

func StrToTimeFormat(str string, format string) (*Time, error)

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

func StrToTimeLayout(str string, layout string) (*Time, error)

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

func (t *Time) Add(d time.Duration) *Time

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

func (t *Time) AddDate(years int, months int, days int) *Time

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

func (t *Time) AddStr(duration string) (*Time, error)

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

func (t *Time) After(u *Time) bool

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

func (t *Time) Before(u *Time) bool

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) Clone

func (t *Time) Clone() *Time

Clone returns a new Time object which is a clone of current time object.

func (*Time) DayOfYear

func (t *Time) DayOfYear() int

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

func (t *Time) DaysInMonth() int

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) EndOfDay

func (t *Time) EndOfDay() *Time

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

func (t *Time) EndOfHalf() *Time

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

func (t *Time) EndOfHour() *Time

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

func (t *Time) EndOfMinute() *Time

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

func (t *Time) EndOfMonth() *Time

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

func (t *Time) EndOfQuarter() *Time

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

func (t *Time) EndOfWeek() *Time

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

func (t *Time) EndOfYear() *Time

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

func (t *Time) Equal(u *Time) bool

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

func (t *Time) Format(format string) string

Format formats and returns the formatted result with 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.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

func (t *Time) FormatNew(format string) *Time

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

func (t *Time) FormatTo(format string) *Time

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) ISO8601

func (t *Time) ISO8601() string

ISO8601 formats the time as ISO8601 and returns it as string.

func (*Time) IsLeapYear

func (t *Time) IsLeapYear() bool

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

func (t *Time) IsZero() bool

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

func (t *Time) Layout(layout string) string

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

func (t *Time) LayoutNew(layout string) *Time

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

func (t *Time) LayoutTo(layout string) *Time

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) Local

func (t *Time) Local() *Time

Local converts the time to local timezone.

func (Time) MarshalJSON

func (t Time) MarshalJSON() ([]byte, error)

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

func (t *Time) Microsecond() int

Microsecond returns the microsecond offset within the second specified by t, in the range [0, 999999].

func (*Time) Millisecond

func (t *Time) Millisecond() int

Millisecond returns the millisecond offset within the second specified by t, in the range [0, 999].

func (*Time) Month

func (t *Time) Month() int

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

func (t *Time) Nanosecond() int

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) RFC822

func (t *Time) RFC822() string

RFC822 formats the time as RFC822 and returns it as string.

func (*Time) Round

func (t *Time) Round(d time.Duration) *Time

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

func (t *Time) Scan(value interface{}) error

Scan implements interface used by Scan in package database/sql for Scanning value from database to local golang variable.

func (*Time) Second

func (t *Time) Second() int

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

func (t *Time) StartOfDay() *Time

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

func (t *Time) StartOfHalf() *Time

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

func (t *Time) StartOfHour() *Time

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

func (t *Time) StartOfMinute() *Time

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

func (t *Time) StartOfMonth() *Time

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

func (t *Time) StartOfQuarter() *Time

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

func (t *Time) StartOfWeek() *Time

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

func (t *Time) StartOfYear() *Time

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

func (t *Time) String() 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

func (t *Time) Sub(u *Time) time.Duration

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

func (t *Time) Timestamp() int64

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

func (t *Time) TimestampMicro() int64

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

func (t *Time) TimestampMicroStr() string

TimestampMicroStr is a convenience method which retrieves and returns the timestamp in microseconds as string.

func (*Time) TimestampMilli

func (t *Time) TimestampMilli() int64

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

func (t *Time) TimestampMilliStr() string

TimestampMilliStr is a convenience method which retrieves and returns the timestamp in milliseconds as string.

func (*Time) TimestampNano

func (t *Time) TimestampNano() int64

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

func (t *Time) TimestampNanoStr() string

TimestampNanoStr is a convenience method which retrieves and returns the timestamp in nanoseconds as string.

func (*Time) TimestampStr

func (t *Time) TimestampStr() string

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

func (t *Time) ToLocation(location *time.Location) *Time

ToLocation converts current time to specified location.

func (*Time) ToZone

func (t *Time) ToZone(zone string) (*Time, error)

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

func (t *Time) Truncate(d time.Duration) *Time

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) UTC

func (t *Time) UTC() *Time

UTC converts current time to UTC timezone.

func (*Time) UnmarshalJSON

func (t *Time) UnmarshalJSON(b []byte) error

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

func (t *Time) UnmarshalText(data []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface. Note that it overwrites the same implementer of `time.Time`.

func (*Time) Value

func (t *Time) Value() (driver.Value, error)

Value is the interface providing the Value method for package database/sql/driver for retrieving value from golang variable to database.

func (*Time) WeeksOfYear

func (t *Time) WeeksOfYear() int

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL