gtime

package
v1.9.8 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2019 License: MIT Imports: 7 Imported by: 223

Documentation

Overview

Package gtime provides functionality for measuring and displaying time.

Index

Constants

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

	// Regular expression1(datetime separator supports '-', '/', '.').
	// Eg:
	// "2017-12-14 04:51:34 +0805 LMT",
	// "2017-12-14 04:51:34 +0805 LMT",
	// "2006-01-02T15:04:05Z07:00",
	// "2014-01-17T01:19:15+08:00",
	// "2018-02-09T20:46:17.897Z",
	// "2018-02-09 20:46:17.897",
	// "2018-02-09T20:46:17Z",
	// "2018-02-09 20:46:17",
	// "2018/10/31 - 16:38:46"
	// "2018-02-09",
	// "2018.02.09",
	TIME_REAGEX_PATTERN1 = `(\d{4}[-/\.]\d{2}[-/\.]\d{2})[:\sT-]*(\d{0,2}:{0,1}\d{0,2}:{0,1}\d{0,2}){0,1}\.{0,1}(\d{0,9})([\sZ]{0,1})([\+-]{0,1})([:\d]*)`

	// Regular expression2(datetime separator supports '-', '/', '.').
	// Eg:
	// 01-Nov-2018 11:50:28
	// 01/Nov/2018 11:50:28
	// 01.Nov.2018 11:50:28
	// 01.Nov.2018:11:50:28
	TIME_REAGEX_PATTERN2 = `` /* 134-byte string literal not displayed */
)

Variables

This section is empty.

Functions

func Date

func Date() string

Date returns current date in string like "2006-01-02".

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.

func Millisecond

func Millisecond() int64

Millisecond returns the timestamp in milliseconds.

func Nanosecond

func Nanosecond() int64

Nanosecond returns the timestamp in nanoseconds.

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.

func SetTimeZone

func SetTimeZone(zone string) error

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

Types

type Time

type Time struct {
	time.Time
}

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.

func New

func New(t ...time.Time) *Time

New creates and returns a Time object with given time.Time object. The parameter <t> is optional.

func NewFromStr

func NewFromStr(str string) *Time

NewFromStr creates and returns a Time object with given string.

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.

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.

func NewFromTime

func NewFromTime(t time.Time) *Time

NewFromTime creates and returns a Time object with given time.Time object.

func NewFromTimeStamp

func NewFromTimeStamp(timestamp int64) *Time

NewFromTimeStamp creates and returns a Time object with given timestamp, which can be in seconds to nanoseconds.

func Now

func Now() *Time

Now returns a time object for now.

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'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

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

StrToTime converts string to *Time object. 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

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

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

func (*Time) Add

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

Add adds the duration to current time.

func (*Time) AddDate

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

AddDate adds year, month and day to the time.

func (*Time) AddStr

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

AddStr parses the given duration as string and adds it to current time.

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.

func (*Time) DaysInMonth

func (t *Time) DaysInMonth() int

DaysInMonth returns the day count of current month.

func (*Time) Format

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

Format formats and returns the formatted result with custom <format>.

func (*Time) FormatTo

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

FormatTo formats and returns a new Time object with given custom <format>.

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.

func (*Time) Layout

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

Layout formats the time with stdlib layout and returns the formatted result.

func (*Time) LayoutTo

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

Layout formats the time with stdlib layout and returns the new Time object.

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.

func (*Time) Microsecond

func (t *Time) Microsecond() int64

Microsecond returns the timestamp in microseconds.

func (*Time) Millisecond

func (t *Time) Millisecond() int64

Millisecond returns the timestamp in milliseconds.

func (*Time) Nanosecond

func (t *Time) Nanosecond() int64

Nanosecond returns the timestamp in nanoseconds.

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.

func (*Time) Second

func (t *Time) Second() int64

Second returns the timestamp in seconds.

func (*Time) String

func (t *Time) String() string

String returns current time object as 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.

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.

func (*Time) UTC

func (t *Time) UTC() *Time

UTC converts current time to UTC timezone.

func (*Time) UnmarshalJSON added in v1.9.7

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

UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.

func (*Time) WeeksOfYear

func (t *Time) WeeksOfYear() int

WeeksOfYear returns the point of current week for the year.

Jump to

Keyboard shortcuts

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