package time提供测量和显示时间的能力。
Index
- Constants
- func After(d Duration) <-chan Time
- func Sleep(d Duration)
- func Tick(d Duration) <-chan Time
- type Duration
- type Location
- type Month
- type ParseError
- type Ticker
- type Time
- func Date(year int, month Month, day, hour, min, sec, nsec int, loc *Location) Time
- func Now() Time
- func Parse(layout, value string) (Time, error)
- func ParseInLocation(layout, value string, loc *Location) (Time, error)
- func Unix(sec int64, nsec int64) Time
- func (t Time) Add(d Duration) Time
- func (t Time) AddDate(years int, months int, days int) Time
- func (t Time) After(u Time) bool
- func (t Time) Before(u Time) bool
- func (t Time) Clock() (hour, min, sec int)
- func (t Time) Date() (year int, month Month, day int)
- func (t Time) Day() int
- func (t Time) Equal(u Time) bool
- func (t Time) Format(layout string) string
- func (t *Time) GobDecode(buf []byte) error
- func (t Time) GobEncode() ([]byte, error)
- func (t Time) Hour() int
- func (t Time) ISOWeek() (year, week int)
- func (t Time) In(loc *Location) Time
- func (t Time) IsZero() bool
- func (t Time) Local() Time
- func (t Time) Location() *Location
- func (t Time) MarshalJSON() ([]byte, error)
- func (t Time) Minute() int
- func (t Time) Month() Month
- func (t Time) Nanosecond() int
- func (t Time) Round(d Duration) Time
- func (t Time) Second() int
- func (t Time) String() string
- func (t Time) Sub(u Time) Duration
- func (t Time) Truncate(d Duration) Time
- func (t Time) UTC() Time
- func (t Time) Unix() int64
- func (t Time) UnixNano() int64
- func (t *Time) UnmarshalJSON(data []byte) (err error)
- func (t Time) Weekday() Weekday
- func (t Time) Year() int
- func (t Time) YearDay() int
- func (t Time) Zone() (name string, offset int)
- type Timer
- type Weekday
Examples
Package Files
format.go sleep.go sys_unix.go tick.go time.go zoneinfo.go zoneinfo_read.go zoneinfo_unix.go
1. 基本函数
func After
func After(d Duration) <-chan Time
在等待了d时间间隔后,向返回channel发送当前时间。与NewTimer(d).C等效。事例如下:
select {
case m := <-c:
handle(m)
case <-time.After(5 * time.Minute):
fmt.Println("timed out")
}
主要用于超时处理,以防死锁。
func Sleep
func Sleep(d Duration)
挂起当前goroutine,d时间间隔。
time.Sleep(100 * time.Millisecond)
func Tick
func Tick(d Duration) <-chan Time
Tick是NewTicker的一个封装,只用于访问ticking channel;用户不需要关闭ticker。示例:
c := time.Tick(1 * time.Minute)
for now := range c {
fmt.Printf("%v %s\n", now, statusUpdate())
}
2. type Duration
type Duration
type Duration int64
Duration是两个纳秒时间标记之间的间隔,其表示的最大间隔是290年。
const (
Nanosecond Duration = 1
Microsecond = 1000 * Nanosecond
Millisecond = 1000 * Microsecond
Second = 1000 * Millisecond
Minute = 60 * Second
Hour = 60 * Minute
)
上面的常量都是常用的duration,需要记住。
如果将一个整数转化成一个duration的话,需要这么做:
seconds := 10
fmt.Print(time.Duration(seconds)*time.Second) // prints 10s
计算经过的时间间隔的示例如下,此示例很通用:
t0 := time.Now()
expensiveCall()
t1 := time.Now()
fmt.Printf("The call took %v to run.\n", t1.Sub(t0))
func Since
func Since(t Time) Duration
Since返回从时间t开始的已流逝时间,它是time.Now().Sub(t)的浓缩版。
3. type Month
type Month
type Month int
const (
January Month = 1 + iota
February
March
April
May
June
July
August
September
October
November
December
)
注意其写法。
4. type Ticker
type Ticker
type Ticker struct {
C <-chan Time // The channel on which the ticks are delivered.
// contains filtered or unexported fields
}
Ticker包含一个channel,此channel以固定间隔发送ticks。
func NewTicker
func NewTicker(d Duration) *Ticker
返回一个Ticker,此Ticker包含一个channel,此channel以给定的duration发送时间。duration d必须大于0.
func (*Ticker) Stop
func (t *Ticker) Stop()
用于关闭相应的Ticker,但并不关闭channel。
5. type Time
type Time
type Time struct { // contains filtered or unexported fields }
Time以纳秒来表示一个时刻。程序应该存储或传递Time值,而不是指针,即time.Time,而非*time.Time。
6. type Timer
type Timer
type Timer struct {
C <-chan Time
// contains filtered or unexported fields
}
Timer type代表了一个事件,如果不是被AfterFunc创建的Timer,当Timer过期,当前时间将发送到C。
func AfterFunc
func AfterFunc(d Duration, f func()) *Timer
AfterFunc等待duration耗尽后,在当前的goroutine中调用f函数。它返回一个Timer,利用此Timer的Stop方法,可以取消调用f函数。