先了解下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的更多相关文章

  1. Golang时间格式化

    PHP中格式化时间很方便,只需要一个函数就搞定: date("Y-m-d H:i:s") 而在Golang中,用的是"2006-01-02 15:04:05"这 ...

  2. Golang时间函数及测试函数执行时间案例

    package main import ( "fmt" "time" ) func main(){ //[时间获取及格式化] //获取当前时间 now_time ...

  3. 三、golang时间、流程控、函数

    一.本篇内容 1.string和strconv使用 2.go中的时间和日期类型 3.流程控制 4.函数讲解 二.string和strconv使用 1.  string.HasPrefix(s trin ...

  4. golang 时间转换的问题

    一般在获取到时间字符串,需要将时间字符串格式化为golang的"time.Time"对象的时候,通常有2个函数,分别是. time.Parse(layout, value stri ...

  5. cgo 随笔(golang)

    结构体应用 //结构体定义如下 // test.h struct test { int a; int b; int c; } 在golang中的调用如下: package name import &q ...

  6. 工作随笔——Golang interface 转换成其他类型

    新的公司,新的氛围.一年了,打算写点什么.so,那就写google的golang语言吧. 最最最基础的语法结构见go语言菜鸟教程 接下来写点菜鸟教程没有的. go语言的设计者认为:go语言必须让程序员 ...

  7. golang时间转换

    1.datetime转换成时间字符串 package main import ( "fmt" "reflect" "time" ) func ...

  8. 2017年1月4日 16:16:24开始学习Linux——好像回到上次发随笔的时间。

    auto为C语言局部变量的默认属性 static指明变量的静态属性,也具有作用域限定符的意义 static修饰的全局变量作用域只是生命的文件中,修饰的函数作用域只是声明的文件中 register指明将 ...

  9. golang时间

    //获取本地location toBeCharge := "2015-01-01 00:00:00" //待转化为时间戳的字符串 注意 这里的小时和分钟还要秒必须写 因为是跟着模板 ...

随机推荐

  1. poj 3614 伪素数问题

    题意:1.p不是素数 2.(a^p)%p=a 输出yes  不满足输出no 思路: 判断素数问题,直接暴力判断 bool is_prime(int n) {  for(int i=2;i*i<= ...

  2. 关于51单片机IO引脚的驱动能力与上拉电阻

    单片机的引脚,可以用程序来控制,输出高.低电平,这些可算是单片机的输出电压.但是,程序控制不了单片机的输出电流. 单片机的输出电流,很大程度上是取决于引脚上的外接器件. 单片机输出低电平时,将允许外部 ...

  3. HDU 5399 数学 Too Simple

    题意:有m个1~n的映射,而且对于任意的 i 满足 f1(f2(...fm(i))) = i 其中有些映射是知道的,有些是不知道的,问一共有多少种置换的组合. 分析: 首先这些置换一定是1~n的一个置 ...

  4. Python虚拟机框架

    Python字节码 我们知道,Python源代码在执行前,会先将源代码编译为字节码序列,Python虚拟机就根据这些字节码进行一系列的操作,从而完成对Python程序的执行.在Python2.5中,一 ...

  5. 【USACO09FEB】 庙会班车 Fair Shuttle 贪心+线段树

    Although Farmer John has no problems walking around the fair to collect prizes or see the shows, his ...

  6. Tinkoff Internship Warmup Round 2018 and Codeforces Round #475 (Div. 2)

    A. Splits time limit per test 1 second memory limit per test 256 megabytes input standard input outp ...

  7. springboot添加外部jar包及打包

    项目中除了从pom中添加依赖包,还可以添加本地的jar包,怎么做呢? 1.在src下新建目录lib,将jar包添加到lib中 2.在pom文件里添加配置以下属性,就可以使用jar包了 <depe ...

  8. 九度oj 题目1187:最小年龄的3个职工

    题目描述: 职工有职工号,姓名,年龄.输入n个职工的信息,找出3个年龄最小的职工打印出来. 输入: 输入第一行包括1个整数N,1<=N<=30,代表输入数据的个数. 接下来的N行有N个职工 ...

  9. 刷题总结——game(hdu4616)

    题目: Nowadays, there are more and more challenge game on TV such as 'Girls, Rush Ahead'. Now, you par ...

  10. Hadoop 3.1.0 在 Ubuntu 16.04 上安装时遇到的问题

    1.Hadoop 安装 pdsh localhost: Connection refused Hadoop安装过程中使用 $ sbin/start-dfs.sh 启动节点时,发生错误提示: pdsh@ ...