Go之time包用法

time包提供了工夫的显示和测量用的函数。日历的计算采纳的是公历。
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 uint64ext  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}

time能够准确到纳秒
示例

package mainimport (    "fmt"    "time")func main() {    t:=time.Now()    fmt.Println(t)//获取以后工夫2021-09-23 10:55:44.831571 +0800 CST m=+0.000090412    fmt.Println(t.Unix())//获取以后工夫工夫戳 1632366278    fmt.Println(t.UnixMilli())//获取以后工夫毫秒 1632366278605    fmt.Println(t.UnixMicro())//获取以后工夫微秒 1632366278605122    fmt.Println(t.UnixNano())//获取以后工夫时纳秒 1632366278605122000    fmt.Println(t.Hour())//获取以后小时 10    fmt.Println(t.Day())//获取以后天 23    fmt.Println(t.Weekday())//获取以后周Thursday    fmt.Println(t.ISOWeek())//获取以后周2021 38    //格式化以后工夫示意    fmt.Println(t.String())//字符型    fmt.Println(t.Format("2006-01-02 15:04:05"))//2021-09-23 11:12:42    fmt.Println(t.Format("2006-01-02"))//2021-09-23    fmt.Println(t.Format("15:04:05"))//11:12:42    //指定工夫戳转换    fmt.Println(time.Unix(1632366278, 0).Format("2006-01-02 15:04:05"))//2021-09-23 11:04:38    //指定工夫转工夫戳    tm2, _ := time.Parse("2006-01-02 15:04:05", "2021-09-23 11:04:38")    fmt.Println( tm2.Unix())//1632395078    //"2021-09-08T08:18:46+08:00" 转2021-09-08 08:18:46    t, _= time.Parse(time.RFC3339, "2021-09-08T08:18:46+08:00")    fmt.Println(t.Format("2006-01-02 15:04:05"))    //2021-09-07T17:01:34.182659088Z 转2021-09-07 17:01:34    t, _= time.Parse(time.RFC3339Nano, "2021-09-07T17:01:34.182659088Z")    fmt.Println(t.Format("2006-01-02 15:04:05"))    //其余格局相似可参考    //ANSIC       = "Mon Jan _2 15:04:05 2006"    //UnixDate    = "Mon Jan _2 15:04:05 MST 2006"    //RubyDate    = "Mon Jan 02 15:04:05 -0700 2006"    //RFC822      = "02 Jan 06 15:04 MST"    //RFC822Z     = "02 Jan 06 15:04 -0700" // 应用数字示意时区的RFC822    //RFC850      = "Monday, 02-Jan-06 15:04:05 MST"    //RFC1123     = "Mon, 02 Jan 2006 15:04:05 MST"    //RFC1123Z    = "Mon, 02 Jan 2006 15:04:05 -0700" // 应用数字示意时区的RFC1123    //RFC3339     = "2006-01-02T15:04:05Z07:00"    //RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00"    //Kitchen     = "3:04PM"    //// 不便的工夫戳    //Stamp      = "Jan _2 15:04:05"    //StampMilli = "Jan _2 15:04:05.000"    //StampMicro = "Jan _2 15:04:05.000000"    //StampNano  = "Jan _2 15:04:05.000000000"     //设置时区 Location    //默认UTC    loc, _ := time.LoadLocation("")    // 服务器设定的时区,个别为CST    //loc, _ := time.LoadLocation("Local")    //loc, _ := time.LoadLocation("Asia/Shanghai")    t.In(loc).Format("2006-01-02 15:04:05");    //1.5s后    now := time.Now()    tp, _ := time.ParseDuration("1.5s")    fmt.Println(tp,tp.Truncate(1000), tp.Seconds(), tp.Nanoseconds())    m1 := now.Add(tp)    fmt.Println(m1)//2021-09-23 14:30:42.006213 +0800 CST m=+1.500352171    //1个小时前    tp, _ = time.ParseDuration("-1h")    m1 = now.Add(tp)    fmt.Println(m1)//2021-09-23 13:30:40.506213 +0800 CST m=-3599.999647829    //休眠工夫    //time.Sleep(time.Duration(10) * time.Second)    // func After(d Duration) <-chan Time  非阻塞,可用于提早    //time.After(time.Duration(10) * time.Second)    // func Since(t Time) Duration 两个工夫点的距离    start := time.Now()    fmt.Println(time.Since(start))   // 等价于 Now().Sub(t), 可用来计算一段业务的耗费工夫    //func Until(t Time) Duration     //  等价于 t.Sub(Now()),t与以后工夫的距离    time3 := "2021-03-20 08:50:29"    time4 := "2021-03-20 08:50:29"    //先把工夫字符串格式化成雷同的工夫类型    t3, _ := time.Parse("2006-01-02 15:04:05", time3)    t4, _ := time.Parse("2006-01-02 15:04:05", time4)    fmt.Println(t3.Equal(t4)) //true    now = time.Now()    //Ticker 类型蕴含一个 channel,有时咱们会遇到每隔一段时间执行的业务(比方设置心跳工夫等),就能够用它来解决,这是一个反复的过程    // 无奈勾销    //tick := time.Tick(1 * time.Minute)    //for _ = range tick {    //    // do something    //}    //NewTicker返回一个新的Ticker,该Ticker蕴含一个通道字段,并会每隔时间段d就向该通道发送过后的工夫。它会调整工夫距离或者抛弃tick信息以适应反馈慢的接收者。如果d<=0会panic。敞开该Ticker能够开释相干资源。    // 可通过调用ticker.Stop勾销    //创立一个周期性的定时器    //设置定时器为3秒    timer := time.NewTimer(3 * time.Second)    fmt.Println("以后工夫为:", time.Now())    t = <-timer.C //从定时器拿数据    fmt.Println("以后工夫为:", t)    timer.Stop()//进行    }

links

https://studygolang.com/pkgdoc

  • 目录
  • 上一节:
  • 下一节: