随笔 - 173,  文章 - 0,  评论 - 0,  阅读 - 10万
1
2
3
r := time.Now()
//运行间隔
d := time.Since(r)

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

time.Now结构体

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
type Time struct {<br>     //以下来自机翻
        //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
}

 

计算时间差的方法实体

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// 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   黑熊一只  阅读(48)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示