先了解下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. 牛客练习赛29 B

    炎热的早上,gal男神们被迫再操场上列队,gal男神们本来想排列成x∗x的正方形,可是因为操场太小了(也可能是gal男神太大了),校长安排gal男神们站成多个4∗4的正方形(gal男神们可以正好分成n ...

  2. PAT Basic 1082

    1082 射击比赛 本题目给出的射击比赛的规则非常简单,谁打的弹洞距离靶心最近,谁就是冠军:谁差得最远,谁就是菜鸟.本题给出一系列弹洞的平面坐标(x,y),请你编写程序找出冠军和菜鸟.我们假设靶心在原 ...

  3. Linux操作系统启动流程

    一般来说,所有的操作系统的启动流程基本就是: 总的来说,linux系统启动流程可以简单总结为以下几步:1)开机BIOS自检,加载硬盘.2)读取MBR,进行MBR引导.3)grub引导菜单(Boot L ...

  4. webservice soap wsdl简介

    先给出一个概念 SOA ,即Service Oriented Architecture ,中文一般理解为面向服务的架构, 既然说是一种架构的话,所以一般认为 SOA 是包含了运行环境,编程模型, 架构 ...

  5. luogu3389 【模板】高斯消元法

    #include <algorithm> #include <iostream> #include <cstdio> #include <cmath> ...

  6. html 标签附加文本属性

    <!DOCTYPE html> <html> <head> <script> function showDetails(animal) { var an ...

  7. C语言知识点(4)

    一.while.    dowhile. 1.while while (表达式) { 语句: … 语句: } 2.while do { printf(“%d/n,I);…}while (i<=1 ...

  8. php hash防止表单

    <?php /** * Created by PhpStorm. * User: brady * Desc: * Date: 2017/7/12 * Time: 15:01 */class te ...

  9. 飞行员配对方案问题(匈牙利算法+sort)

    洛谷传送门 匈牙利算法+sort 没什么好说的. ——代码 #include <cstdio> #include <cstring> #include <algorith ...

  10. HDU 5402 : Travelling Salesman Problem

    题目大意:n*m的格子,从左上角走到右下角,每个格子只能走一遍,每个格子上有一个非负数,要让途径的数字和最大,最后要输出路径 思路:显然茹果n,m有一个是奇数的话所有格子的数字都能被我吃到,如果都是偶 ...