了解时间常量
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”
)
常用方法:
time.Now(). Unix ()
//当前时间戳
time.Now().UnixNano()
//精确到 纳秒
time.Now(). Format (“2006-01-02 15:04:05”)
//当前时间格式化 记忆方法:6-1-2-3-4-5
time.Unix(1389058332, 0).Format(“2006-01-02 15:04:05”)
//把时间戳格式化,func Unix(sec int64, nsec int64) Time {}当前时间戳和纳秒时间戳
time.Parse(time.RFC3339Nano, str)
//str格式化时间转时间戳,RFC3339Nano就是(“2006-01-02 15:04:05”)
t := time.Now().Add(-1 * time. Minute )
//当前时间的前1分钟
t1.Before(t2)
t1 == t2
//判断时间
t := time.Unix(1469579899, 0)
t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second()
//获取时间戳指定信息
time.Sleep(100 * time.Millisecond)
//休眠时间
函数介绍
time.Duration(时间长度,消耗时间)
time.Time(时间点)
time.C(放时间的 channel 通道)
func After(d Duration) <-chan Time
chan := time.After(time.Secone*1)
示例:
select {
case m := <-c:
handle(m)
case <-time.After(5 * time.Minute): //超时
fmt.Println(“timed out”)
}
func Tick(d Duration) <-chan Time
//它是表示每隔多少时间之后,是一个重复的过程
m, _ := time.ParseDuration(“-1m”)
m1 := now.Add(m)
//时间计算,1分钟前
subM := now.Sub(m1)
fmt.Println(subM.Minutes(), “分钟”)
//时间差换算
func Date(year int, month Month, day, hour, min, sec, nsec int, loc *Location) Time {}
// 返回指定时间
t_by_date := time.Date(2017, time.Month(2), 23, 1, 30, 30, 0, l)
小结:
golang日常开发用的time处理本篇都介绍了,其他类型转换可自行查看官网介绍。
更多精彩内容敬请关注每日编程,每天进步一点。