• Golang标准包time总结


    时间

    标准包Time

    Golang的time标准包

    type Time struct {
        // 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 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

    Time结构的官方解释:Time结构代表具有纳秒精度的瞬间

    程序在存储和传递Time时应该实用值(t Time)而不是指针(t *Time),即Time类型的变量和结构字段的类型应为time.Time,而不是time.Time

    可以看到三个属性
    在这里插入图片描述

    Go语言出生时便使用uint64,因此全部用于表示时间时则会是一个非常大的时间:

    package main
    
    import "time"
    
    const UINT64_MIN uint64 = 0
    const UINT64_MAX uint64 = ^uint64(0)
    const form = "2006-01-02 15:04:05"
    
    func main() {
    
        t, _ := time.Parse(form, "9999-12-31 23:59:59")
        println("unint64 的最大值为:", UINT64_MAX)
        println("9999-12-31 23:59:59 的时间戳为:", t.Unix())
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    结果:

    unint64 的最大值为: 18446744073709551615
    9999-12-31 23:59:59 的时间戳为: 253402300799
    
    • 1
    • 2

    因此wall(64位)中的某些位可以拿来做其他事情,比如标识是否包含monotonic clocks等。
    实际上Time结构也正是如此,官方文档:

    // 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.
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    1. wallext两个属性分别用于编码wall time中的总秒数和wall time中的纳秒数,还可能包含了可选的monotonic clock的纳秒格式的读数
    2. 从高位到低位,wall 编码时,第一个位(1-bit)是一个标识符位,用于标识是否包含了monotonic clock。可以将wall与hasMonotonic做位与(&)运算来检查

    hasMonotonic定义: hasMonotonic = 1 << 63

    1. 接下来的33位和最后的30位,分别用于编码wall time的总秒数和纳秒数, 纳秒数取值范围:[0,999999999]
    2. 如果hasMonotonic位取值为0,则中间的33位会被设置为且必须为0, 然后会将自 1 年 1 月 1 日 0 时 0 分 0 纳秒的有符号的 wall clock 总秒数都存储在 ext 中。但是,纳秒数还是存储在原来的位置,也就是 wall 的剩余的 30 位
    3. 如果 hasMonotonic 位的取值为 1 ,那么中间的 33 位存储了自 1885 1 年 1 月 1 日 0 时 0 分 0 纳秒的以来的无符号的 wall clock 的总秒数,而 ext 则存储了有符号的 64 位的 monotonic clock 读数,也就是,进程创建以来的总纳秒数

    至于loc这个属性, loc用于指定了用于确定与此时间对应的分、时、月、日、年的时区

    如果loc值为null, 则表示UTC。 所有的UTC时间都用loc == nil 表示, 而不是 loc == &utcLoc

    时间戳转换

    
    package main
    
    import (
    	"log"
    	"time"
    )
    
    func main() {
    	t := int64(1546926630)      //外部传入的时间戳(秒为单位),必须为int64类型
    	t1 := "2019-01-08 13:50:30" //t1的时间字符串
    
    	//时间转换的模板,golang里面只能是 "2006-01-02 15:04:05" (go的诞生时间)
    	timeTemplate1 := "2006-01-02 15:04:05" //常规类型
    	timeTemplate2 := "2006/01/02 15:04:05" //其他类型
    	timeTemplate3 := "2006-01-02"          //其他类型
    	timeTemplate4 := "15:04:05"            //其他类型
    	timeTemplate5 := "20060102150405"      //其他类型
    
    	// ======= 将时间戳格式化为日期字符串 =======
    	log.Println(time.Unix(t, 0).Format(timeTemplate1)) //输出:2019-01-08 13:50:30
    	log.Println(time.Unix(t, 0).Format(timeTemplate2)) //输出:2019/01/08 13:50:30
    	log.Println(time.Unix(t, 0).Format(timeTemplate3)) //输出:2019-01-08
    	log.Println(time.Unix(t, 0).Format(timeTemplate4)) //输出:13:50:30
    	log.Println(time.Unix(t, 0).Format(timeTemplate5)) //输出:20190108135030
    
    	// ======= 将时间字符串转换为时间戳 =======
    	stamp, _ := time.ParseInLocation(timeTemplate1, t1, time.Local) //使用parseInLocation将字符串格式化返回本地时区时间
    	log.Println(stamp.Unix())                                       //输出:1546926630
    }
    
    
    • 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

    获取想要的时间格式——string

    // time对象
      timeStr := time.Now().Format("2006-01-02")
    	println(timeStr) // 2022-09-18
    // 时间戳
      t := int64(1546926630)
      println(time.Unix(t, 0).Format("2006-01-02")) //2019-01-08
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    获取想要的时间戳——int64

      timeStr := time.Now().Format("2006-01-02")
      t5, _ := time.ParseInLocation("2006-01-02", timeStr, time.Local)
    	timeUnix := t5.Unix()
    	println(timeUnix) // 1663430400
    
    • 1
    • 2
    • 3
    • 4

    在这里插入图片描述

    处理时间差

    time常用方法
    
    After(u Time) bool 
    时间类型比较,是否在Time之后
    
    Before(u Time) bool 
    时间类型比较,是否在Time之前
    
    Equal(u Time) bool 
    比较两个时间是否相等
    
    IsZero() bool 
    判断时间是否为零值,如果sec和nsec两个属性都是0的话,则该时间类型为0
    
    Date() (year int, month Month, day int) 
    返回年月日,三个参数
    
    Year() int 
    返回年份
    
    Month() Month 
    返回月份.是Month类型
    
    Day() int 
    返回多少号
    
    Weekday() Weekday 
    返回星期几,是Weekday类型
    
    ISOWeek() (year, week int) 
    返回年份,和该填是在这年的第几周.
    
    Clock() (hour, min, sec int) 
    返回小时,分钟,秒
    
    Hour() int 
    返回小时
    
    Minute() int 
    返回分钟
    
    Second() int 
    返回秒数
    
    Nanosecond() int 
    返回纳秒
    
    • 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
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    // Add 时间相加
    	now := time.Now()
    	// ParseDuration parses a duration string.
    	// A duration string is a possibly signed sequence of decimal numbers,
    	// each with optional fraction and a unit suffix,
    	// such as "300ms", "-1.5h" or "2h45m".
    	//  Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
    	
    	// 10分钟前
    	m, _ := time.ParseDuration("-1m")
    	m1 := now.Add(m)
    	fmt.Println(m1)
    	// 一天后
    	dd, _ := time.ParseDuration("24h")
    	dd1 := now.Add(dd)
    	fmt.Println(dd1)
    	
    	
    	// Sub 计算两个时间差
    	subM := now.Sub(m1)
    	fmt.Println(subM.Minutes(), "分钟")
    
    	sumD := now.Sub(dd1)
    	fmt.Printf("%v 天\n", sumD.Hours()/24)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
  • 相关阅读:
    AutoCAD Civil 3D中文本地化模板下载及设置
    开放式耳机百元价位怎么选、公认最好的百元开放式耳机
    2021 ICPC Southeastern Europe Regional Contest(更新至五题)
    linux环境下使用lighthouse与selenium
    开发工程师必备————【Day23】Django基础知识
    AutoJs学习-实现区域截图+文字识别+摇一摇截图+截图查看器
    中国石油大学(北京)-《 油层物理》第三阶段在线作业
    Java空指针异常的正确理解
    流量调度、微服务可寻址性和注册中心
    中断系统结构及中断控制详解
  • 原文地址:https://blog.csdn.net/qq_45366447/article/details/126922830