r := time.Now()
//运行间隔
d := time.Since(r)

 d就是对r位置,执行到 time.Since位置的间隔,是一个被声明为duration的int类型。

time.Now结构体

type Time struct {
     //以下来自机翻 //wall和ext分别对壁时间秒、壁时间纳秒和壁时间纳秒进行编码,      //以及可选的以纳秒为单位的单调时钟读取。      // //wall从高位到低位对1位标志(hasMonotonic)进行编码, //33位秒字段和30位壁时间纳秒字段。 //纳秒字段在[0999999999]的范围内。      //如果hasMonotonic位为0,则33位字段必须为零     //并且自1年1月1日起的完整有符号64位墙秒存储在ext。      //如果hasMonotonic位为1,则33位字段保持33位      //自1885年1月1日以来的无符号墙秒数,并且ext持有      //有符号的64位单调时钟读取,自进程开始以来为纳秒。 // wall and ext encode the wall time seconds, wall time nanoseconds, // and optional monotonic clock reading in nanoseconds. // // From high to low bit position, wall encodes a 1-bit flag (hasMonotonic), // a 33-bit seconds field, and a 30-bit wall time nanoseconds field. // The nanoseconds field is in the range [0, 999999999]. // If the hasMonotonic bit is 0, then the 33-bit field must be zero // and the full signed 64-bit wall seconds since Jan 1 year 1 is stored in ext. // If the hasMonotonic bit is 1, then the 33-bit field holds a 33-bit // unsigned wall seconds since Jan 1 year 1885, and ext holds a // signed 64-bit monotonic clock reading, nanoseconds since process start. wall uint64 ext int64 //loc指定应用于      //确定分钟、小时、月份、日期和年份      //对应于这个时间。      //零位置表示UTC。      //所有UTC时间都用loc==nil表示,从不使用loc==&utcLoc。 // 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 }

 

计算时间差的方法实体

// Sub returns the duration t-u. If the result exceeds the maximum (or minimum)
// value that can be stored in a Duration, the maximum (or minimum) duration
// will be returned.
// To compute t-d for a duration d, use t.Add(-d).
func (t Time) Sub(u Time) Duration {
	if t.wall&u.wall&hasMonotonic != 0 {
		te := t.ext
		ue := u.ext
		d := Duration(te - ue)
		if d < 0 && te > ue {
			return maxDuration // t - u is positive out of range
		}
		if d > 0 && te < ue {
			return minDuration // t - u is negative out of range
		}
		return d
	}
	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
	}
}

  

 

  

posted on 2023-04-11 17:42  黑熊一只  阅读(44)  评论(0编辑  收藏  举报