您所在的位置:首页 / 行业动态

Go 每日一练之关于时间操作。

2020.08.11

2306

在Go里操作时间有很多独特的地方,下面我们来看看有什么独特地方。

我们在程序里说时间时,一定是包括了时间值与时区的。另外,时区的表示方式也有多种:

UTC时间:世界协调时间(UTC)是世界上不同国家用来调节时钟和时间的主要时间标准。

               :也就是零时区的时间

CST时间:中央标准时间
            Central Standard Time (USA) UT-6:00(美国cst时间:零区时减6个小时)

            Central Standard Time (Australia) UT+9:30(澳大利亚cst:加9个半小时)

            China Standard Time UT+8:00(中国cst:加8个小时)

            Cuba Standard Time UT-4:00  (古巴cst:减4个小时)


go 针对不同的参数类型提供了以下初始化的方式

    time := time.Now()
    // 默认UTC    
    loc, err := time.LoadLocation("") 
    // 一般为CST
    loc, err := time.LoadLocation("Local")
    // 美国洛杉矶PDT
    loc, err := time.LoadLocation("America/Los_Angeles")
    // CST
    loc, _:= time.LoadLocation("Asia/Chongqing") 

      // func Now() Time
      fmt.Println(time.Now())

      // func Parse(layout, value string) (Time, error)
      time.Parse("2016-01-02 15:04:05", "2018-04-23 12:24:51")

      // func ParseInLocation(layout, value string, loc *Location) (Time, error) (layout已带时区时可直接用Parse)
      time.ParseInLocation("2006-01-02 15:04:05", "2017-05-11 14:06:06", time.Local)

      // func Unix(sec int64, nsec int64) Time
      time.Unix(1e9, 0)

      // func Date(year int, month Month, day, hour, min, sec, nsec int, loc *Location) Time
      time.Date(2018, 1, 2, 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))

      // func (t Time) Local() Time


格式化



格式化为字符串我们需要使用 time.Format 方法来转换成我们想要的格式
fmt.Println(time.Now().Format("2006-01-02 15:04:05"))  // 2018-04-24 10:11:20
fmt.Println(time.Now().Format(time.UnixDate))         // Tue Apr 24 09:59:02 CST 2018
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,刚开始使用时需要注意。
时间戳

      func (t Time) Unix() int64       func (t Time) UnixNano() int64       fmt.Println(time.Now().Unix())       // 获取指定日期的时间戳       dt, _ := time.Parse("2016-01-02 15:04:05", "2018-04-23 12:24:51")       fmt.Println(dt.Unix())       fmt.Println(time.Date(2018, 1,2,15,30,10,0, time.Local).Unix())


时间段(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)
    // 默认UTC    
    loc, err := time.LoadLocation("") 
    // 服务器设定的时区,一般为CST
    loc, err := time.LoadLocation("Local")
    // 美国洛杉矶PDT
    loc, err := time.LoadLocation("America/Los_Angeles")

    // 获取指定时区的时间点
    local, _ := time.LoadLocation("America/Los_Angeles")
    fmt.Println(time.Date(2018,1,1,12,0,0,0, local))
时间运算
      // 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 两个时间点的间隔
      start := time.Now()
      fmt.Println(time.Since(start))   // 等价于 Now().Sub(t), 可用来计算一段业务的消耗时间

      func Until(t Time) Duration     //  等价于 t.Sub(Now()),t与当前时间的间隔

      // func (t Time) Add(d Duration) Time
      fmt.Println(dt.Add(time.Duration(10) * time.Second))   // 加

      func (t Time) Sub(u Time) Duration                    // 减 

      // func (t Time) AddDate(years int, months int, days int) Time
      fmt.Println(dt.AddDate(1, 1, 1))

      // func (t Time) Before(u Time) bool
      // func (t Time) After(u Time) bool
      // func (t Time) Equal(u Time) bool          比较时间点时尽量使用Equal函数 



相关新闻

“成都社保”微信公众平台上线

2016.07.18

3481

“成都社保”微信公众平台正式上线运行,是“互联网+社保”行动的又一创新成果,标志着成都社保经办管理服务现代治理体系建设迈出坚实步伐。