随笔:Golang 时间Time
先了解下time类型:
type Time struct {
// sec gives the number of seconds elapsed since
// January 1, year 1 00:00:00 UTC.
sec int64
// nsec specifies a non-negative nanosecond
// offset within the second named by Seconds.
// It must be in the range [0, 999999999].
nsec int32
// loc specifies the Location that should be used to
// determine the minute, hour, month, day, and year
// that correspond to this Time.
// Only the zero Time has a nil Location.
// In that case it is interpreted to mean UTC.
loc *Location
}
对于time.Time类型,我们可以通过使用函数Before,After,Equal来比较两个time.Time时间:
t1 := time.Now()
t2 := t1.Add(-1 * time.Minute)
fmt.Println(t1.Before(t2))
上面t1是当前时间,t2是当前时间的前一分钟,输出结果:false
对于两个time.Time时间是否相等,也可以直接使用==来判断:
t1 := time.Now()
t2 := t1.Add(-1 * time.Minute)
fmt.Println(t1 == t2)
我们可以通过IsZero来判断时间Time的零值
t1.IsZero()
我们常常会将time.Time格式化为字符串形式:
t := time.Now()
str_t := t.Format("2006-01-02 15:04:05")
至于格式化的格式为什么是这样的,已经被很多人吐槽了,但是,这是固定写法,没什么好说的。
那么如何将字符串格式的时间转换成time.Time呢?下面两个函数会比较常用到:
A)
t, _ := time.Parse("2006-01-02 15:04:05", "2017-04-25 09:14:00")
这里我们发现,t:=time.Now()返回的时间是time.Time,上面这行代码也是time.Time但是打印出来的结果:
2017-04-25 16:15:11.235733 +0800 CST
2016-06-13 09:14:00 +0000 UTC
为什么会不一样呢?原因是 time.Now() 的时区是 time.Local,而 time.Parse 解析出来的时区却是 time.UTC(可以通过 Time.Location() 函数知道是哪个时区)。在中国,它们相差 8 小时。
所以,一般的,我们应该总是使用 time.ParseInLocation 来解析时间,并给第三个参数传递 time.Local:
t, _ := time.ParseInLocation("2006-01-02 15:04:05", "2017-04-25 16:14:00", time.Local)
之前项目中遇到要获取某个时间之前的最近整点时间,这个时候有几种办法:
t, _ := time.ParseInLocation("2006-01-02 15:04:05", time.Now().Format("2006-01-02 15:00:00"), time.Local)
t, _ := time.ParseInLocation("2006-01-02 15:04:05", "2016-06-13 15:34:39", time.Local)
t0:=t.Truncate(1 * time.Hour)
随笔:Golang 时间Time的更多相关文章
- Golang时间格式化
PHP中格式化时间很方便,只需要一个函数就搞定: date("Y-m-d H:i:s") 而在Golang中,用的是"2006-01-02 15:04:05"这 ...
- Golang时间函数及测试函数执行时间案例
package main import ( "fmt" "time" ) func main(){ //[时间获取及格式化] //获取当前时间 now_time ...
- 三、golang时间、流程控、函数
一.本篇内容 1.string和strconv使用 2.go中的时间和日期类型 3.流程控制 4.函数讲解 二.string和strconv使用 1. string.HasPrefix(s trin ...
- golang 时间转换的问题
一般在获取到时间字符串,需要将时间字符串格式化为golang的"time.Time"对象的时候,通常有2个函数,分别是. time.Parse(layout, value stri ...
- cgo 随笔(golang)
结构体应用 //结构体定义如下 // test.h struct test { int a; int b; int c; } 在golang中的调用如下: package name import &q ...
- 工作随笔——Golang interface 转换成其他类型
新的公司,新的氛围.一年了,打算写点什么.so,那就写google的golang语言吧. 最最最基础的语法结构见go语言菜鸟教程 接下来写点菜鸟教程没有的. go语言的设计者认为:go语言必须让程序员 ...
- golang时间转换
1.datetime转换成时间字符串 package main import ( "fmt" "reflect" "time" ) func ...
- 2017年1月4日 16:16:24开始学习Linux——好像回到上次发随笔的时间。
auto为C语言局部变量的默认属性 static指明变量的静态属性,也具有作用域限定符的意义 static修饰的全局变量作用域只是生命的文件中,修饰的函数作用域只是声明的文件中 register指明将 ...
- golang时间
//获取本地location toBeCharge := "2015-01-01 00:00:00" //待转化为时间戳的字符串 注意 这里的小时和分钟还要秒必须写 因为是跟着模板 ...
随机推荐
- bash循环for/while/until
shell流程控制之一:for循环 for VAR in LIST; do STATEMENT1 ... done 例: ...
- 如何将Python对象保存在本地文件中?
Python对象的永久存储 1.使用Python的pickle模块 import pickle class A: def __init__(self,name,a): self.name=name s ...
- php msql 表单
http://www.cnblogs.com/webers/p/3849707.html
- 网络编程基础socket 重要中:TCP/UDP/七层协议
计算机网络的发展及基础网络概念 问题:网络到底是什么?计算机之间是如何通信的? 早期 : 联机 以太网 : 局域网与交换机 广播 主机之间“一对所有”的通讯模式,网络对其中每一台主机发出的信号都进行无 ...
- mac常用软件,自用找了很久的分享一下相信很多人需要
CleanMyMac 3.1.1.dmg比较好用的清理软件.破解版!http://pan.baidu.com/s/1i4mo7jvNTFS读写 Tuxera NTFS for Mac.rar也是破解的 ...
- KVO And KVC
http://www.cocoachina.com/industry/20140224/7866.html
- 软件工程师应该关注的web加密手段
加密算法 1.非对称加密(公开密钥加密) 公开密钥加密,是加密和解密使用不同密钥的算法,广泛用于信息传输中. 常见的算法有:RSA.ElGamal.Rabin. 2.对称加密 ...
- mvc “System.NullReferenceException”类型的异常在 App_Web_zo44wdaq.dll 中发生,但未在用户代码中进行处理 其他信息: 未将对象引用设置到对象的实例。
“System.NullReferenceException”类型的异常在 App_Web_zo44wdaq.dll 中发生,但未在用户代码中进行处理 其他信息: 未将对象引用设置到对象的实例. 解决 ...
- matlab 初级画图
matlab 初级画图 1.plot() plot(x,y) plots each vector pairs (x,y) 画图函数画出每个点 每组变量 plot (y) plots eac ...
- DB2 和 有道词典冲突: A communication error has been detected. Communication protocol being used: Reply.fill().
我在本机安装了DB2 9.5. 使用java jdbc连接,一直没有问题. QC for db2 连接 也一直没有问题. 突然有一天 Java程序连接 报错: A communication erro ...