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 天\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)
参考文献
golang ---timeb的更多相关文章
- Golang, 以17个简短代码片段,切底弄懂 channel 基础
(原创出处为本博客:http://www.cnblogs.com/linguanh/) 前序: 因为打算自己搞个基于Golang的IM服务器,所以复习了下之前一直没怎么使用的协程.管道等高并发编程知识 ...
- 说说Golang的使用心得
13年上半年接触了Golang,对Golang十分喜爱.现在是2015年,离春节还有几天,从开始学习到现在的一年半时间里,前前后后也用Golang写了些代码,其中包括业余时间的,也有产品项目中的.一直 ...
- TODO:Golang指针使用注意事项
TODO:Golang指针使用注意事项 先来看简单的例子1: 输出: 1 1 例子2: 输出: 1 3 例子1是使用值传递,Add方法不会做任何改变:例子2是使用指针传递,会改变地址,从而改变地址. ...
- Golang 编写的图片压缩程序,质量、尺寸压缩,批量、单张压缩
目录: 前序 效果图 简介 全部代码 前序: 接触 golang 不久,一直是边学边做,边总结,深深感到这门语言的魅力,等下要跟大家分享是最近项目 服务端 用到的图片压缩程序,我单独分离了出来,做成了 ...
- golang struct扩展函数参数命名警告
今天在使用VSCode编写golang代码时,定义一个struct,扩展几个方法,如下: package storage import ( "fmt" "github.c ...
- golang语言构造函数
1.构造函数定义 构造函数 ,是一种特殊的方法.主要用来在创建对象时初始化对象, 即为对象成员变量赋初始值,总与new运算符一起使用在创建对象的语句中.特别的一个类可以有多个构造函数 ,可根据其参数个 ...
- TODO:Golang语言TCP/UDP协议重用地址端口
TODO:Golang语言TCP/UDP协议重用地址端口 这是一个简单的包来解决重用地址的问题. go net包(据我所知)不允许设置套接字选项. 这在尝试进行TCP NAT时尤其成问题,其需要在同一 ...
- golang的安装
整理了一下,网上关于golang的安装有三种方式(注明一下,我的环境为CentOS-6.x, 64bit) 方式一:yum安装(最简单) rpm -Uvh http://dl.fedoraprojec ...
- golang枚举类型 - iota用法拾遗
在c#.java等高级语言中,经常会用到枚举类型来表示状态等.在golang中并没有枚举类型,如何实现枚举呢?首先从枚举的概念入手. 1.枚举类型定义 从百度百科查询解释如下:http://baike ...
随机推荐
- React入门(二)
组件的生命周期 概念:在组件创建.到加载到页面运行.以及组件被销毁的过程中伴随的事件.组件的生命周期是指在组件的特定时期触发的事件. 组件的生命周期分为三个部分: 组件创建阶段:只执行一次 compo ...
- PHP开发之--安装composer
composer是一个很好用的工具,类似于iOS开发项目中所用的cocopods,所以就想着安装下,过程也很简单: 1.切换到如下目录(在很多博客中都是这样写的,先安装,然后mv到/usr/local ...
- 七、union联合
Union:联合 作用:把2次或多次查询结果合并起来 案例(认识union):我想把商品价格大于5000元的和商品价格小于20元的商品都取出来 1.Select * from goods where ...
- conan使用(一)--安装和应用
1. 安装conan 使用pip命令安装conan,如果没有安装pip的,可以先安装好python后,再安装pip. pip install conan 之后就可以使用conan命令. 2. 管理远程 ...
- Subversion简介(一)
Subversion 就是一款实现版本控制的工具软件,通常也称为版本控制器,简称 SVN. Subversion 是 Apache 软件基金会组织下的一个项目. SVN 的工作原理:采取客户端/服务器 ...
- (day48作业)jQuery+Bootstrap练习题
目录 一.图书管理系统页面搭建 二.jQuery练习题 一.图书管理系统页面搭建 <!DOCTYPE html> <html lang="en"> < ...
- 前端之CSS(下)
文本属性 ## 文字对齐 #text-align属性规定元素中文本的水平对齐方式 left: 左边对齐 默认值 right: 右对齐 center: 居中对齐 justify: 两端对齐 ## 文字装 ...
- 使用表格 代替 txt文件(未完结)
使用xlrd读取用户数据 首先创建一个xls文件 xlrd模块的简单使用 添加处理execl的接口: 使用excel生成测试报表
- onchange onpropertychange 和 oninput 事件的区别
onchange 事件在内容改变(两次内容有可能还是相等的)且失去焦点时触发. onpropertychange 事件却是实时触发,即每增加或删除一个字符就会触发,通过 js 改变也会触发该事件,但是 ...
- Spring Cloud微服务安全实战_4-3_订单微服务&价格微服务
实现一个场景: 订单微服务: POM: <?xml version="1.0" encoding="UTF-8"?> <project xml ...