网络编程
位置:首页>> 网络编程>> Go语言>> golang time包的用法详解

golang time包的用法详解

作者:飘ing  发布时间:2024-04-25 13:19:19 

标签:golang,time包

在我们编程过程中,经常会用到与时间相关的各种务需求,下面来介绍 golang 中有关时间的一些基本用法,我们从 time 的几种 type 来开始介绍。

时间可分为时间点与时间段,golang 也不例外,提供了以下两种基础类型
- 时间点(Time)
- 时间段(Duration)

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

我们将按以上顺序来介绍 time 包的使用。

时间点(Time)

我们使用的所有与时间相关的业务都是基于点而延伸的,两点组成一个时间段,大多数应用也都是围绕这些点与面去做逻辑处理。

初始化

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


  // 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

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

格式化

to string

格式化为字符串我们需要使用 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,刚开始使用时需要注意。

to time stamp


  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())

其他

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)

我们在来介绍一下时区的相关的函数


 // 默认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))

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

时间运算

来源:https://blog.csdn.net/wschq/article/details/80114036

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com