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 天\n", 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方法使用的正确姿势

golang ---timeb的更多相关文章

  1. Golang, 以17个简短代码片段,切底弄懂 channel 基础

    (原创出处为本博客:http://www.cnblogs.com/linguanh/) 前序: 因为打算自己搞个基于Golang的IM服务器,所以复习了下之前一直没怎么使用的协程.管道等高并发编程知识 ...

  2. 说说Golang的使用心得

    13年上半年接触了Golang,对Golang十分喜爱.现在是2015年,离春节还有几天,从开始学习到现在的一年半时间里,前前后后也用Golang写了些代码,其中包括业余时间的,也有产品项目中的.一直 ...

  3. TODO:Golang指针使用注意事项

    TODO:Golang指针使用注意事项 先来看简单的例子1: 输出: 1 1 例子2: 输出: 1 3 例子1是使用值传递,Add方法不会做任何改变:例子2是使用指针传递,会改变地址,从而改变地址. ...

  4. Golang 编写的图片压缩程序,质量、尺寸压缩,批量、单张压缩

    目录: 前序 效果图 简介 全部代码 前序: 接触 golang 不久,一直是边学边做,边总结,深深感到这门语言的魅力,等下要跟大家分享是最近项目 服务端 用到的图片压缩程序,我单独分离了出来,做成了 ...

  5. golang struct扩展函数参数命名警告

    今天在使用VSCode编写golang代码时,定义一个struct,扩展几个方法,如下: package storage import ( "fmt" "github.c ...

  6. golang语言构造函数

    1.构造函数定义 构造函数 ,是一种特殊的方法.主要用来在创建对象时初始化对象, 即为对象成员变量赋初始值,总与new运算符一起使用在创建对象的语句中.特别的一个类可以有多个构造函数 ,可根据其参数个 ...

  7. TODO:Golang语言TCP/UDP协议重用地址端口

    TODO:Golang语言TCP/UDP协议重用地址端口 这是一个简单的包来解决重用地址的问题. go net包(据我所知)不允许设置套接字选项. 这在尝试进行TCP NAT时尤其成问题,其需要在同一 ...

  8. golang的安装

    整理了一下,网上关于golang的安装有三种方式(注明一下,我的环境为CentOS-6.x, 64bit) 方式一:yum安装(最简单) rpm -Uvh http://dl.fedoraprojec ...

  9. golang枚举类型 - iota用法拾遗

    在c#.java等高级语言中,经常会用到枚举类型来表示状态等.在golang中并没有枚举类型,如何实现枚举呢?首先从枚举的概念入手. 1.枚举类型定义 从百度百科查询解释如下:http://baike ...

随机推荐

  1. vue中利用Promise封装jsonp并调取数据

    Promise就是一个给一步操作提供的容器,在这个容器里,有两个阶段无法改变的阶段,第一个阶段就是Pending(进行),第二个阶段就是结果阶段,包含Fulfilled(成功).Rejected(失败 ...

  2. 奖金高达150万元的NEST即将上线,速来报名

    表哥.表姐们快粗来~~~分享一个劲爆消息: 年度奖金总额高达150万元的众测平台(NEST)已于2019年6月10日启动全球报名,6月26号开始首批线上众测,欢迎精英白帽们前来挑战! 更多详情请认真阅 ...

  3. NBIOT实现UDP协议的发送和接收(包含软件升级)

    源码下载: nbiot_module程序(java netbean) -> 提取码 UdpServer程序(C# vs2010) -> 提取码 QQ:505645074 前提条件:开NB卡 ...

  4. python使用ftplib模块实现FTP文件的上传下载

    python已经默认安装了ftplib模块,用其中的FTP类可以实现FTP文件的上传下载 FTP文件上传下载 # coding:utf8 from ftplib import FTP def uplo ...

  5. pip安装模块使用国内镜像源加速安装

    今天在安装Python模块matplotlib的时候,一直安装不成功,老是提示“socket.timeout: The read operation timed out”或者“Read timed o ...

  6. JS高阶---回调函数

    1.什么函数是回调函数? 此时两者的执行并没有先后顺序 两个都是回调函数 满足三个特点就是回调 .定义了函数 .没有主动调用 .最后执行了 2.常见的回调函数有哪些? .DOM事件回调函数 .定时器回 ...

  7. 201871010132--张潇潇--《面向对象程序设计(java)》第十二周学习总结

    博文正文开头格式:(2分) 项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cnblogs.co ...

  8. shell之seq

    seq 用于生成从一个数到另一个数之间的所有整数 seq [选项]... 尾数 seq [选项]... 首数 尾数 seq [选项]... 首数 增量 尾数 例如: 1.  -s 指定分隔符,默认分隔 ...

  9. poi基本使用

    poi基本使用 依赖 <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi< ...

  10. DRL强化学习:

    IT博客网 热点推荐 推荐博客 编程语言 数据库 前端 IT博客网 > 域名隐私保护 免费 DRL前沿之:Hierarchical Deep Reinforcement Learning 来源: ...