zoukankan      html  css  js  c++  java
  • golang ---timeb

    golang 提供了以下两种基础类型 
    - 时间点(Time) 
    - 时间段(Duration)

    除此之外 golang 也提供了以下类型,做一些特定的业务 
    - 时区(Location) 
    - Ticker 
    - Timer(定时器)

    时间点(Time)

    package main
    
    import (
    	"fmt"
    	"time"
    )
    
    func main() {
    	fmt.Println(time.Now())
    
    	// longForm shows by example how the reference time would be represented in
    	// the desired layout.
    	const longForm = "Jan 2, 2006 at 3:04pm (MST)"
    	t, _ := time.Parse(longForm, "Oct 11, 2019 at 7:14pm (PST)")
    	fmt.Println(t)
    
    	// shortForm is another way the reference time would be represented
    	// in the desired layout; it has no time zone present.
    	// Note: without explicit zone, returns time in UTC.
    	const shortForm = "2006-Jan-02"
    	t, _ = time.Parse(shortForm, "2019-Oct-11")
    	fmt.Println(t)
    
    	// func ParseInLocation(layout, value string, loc *Location) (Time, error) (layout已带时区时可直接用Parse)
    	t, _ = time.ParseInLocation("2006-01-02 15:04:05", "2019-10-11 14:06:06", time.Local)
    	fmt.Println(t)
    
    	t = time.Unix(1e9, 0)
    	fmt.Println(t) // func Unix(sec int64, nsec int64) Time
    
    	// func Date(year int, month Month, day, hour, min, sec, nsec int, loc *Location) Time
    	fmt.Println(time.Date(2019, 10, 11, 15, 30, 10, 0, time.Local))
    
    	// func (t Time) In(loc *Location) Time 当前时间对应指定时区的时间
    	loc, _ := time.LoadLocation("America/Los_Angeles")
    	fmt.Println(time.Now().In(loc))
    
    } 

    其中layout的时间必须是"2006-01-02 15:04:05"这个时间,不管格式如何,时间点一定得是这个,如:"Jan 2, 2006 at 3:04pm (MST)""2006-Jan-02"等。如换一个时间解析出来的时间就不对了,要特别注意这一点。

     获取到时间点之后为了满足业务和设计,需要转换成我们需要的格式,也就是所谓的时间格式化。

    格式化

    to string

    格式化为字符串我们需要使用 time.Format 方法来转换成我们想要的格式

    package main
    
    import (
    	"fmt"
    	"time"
    )
    
    func main() {
    	fmt.Println(time.Now().Format("2006-01-02 15:04:05"))
    	fmt.Println(time.Now().Format(time.UnixDate))
    
    }
    

     输出:

    2019-10-11 19:35:25
    Fri Oct 11 19:35:25 CST 2019
    

     Format 函数中可以指定你想使用的格式,同时 time 包中也给了一些我们常用的格式

    const (
        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 with numeric zone
        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 with numeric zone
        RFC3339     = "2006-01-02T15:04:05Z07:00"
        RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00"
        Kitchen     = "3:04PM"
        // Handy time stamps.
        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"
    )     
    

    注意: galang 中指定的特定时间格式为 "2006-01-02 15:04:05 -0700 MST", 为了记忆方便,按照美式时间格式 月日时分秒年 外加时区 排列起来依次是 01/02 03:04:05PM ‘06 -0700,刚开始使用时需要注意。

     

    to time stamp

    package main
    
    import (
    	"fmt"
    	"time"
    )
    
    func main() {
    
    	fmt.Println(time.Now().Unix())
    
    	// 获取指定日期的时间戳
    	dt, _ := time.ParseInLocation("2016-01-02 15:04:05", "2019-10-11 12:24:51", time.UTC)
    
    	fmt.Println(dt)
    	fmt.Println(dt.Unix())
    
    	fmt.Println(time.Date(2019, 10, 11, 15, 30, 10, 0, time.Local).Unix())
    
    }
    

      输出:

    1570794525
    0001-01-01 00:00:00 +0000 UTC
    -62135596800
    1570779010
    

      

    其他

    time 包还提供了一些常用的方法,基本覆盖了大多数业务,从方法名就能知道代表的含义

    func (t Time) Date() (year int, month Month, day int)
          func (t Time) Clock() (hour, min, sec int)
          func (t Time) Year() int
          func (t Time) Month() Month
          func (t Time) Day() int
          func (t Time) Hour() int
          func (t Time) Minute() int
          func (t Time) Second() int
          func (t Time) Nanosecond() int
          func (t Time) YearDay() int
          func (t Time) Weekday() Weekday
          func (t Time) ISOWeek() (year, week int)
          func (t Time) IsZero() bool
          func (t Time) Local() Time
          func (t Time) Location() *Location
          func (t Time) Zone() (name string, offset int)
          func (t Time) Unix() int64
    

      

    时间段(Duartion)

    时间段,即 Duartion 类型

    // func ParseDuration(s string) (Duration, error)
          tp, _ := time.ParseDuration("1.5s")
          fmt.Println(tp.Truncate(1000), tp.Seconds(), tp.Nanoseconds())
    
          func (d Duration) Hours() float64
          func (d Duration) Minutes() float64
          func (d Duration) Seconds() float64
          func (d Duration) Nanoseconds() int64
          func (d Duration) Round(m Duration) Duration         // 四舍五入
          func (d Duration) Truncate(m Duration) Duration      // 向下取整
    

     

    时区(Location)

    package main
    
    import (
    	"fmt"
    	"time"
    )
    
    func main() {
    
    	// 默认UTC
    	loc1, err := time.LoadLocation("")
    	if err != nil {
    		fmt.Println(err)
    	}
    	fmt.Println(loc1)
    
    	// 服务器设定的时区,一般为CST
    	loc2, err := time.LoadLocation("Local")
    	if err != nil {
    		fmt.Println(err)
    	}
    	fmt.Println(loc2)
    
    	// 美国洛杉矶PDT
    	loc3, err := time.LoadLocation("America/Los_Angeles")
    	if err != nil {
    		fmt.Println(err)
    	}
    	fmt.Println(loc3)
    
    	// 获取指定时区的时间点
    	local, _ := time.LoadLocation("America/Los_Angeles")
    	fmt.Println(time.Date(2018, 1, 1, 12, 0, 0, 0, local))
    
    }
    

      输出:

    UTC
    Local
    America/Los_Angeles
    2018-01-01 12:00:00 -0800 PST
    

      可以在 $GOROOT/lib/time/zoneinfo.zip 文件下看到所有时区

     

    时间运算

    时间运算相关的函数

    package main
    
    import (
    	"fmt"
    	"time"
    )
    
    func main() {
    	start := time.Now()
    
    	// func Sleep(d Duration)   休眠多少时间,休眠时处于阻塞状态,后续程序无法执行
    	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 两个时间点的间隔
    
    	fmt.Println(time.Since(start)) // 等价于 Now().Sub(t), 可用来计算一段业务的消耗时间
    
    	// func Until(t Time) Duration     //  等价于 t.Sub(Now()),t与当前时间的间隔
    	fmt.Println(time.Until(start))
    
    	dt := time.Now()
    
    	// func (t Time) Add(d Duration) Time
    	fmt.Println(dt.Add(time.Duration(60) * time.Second)) // 加
    
    	// func (t Time) Sub(u Time) Duration                    // 减
    
    	// func (t Time) AddDate(years int, months int, days int) Time
    	fmt.Println(dt.AddDate(0, 1, 1))
    
    	// func (t Time) Before(u Time) bool
    	// func (t Time) After(u Time) bool
    	// func (t Time) Equal(u Time) bool          比较时间点时尽量使用Equal函数
    
    	fmt.Println(dt.After(start))
    
    }
    

      时间差运算实例:

    package main
    
    import (
    	"fmt"
    	"strings"
    	"time"
    )
    
    func main() {
    	// 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("-10m")
    	m1 := now.Add(m)
    	fmt.Println("m1: ", m1)
    
    	// 8个小时前
    	h, _ := time.ParseDuration("-1h")
    	h1 := now.Add(8 * h)
    	fmt.Println("h1: ", h1)
    
    	// 一天前
    	d, _ := time.ParseDuration("-24h")
    	d1 := now.Add(d)
    	fmt.Println("d1: ", d1)
    
    	printSplit(50)
    
    	// 10分钟后
    	mm, _ := time.ParseDuration("10m")
    	mm1 := now.Add(mm)
    	fmt.Println("mm1: ", mm1)
    
    	// 8小时后
    	hh, _ := time.ParseDuration("1h")
    	hh1 := now.Add(hh * 8)
    	fmt.Println("hh1: ", hh1)
    
    	// 一天后
    	dd, _ := time.ParseDuration("24h")
    	dd1 := now.Add(dd)
    	fmt.Println("dd1: ", dd1)
    
    	printSplit(50)
    
    	// Sub 计算两个时间差
    	subM := now.Sub(m1)
    	fmt.Println(subM.Minutes(), "分钟")
    
    	sumH := now.Sub(h1)
    	fmt.Println(sumH.Hours(), "小时")
    
    	sumD := now.Sub(d1)
    	fmt.Printf("%v 天
    ", sumD.Hours()/24)
    
    }
    
    func printSplit(count int) {
    	fmt.Println(strings.Repeat("#", count))
    }
    

      

    时区换算

    package main
    
    import (
    	"fmt"
    	"time"
    )
    
    func main() {
    	// time.Local 用来表示当前服务器时区
    	// 自定义地区时间
    	secondsEastOfUTC := int((8 * time.Hour).Seconds())
    	beijing := time.FixedZone("Beijing Time", secondsEastOfUTC)
    	fmt.Println(time.Date(2018, 1, 2, 0, 0, 0, 0, beijing)) // 2018-01-02 00:00:00 +0800 Beijing Time
    
    	// 当前时间转为指定时区时间
    	fmt.Println(time.Now().In(beijing))
    
    	// 指定时间转换成指定时区对应的时间
    	dt, err := time.ParseInLocation("2006-01-02 15:04:05", "2017-05-11 14:06:06", beijing)
    	if err != nil {
    		fmt.Println(err)
    	}
    	fmt.Println(dt)
    
    	// 当前时间在零时区年月日   时分秒  时区
    	year, mon, day := time.Now().UTC().Date() // 2018 April 24
    	fmt.Println(year, mon, day)
    
    	hour, min, sec := time.Now().UTC().Clock() // 3 47 15
    	fmt.Println(hour, min, sec)
    
    	zone, _ := time.Now().UTC().Zone() // UTC
    	fmt.Println(zone)
    
    }
    

      

    比较两个时间点

    dt := time.Date(2018, 1, 10, 0, 0, 1, 100, time.Local)
    fmt.Println(time.Now().After(dt)) // true
    fmt.Println(time.Now().Before(dt)) // false
    
    // 是否相等 判断两个时间点是否相等时推荐使用 Equal 函数
    fmt.Println(dt.Equal(time.Now()))
    

      


    设置执行时间
    通过time.After 函数与 select 结合使用可用于处理程序超时设定

    select {
    case m := <- c:
    // do something
    case <- time.After(time.Duration(1)*time.Second):
    fmt.Println("time out")
    }
    

      



    Ticker类型
    Ticker 类型包含一个 channel,有时我们会遇到每隔一段时间执行的业务(比如设置心跳时间等),就可以用它来处理,这是一个重复的过程

    // 无法取消
    tick := time.Tick(1 * time.Minute)
    for _ = range tick {
    // do something
    }
    
    // 可通过调用ticker.Stop取消
    ticker := time.NewTicker(1 * time.Minute)
    for _ = range tick {
    // do something
    }
    

      


    Timer类型
    Timer 类型用来代表一个单独的事件,当设置的时间过期后,发送当前的时间到 channel, 我们可以通过以下两种方式来创建

    func AfterFunc(d Duration, f func()) *Timer // 指定一段时间后指定的函数
    func NewTimer(d Duration) *Timer 
    

      

    以上两函数都可以使用 Reset, 这个有个需要注意的地方是使用 Reset 时需要确保 t.C 通道被释放时才能调用,以防止发生资源竞争的问题,可通过以下方式解决

    if !t.Stop() {
      <-t.C
    }
    t.Reset(d)
    

      

    参考文献

    package time

    golang积累-时间、时区、格式的使用

    论golang Timer Reset方法使用的正确姿势



  • 相关阅读:
    是否可以在tomcat servlet中禁用jsessionid?
    一个屌丝程序猿的人生(一百二十一)
    postman 使用
    【Ubuntu】命令行安装deb安装包
    opencv的cascade分类器源码分析
    Face Detection – OpenCV, Dlib and Deep Learning ( C++ / Python )
    小样本目标检测研究现状
    图像特征提取三大法宝:HOG特征,LBP特征,Haar特征
    搞清楚nand flash和 nor flash 以及 spi flash 和cfi flash 的区别
    xhsell远程vmware ubuntu16.04
  • 原文地址:https://www.cnblogs.com/saryli/p/11656711.html
Copyright © 2011-2022 走看看