Go语言——time包
时间:2022-09-26 15:00:01
概述:
- 需要先import “time”。time该包提供了显示和测量时间的函数。
算采是公历。
time包中类型及方法
- type Weekday
func (d Weekday) String() string - type Month
func (m Month) String() string - type Location
func LoadLocation(name string) (*Location, error)
func FixedZone(name string, offset int) *Location
func (l *Location) String() string - type Time
func Date(year int, month Month, day, hour, min, sec, nsec int, loc*Location) Time
func Parse(layout, value string) (Time, error)
func ParseInLocation(layout, value string, loc *Location) (Time, error)
func Now() Time
func Unix(sec int64, nsec int64) Time
func (t Time) Location() *Location
func (t Time) Zone() (name string, offset int)
func (t Time) IsZero() bool
func (t Time) Local() Time
func (t Time) UTC() Time
func (t Time) In(loc *Location) Time
func (t Time) Unix() int64
func (t Time) UnixNano() int64
func (t Time) Equal(u Time) bool
func (t Time) Before(u Time) bool
func (t Time) After(u Time) bool
func (t Time) Date() (year int, month Month, day int)
func (t Time) Clock() (hour, min, sec int)
func (t Time) Year() int
func (t Time) Month() Month
func (t Time) ISOWeek() (year, week int)
func (t Time) YearDay() int
func (t Time) Day() int
func (t Time) Weekday() Weekday
func (t Time) Hour() int
func (t Time) Minute() int
func (t Time) Second() int
func (t Time) Nanosecond() int
func (t Time) Add(d Duration) Time
func (t Time) AddDate(years int, months int, days int) Time
func (t Time) Sub(u Time) Duration
func (t Time) Round(d Duration) Time
func (t Time) Truncate(d Duration) Time
func (t Time) Format(layout string) string
func (t Time) String() string
func (t Time) GobEncode() ([]byte, error)
func (t *Time) GobDecode(data []byte) error
func (t Time) MarshalBinary() ([]byte, error)
func (t *Time) UnmarshalBinary(data []byte) error
func (t Time) MarshalJSON() ([]byte, error)
func (t *Time) UnmarshalJSON(data []byte) error
func (t Time) MarshalText() ([]byte, error)
func (t *Time) UnmarshalText(data []byte) error - type Duration
func ParseDuration(s string) (Duration, error)
func Since(t Time) Duration
func (d Duration) Hours() float64
func (d Duration) Minutes() float64
func (d Duration) Seconds() float64
func (d Duration) Nanoseconds() int64
func (d Duration) String() string - type Timer
func NewTimer(d Duration) *Timer
func AfterFunc(d Duration, f func()) *Timer
func (t *Timer) Reset(d Duration) bool
func (t *Timer) Stop() bool - type Ticker
func NewTicker(d Duration) *Ticker
func (t *Ticker) Stop() - const (
Nanosecond Duration = 1 //纳秒ns
Microsecond = 1000 * Nanosecond //微妙us
Millisecond = 1000 * Microsecond //毫秒ms
Second = 1000 * Millisecond //秒s
Minute = 60 * Second
Hour = 60 * Minute
)
time包中核法介绍
- func Now() Time
- Now返回当地时间。
- func (t Time) Local() Time
- Local将时间转换为本地时区,但指向同一时间点Time。
- func (t Time) UTC() Time
- UTC将时间转成UTC和零时区,但指向同?时间点的Time。
- 通过伦敦格林尼治天台原址的国际经线称为0°经线,又称本初午线。
- func Date(year int, month Month, day, hour, min, sec, nsec int, loc*Location) Time
- Date可根据指定值返回时间。loc,时间格式如下:yearmonth-day hour:min:sec nsec nanoseconds的时间点。loc可以是time.Local、time.UTC。string转time
- func Parse(layout, value string) (Time, error)
- Parse能够将格式化的时间字符串分析成它所代表的时间。string转time
- layout定义参考时间:Mon Jan 2 15c04c05 -0700 MST 2006
- 若缺乏表示时区的信息,Parse将时区设置为UTC。layout简写格式:Mon Jan 2 15c04c05 2006
- 预定义的ANSIC、UnixDate、RFC339和其他版式描述了参考时间的标准或方便表示。
- func (t Time) Format(layout string) string
- Format根据layout指定格式返回t代表时间点的格式化本表示。time转string
- layout定义参考时间:Mon Jan 2 15c04c05 -0700 MST 2006
- func (t Time) String() string
- String将时间格式化成字符串(time转string,相当于固定格式Format?法律):2006-01-02 15c04c05.999999999 -0700 MST”
- func (t Time) Unix() int64
- Uni将t表示为Unix时间(时间戳,int64),即从时间点January 1, 1970UTC到时间点t所经过的时间(单位秒)。
- func (t Time) UnixNano() int64
- UnixNano将t表示为Unix时间,即从时间点January 1, 1970 UTC到时间点t所经过的时间(单位纳秒)。
- func (t Time) Equal(u Time) bool
- 判断两个时间是否相同,会考虑时区的影响,因此不同时区标准的时间也可以正确⽐较。本⽅法和⽤t==u不同,这种⽅法还会⽐较地点和时区信息。
- func (t Time) Before(u Time) bool
- 如果t代表的时间点在u之前,返回真;否则返回假。
- func (t Time) After(u Time) bool
- 如果t代表的时间点在u之后,返回真;否则返回假。
- func (t Time) Date() (year int, month Month, day int)
- 返回时间点t对应的年、⽉、⽇。
- func (t Time) Year() int
- 返回时间点t对应的年份。
- func (t Time) Month() Month
- 返回时间点t对应那⼀年的第⼏⽉。
- func (t Time) Day() int
- 返回时间点t对应那⼀⽉的第⼏⽇。
- func (t Time) Weekday() Weekday
- 返回时间点t对应的那⼀周的周⼏。
- func (t Time) Clock() (hour, min, sec int)
- 返回t对应的那⼀天的时、分、秒。
- func (t Time) Hour() int
- 返回t对应的那⼀天的第⼏⼩时,范围[0, 23]。
- func (t Time) Minute() int
- 返回t对应的那⼀⼩时的第⼏分种,范围[0, 59]。
- func (t Time) Second() int
- 返回t对应的那⼀分钟的第⼏秒,范围[0, 59]。
- func (t Time) Nanosecond() int
- 返回t对应的那⼀秒内的纳秒偏移量,范围[0, 999999999]。
- func (t Time) Sub(u Time) Duration
- 返回⼀个时间段t-u。如果结果超出了Duration可以表示的最⼤值/最⼩值,将返回最⼤值/最⼩值。要获取时间点t-d(d为Duration),可以使⽤t.Add(-d)。
- func (d Duration) Hours() float64
- Hours将时间段表示为float64类型的⼩时数。
- func (d Duration) Minutes() float64
- Minutes将时间段表示为float64类型的分钟数。
- func (d Duration) Seconds() float64
- Seconds将时间段表示为float64类型的秒数。
- func (d Duration) Nanoseconds() int64
- Nanoseconds将时间段表示为int64类型的纳秒数,等价于int64(d)。
- func (d Duration) String() string
- 返回时间段采⽤"72h3m0.5s"格式的字符串表示。最前⾯可以有符号,数字+单位为⼀个单元,开始部分的0值单元会被省略;如果时间段<1s,会使⽤"ms"、“us”、“ns"来保证第⼀个单元的数字不是0;如果时间段为0,会返回"0”。
- func ParseDuration(s string) (Duration, error)
- ParseDuration解析⼀个时间段字符串。⼀个时间段字符串是⼀个序列,每个⽚段包含可选的正负号、⼗进制数、可选的⼩数部分和单位后缀,如"300ms"、“-1.5h”、“2h45m”。合法的单位有"ns"、“us”/“µs”、“ms”、“s”、“m”、“h”。
- func (t Time) Add(d Duration) Time
- Add返回时间点t+d。
- func (t Time) AddDate(years int, months int, days int) Time
- AddDate返回增加了给出的年份、⽉份和天数的时间点Time。例如,时间点January 1, 2011调⽤AddDate(-1, 2, 3)会返回March 4, 2010。
- AddDate会将结果规范化,类似Date函数的做法。因此,举个例⼦,给时间点October 31添加⼀个⽉,会⽣成时间点December 1。(从时间点November 31规范化⽽来)