gtime

package
v1.8.3 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2019 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package gtime provides functionality for measuring and displaying time.

Index

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

	// 常用时间格式正则匹配,支持的标准时间格式:
	// "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]*)`
	// 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

获得当前的日期(例如:2006-01-02)

func Datetime

func Datetime() string

获得当前的时间(例如:2006-01-02 15:04:05)

func FuncCost

func FuncCost(f func()) int64

计算函数f执行的时间,单位纳秒

func Microsecond

func Microsecond() int64

获取当前的微秒数

func Millisecond

func Millisecond() int64

获取当前的毫秒数

func Nanosecond

func Nanosecond() int64

获取当前的纳秒数

func Second

func Second() int64

获取当前的秒数(时间戳)

func SetTimeZone

func SetTimeZone(zone string) error

设置当前进程全局的默认时区,如: Asia/Shanghai

Types

type Time

type Time struct {
	time.Time
}

func ConvertZone

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

时区转换

func New

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

创建一个空的时间对象,参数可以是标准库时间对象,可选

func NewFromStr

func NewFromStr(str string) *Time

从字符串转换为时间对象,复杂的时间字符串需要给定格式

func NewFromStrFormat

func NewFromStrFormat(str string, format string) *Time

从字符串转换为时间对象,指定字符串时间格式,format格式形如:Y-m-d H:i:s

func NewFromStrLayout

func NewFromStrLayout(str string, layout string) *Time

从字符串转换为时间对象,通过标准库layout格式进行解析,layout格式形如:2006-01-02 15:04:05

func NewFromTime

func NewFromTime(t time.Time) *Time

标准时间对象转换为自定义的时间对象

func NewFromTimeStamp

func NewFromTimeStamp(timestamp int64) *Time

时间戳转换为时间对象,时间戳支持到纳秒的数值

func Now

func Now() *Time

当前时间对象

func ParseTimeFromContent

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

从字符串内容中(也可以是文件名称等等)解析时间,并返回解析成功的时间对象,否则返回nil。 注意当内容中存在多个时间时,会解析第一个。 format参数可以指定需要解析的时间格式。

func StrToTime

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

字符串转换为时间对象,format参数指定格式的format(如: Y-m-d H:i:s),当指定format参数时效果同StrToTimeFormat方法。 注意:自动解析日期时间时,必须有日期才能解析成功,如果字符串中不带有日期字段,那么解析失败。

func StrToTimeFormat

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

字符串转换为时间对象,指定字符串时间格式,format格式形如:Y-m-d H:i:s

func StrToTimeLayout

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

字符串转换为时间对象,通过标准库layout格式进行解析,layout格式形如:2006-01-02 15:04:05

func (*Time) Add

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

当前时间加上指定时间段

func (*Time) AddDate

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

时间日期计算

func (*Time) AddStr

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

当前时间加上指定时间段(使用字符串格式)

func (*Time) Clone

func (t *Time) Clone() *Time

复制当前时间对象

func (*Time) DayOfYear

func (t *Time) DayOfYear() int

返回一个时间点在当年中是第几天 0到365 有润年情况

func (*Time) DaysInMonth

func (t *Time) DaysInMonth() int

一个时间点所在的月最长有多少天 28至31

func (*Time) Format

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

使用自定义日期格式格式化输出日期。

func (*Time) FormatTo

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

通过自定义格式转换当前日期为新的日期。

func (*Time) IsLeapYear

func (t *Time) IsLeapYear() bool

返回是否是润年

func (*Time) Layout

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

使用标准库格式格式化输出日期。

func (*Time) LayoutTo

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

通过标准库格式转换当前日期为新的日期。

func (*Time) Local

func (t *Time) Local() *Time

时区转换为当前设定的Local时区

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

微秒数

func (*Time) Millisecond

func (t *Time) Millisecond() int64

毫秒数

func (*Time) Nanosecond

func (t *Time) Nanosecond() int64

纳秒数

func (*Time) Round

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

Round将舍入t的结果返回到d的最接近的倍数(从零时间开始)。 中间值的舍入行为是向上舍入。 如果d <= 0,Round返回t剥离任何单调时钟读数但不改变。 Round作为零时间以来的绝对持续时间运行; 它不适用于当时的演示形式。 因此,Round(Hour)可能会返回非零分钟的时间,具体取决于时间的位置。

func (*Time) Second

func (t *Time) Second() int64

秒数(时间戳)

func (*Time) String

func (t *Time) String() string

转换为字符串

func (*Time) ToLocation

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

时区转换为指定的时区(通过time.Location)

func (*Time) ToTime

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

Deprecated. Directly use t.Time instead.

func (*Time) ToZone

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

时区转换为指定的时区(通过时区名称,如:Asia/Shanghai)

func (*Time) Truncate

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

Truncate将舍入t的结果返回到d的倍数(从零时间开始)。 如果d <= 0,则Truncate返回t剥离任何单调时钟读数但不改变。 截断时间作为零时间以来的绝对持续时间运行; 它不适用于当时的演示形式。 因此,截断(小时)可能会返回非零分钟的时间,具体取决于时间的位置。

func (*Time) UTC

func (t *Time) UTC() *Time

时区转换为UTC时区

func (*Time) WeeksOfYear

func (t *Time) WeeksOfYear() int

获取时间点在本年内是第多少周

Jump to

Keyboard shortcuts

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