type Time struct { // sec gives the number of seconds elapsed since // January 1, year 1 00:00:00 UTC. sec int64 // nsec specifies a non-negative nanosecond // offset within the second named by Seconds. // It must be in the range [0, 999999999]. nsec int32 // loc specifies the Location that should be used to // determine the minute, hour, month, day, and year // that correspond to this Time. // The nil location means UTC. // All UTC times are represented with loc==nil, never loc==&utcLoc. loc *Location }
// After reports whether the time instant t is after u. func(t Time)After(u Time)bool { return t.sec > u.sec || t.sec == u.sec && t.nsec > u.nsec }
// Before reports whether the time instant t is before u. func(t Time)Before(u Time)bool { return t.sec < u.sec || t.sec == u.sec && t.nsec < u.nsec }
// Equal reports whether t and u represent the same time instant. // Two times can be equal even if they are in different locations. // For example, 6:00 +0200 CEST and 4:00 UTC are Equal. // Do not use == with Time values. func(t Time)Equal(u Time)bool { return t.sec == u.sec && t.nsec == u.nsec }
func(t Time)Sub(u Time)Duration { d := Duration(t.sec-u.sec)*Second + Duration(t.nsec-u.nsec) // Check for overflow or underflow. switch { case u.Add(d).Equal(t): return d // d is correct case t.Before(u): return minDuration // t - u is negative out of range default: return maxDuration // t - u is positive out of range }
// 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. type Timer struct { C <-chan Time r runtimeTimer }
// NewTimer creates a new Timer that will send // the current time on its channel after at least duration d. funcNewTimer(d Duration) * Timer { c := make(chan Time, 1) t := &Timer{ C: c, r: runtimeTimer{ when: when(d), f: sendTime, arg: c, }, } startTimer(&t.r) return t }
funcAfterFunc(d Duration, f func()) * Timer { t := &Timer{ r: runtimeTimer{ when: when(d), f: goFunc, arg: f, }, } startTimer(&t.r) return t }
func(t * Timer)Reset(d Duration)bool { if t.r.f == nil { panic("time: Reset called on uninitialized Timer") } w := when(d) active := stopTimer(&t.r) t.r.when = w startTimer(&t.r) return active }
After函数
1 2 3 4 5 6 7 8 9
// After waits for the duration to elapse and then sends the current time // on the returned channel. // It is equivalent to NewTimer(d).C. // The underlying Timer is not recovered by the garbage collector // until the timer fires. If efficiency is a concern, use NewTimer // instead and call Timer.Stop if the timer is no longer needed. funcAfter(d Duration) <-chanTime { return NewTimer(d).C }
// Stop prevents the Timer from firing. // It returns true if the call stops the timer, false if the timer has already // expired or been stopped. // Stop does not close the channel, to prevent a read from the channel succeeding // incorrectly. // // To prevent a timer created with NewTimer from firing after a call to Stop, // check the return value and drain the channel. // For example, assuming the program has not received from t.C already: // if !t.Stop() { // <-t.C // } // This cannot be done concurrent to other receives from the Timer's // channel. // // For a timer created with AfterFunc(d, f), if t.Stop returns false, then the timer // has already expired and the function f has been started in its own goroutine; // Stop does not wait for f to complete before returning. // If the caller needs to know whether f is completed, it must coordinate // with f explicitly. func(t * Timer)Stop()bool { if t.r.f == nil { panic("time: Stop called on uninitialized Timer") } return stopTimer(&t.r) }