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()) int64
- func ISO8601() string
- func Microsecond() int64
- func Millisecond() int64
- func Nanosecond() int64
- func ParseDuration(s string) (time.Duration, error)
- func RFC822() string
- func Second() int64
- func SetTimeZone(zone string) 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) EndOfDay() *Time
- func (t *Time) EndOfHalf() *Time
- func (t *Time) EndOfHour() *Time
- func (t *Time) EndOfMinute() *Time
- func (t *Time) EndOfMonth() *Time
- func (t *Time) EndOfQuarter() *Time
- func (t *Time) EndOfWeek() *Time
- func (t *Time) EndOfYear() *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) 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
Constants ¶
const ( // Short writes for common usage durations. 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 Datetime ¶
func Datetime() string
Datetime returns current datetime in string like "2006-01-02 15:04:05".
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".
func Microsecond ¶
func Microsecond() int64
Microsecond returns the timestamp in microseconds. Deprecated, use TimestampMicro instead.
func Millisecond ¶
func Millisecond() int64
Millisecond returns the timestamp in milliseconds. Deprecated, use TimestampMilli instead.
func Nanosecond ¶
func Nanosecond() int64
Nanosecond returns the timestamp in nanoseconds. Deprecated, use TimestampNano instead.
func ParseDuration ¶ added in v1.13.3
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 RFC822 ¶
func RFC822() string
ISO8601 returns current datetime in RFC822 format like "Mon, 02 Jan 06 15:04 MST".
func Second ¶
func Second() int64
Second returns the timestamp in seconds. Deprecated, use Timestamp instead.
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.
This should be called before package "time" import. Please refer to issue: https://github.com/golang/go/issues/34814
func Timestamp ¶ added in v1.10.1
func Timestamp() int64
Timestamp retrieves and returns the timestamp in seconds.
func TimestampMicro ¶ added in v1.10.1
func TimestampMicro() int64
TimestampMicro retrieves and returns the timestamp in microseconds.
func TimestampMicroStr ¶ added in v1.11.0
func TimestampMicroStr() string
TimestampMicroStr is a convenience method which retrieves and returns the timestamp in microseconds as string.
func TimestampMilli ¶ added in v1.10.1
func TimestampMilli() int64
TimestampMilli retrieves and returns the timestamp in milliseconds.
func TimestampMilliStr ¶ added in v1.11.0
func TimestampMilliStr() string
TimestampMilliStr is a convenience method which retrieves and returns the timestamp in milliseconds as string.
func TimestampNano ¶ added in v1.10.1
func TimestampNano() int64
TimestampNano retrieves and returns the timestamp in nanoseconds.
func TimestampNanoStr ¶ added in v1.11.0
func TimestampNanoStr() string
TimestampNanoStr is a convenience method which retrieves and returns the timestamp in nanoseconds as string.
func TimestampStr ¶ added in v1.11.0
func TimestampStr() string
TimestampStr is a convenience method which retrieves and returns the timestamp in seconds as string.
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.
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.
func NewFromStr ¶
NewFromStr creates and returns a Time object with given string. Note that it returns nil if there's error occurs.
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.
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.
func NewFromTime ¶
NewFromTime creates and returns a Time object with given time.Time object.
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.
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're 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 ¶
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>.
func StrToTimeFormat ¶
StrToTimeFormat parses string <str> to *Time object with given format <format>. The parameter <format> is like "Y-m-d H:i:s".
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".
func (*Time) DaysInMonth ¶
DaysInMonth returns the day count of current month.
func (*Time) EndOfDay ¶ added in v1.15.2
EndOfDay clones and returns a new time which is the end of day the and its time is set to 23:59:59.
func (*Time) EndOfHalf ¶ added in v1.15.2
EndOfHalf clones and returns a new time which is the end of the half year and its time is set to 23:59:59.
func (*Time) EndOfHour ¶ added in v1.15.2
EndOfHour clones and returns a new time of which the minutes and seconds are both set to 59.
func (*Time) EndOfMinute ¶ added in v1.15.2
EndOfMinute clones and returns a new time of which the seconds is set to 59.
func (*Time) EndOfMonth ¶ added in v1.15.2
EndOfMonth clones and returns a new time which is the end of the month and its time is set to 23:59:59.
func (*Time) EndOfQuarter ¶ added in v1.15.2
EndOfQuarter clones and returns a new time which is end of the quarter and its time is set to 23:59:59.
func (*Time) EndOfWeek ¶ added in v1.15.2
EndOfWeek clones and returns a new time which is the end of week and its time is set to 23:59:59.
func (*Time) EndOfYear ¶ added in v1.15.2
EndOfYear clones and returns a new time which is the end of the year and its time is set to 23:59:59.
func (*Time) Equal ¶ added in v1.10.1
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 (*Time) FormatNew ¶ added in v1.10.1
FormatNew formats and returns a new Time object with given custom <format>.
func (*Time) IsLeapYear ¶
IsLeapYear checks whether the time is leap year.
func (*Time) IsZero ¶ added in v1.15.7
IsZero reports whether t represents the zero time instant, January 1, year 1, 00:00:00 UTC.
func (*Time) LayoutNew ¶ added in v1.10.1
LayoutNew formats the time with stdlib layout and returns the new Time object.
func (*Time) MarshalJSON ¶
MarshalJSON implements the interface MarshalJSON for json.Marshal.
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) Nanosecond ¶
Nanosecond returns the nanosecond offset within the second specified by t, in the range [0, 999999999].
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.
func (*Time) Scan ¶ added in v1.15.6
Scanner is an 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].
func (*Time) StartOfDay ¶ added in v1.15.2
StartOfDay clones and returns a new time which is the start of day, its time is set to 00:00:00.
func (*Time) StartOfHalf ¶ added in v1.15.2
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.
func (*Time) StartOfHour ¶ added in v1.15.2
StartOfHour clones and returns a new time of which the hour, minutes and seconds are set to 0.
func (*Time) StartOfMinute ¶ added in v1.15.2
StartOfMinute clones and returns a new time of which the seconds is set to 0.
func (*Time) StartOfMonth ¶ added in v1.15.2
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 ¶ added in v1.15.2
StartOfQuarter clones and returns a new time which is the first day of the quarter and its time is set to 00:00:00.
func (*Time) StartOfWeek ¶ added in v1.15.2
StartOfWeek clones and returns a new time which is the first day of week and its time is set to 00:00:00.
func (*Time) StartOfYear ¶ added in v1.15.2
StartOfYear clones and returns a new time which is the first day of the year and its time is set to 00:00:00.
func (*Time) Sub ¶ added in v1.10.1
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 (*Time) TimestampMicro ¶ added in v1.10.1
TimestampMicro returns the timestamp in microseconds.
func (*Time) TimestampMicroStr ¶ added in v1.11.0
TimestampMicroStr is a convenience method which retrieves and returns the timestamp in microseconds as string.
func (*Time) TimestampMilli ¶ added in v1.10.1
TimestampMilli returns the timestamp in milliseconds.
func (*Time) TimestampMilliStr ¶ added in v1.11.0
TimestampMilliStr is a convenience method which retrieves and returns the timestamp in milliseconds as string.
func (*Time) TimestampNano ¶ added in v1.10.1
TimestampNano returns the timestamp in nanoseconds.
func (*Time) TimestampNanoStr ¶ added in v1.11.0
TimestampNanoStr is a convenience method which retrieves and returns the timestamp in nanoseconds as string.
func (*Time) TimestampStr ¶ added in v1.11.0
TimestampStr is a convenience method which retrieves and returns the timestamp in seconds as string.
func (*Time) ToLocation ¶
ToLocation converts current time to specified location.
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.
func (*Time) UnmarshalJSON ¶ added in v1.9.7
UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.
func (*Time) UnmarshalText ¶ added in v1.15.2
UnmarshalText implements the encoding.TextUnmarshaler interface. Note that it overwrites the same implementer of `time.Time`.
func (*Time) Value ¶ added in v1.15.6
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.