time包提供了时间的显示和测量用的函数。日历的计算采用的是公历。
一、单调时间
Operating systems provide both a “wall clock,” which is subject to changes for clock synchronization, and a “monotonic clock,” which is not. The general rule is that the wall clock is for telling time and the monotonic clock is for measuring time. Rather than split the API, in this package the Time returned by time.Now contains both a wall clock reading and a monotonic clock reading; later time-telling operations use the wall clock reading, but later time-measuring operations, specifically comparisons and subtractions, use the monotonic clock reading.
操作系统提供两种时间:wall clock(墙上时间,用于时间同步,显示时间)和monotonic clock(单调时间,用于比较,测量时间)。time.Now()返回两种时间(clock和monotonic)分别用于显示和测量。
二、TIME
type Time struct{} // A Time represents an instant in time with nanosecond precision.
Time代表一个纳秒精度的时间点。
A Time represents an instant in time with nanosecond precision.
Programs using times should typically store and pass them as values, not pointers. That is, time variables and struct fields should be of type time.Time, not *time.Time.
Time零值代表时间点January 1, year 1, 00:00:00.000000000 UTC。
const (
Nanosecond Duration = 1
Microsecond = 1000 * Nanosecond
Millisecond = 1000 * Microsecond
Second = 1000 * Millisecond
Minute = 60 * Second
Hour = 60 * Minute
)
now := time.Now() time.Now().Day() time.Now().Minute() time.Now().Month()
type Duration int64 // 表示一段时间间隔,单位纳秒,可表示的最长时间大约290年。
type Location struct{}
type Month int
type Weekday int
func After(d Duration) <-chan Time
var c chan int
func handle(int) {}
func main() {
select {
case m := <-c:
handle(m)
case <-time.After(10 * time.Second):
fmt.Println("timed out")
}
}
func Date(year int, month Month, day, hour, min, sec, nsec int, loc *Location) Time
t := time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)
Date panics if loc is nil.
func Now() Time
func Unix(sec int64, nsec int64) Time
func (t Time) Clock() (hour, min, sec int)
func (t Time) Date() (year int, month Month, day int)
func (t Time) Add(d Duration) Time
func (t Time) After(u Time) bool
func (t Time) Before(u Time) bool
func (t Time) Equal(u Time) bool
func (t Time) UTC() Time //UTC returns t with the location set to UTC.
func (t Time) Unix() int64 //Unix returns t as a Unix time, the number of seconds elapsed since January 1, 1970 UTC.
func (t Time) UnixNano() int64
三、Ticker定时器
func Tick(d Duration) <-chan Time
使用time.Tick(时间间隔)来设置定时器,定时器的本质上是一个通道(channel)。
type Ticker struct{
C <- chan Time // The channel on which the ticks are delivered.
}
A Ticker holds a channel that delivers “ticks” of a clock at intervals.
func NewTicker(d Duration) *Ticker
func (t *Ticker) Reset(d Duration)
func (t *Ticker) Stop()
四、Timer
type Timer struct {
C <-chan Time
// contains filtered or unexported fields
}
The Timer type represents a single event. When the Timer expires, the current time will be sent on C, unless the Timer was created by AfterFunc. A Timer must be created with NewTimer or AfterFunc.
func AfterFunc(d Duration, f func()) *Timer
func NewTimer(d Duration) *Timer
func (t *Timer) Reset(d Duration) bool
func (t *Timer) Stop() bool