golang中的时间和时区

栏目: Go · 发布时间: 8年前

内容简介:先写一段测试代码:输出:从以上代码的测试结果可以得出几点:

先写一段测试代码:

const TIME_LAYOUT = "2006-01-02 15:04:05"

func parseWithLocation(name string, timeStr string) (time.Time, error) {
    locationName := name
    if l, err := time.LoadLocation(locationName); err != nil {
        println(err.Error())
        return time.Time{}, err
    } else {
        lt, _ := time.ParseInLocation(TIME_LAYOUT, timeStr, l)
        fmt.Println(locationName, lt)
        return lt, nil
    }
}
func testTime() {
    fmt.Println("0. now: ", time.Now())
    str := "2018-09-10 00:00:00"
    fmt.Println("1. str: ", str)
    t, _ := time.Parse(TIME_LAYOUT, str)
    fmt.Println("2. Parse time: ", t)
    tStr := t.Format(TIME_LAYOUT)
    fmt.Println("3. Format time str: ", tStr)
    name, offset := t.Zone()
    name2, offset2 := t.Local().Zone()
    fmt.Printf("4. Zone name: %v, Zone offset: %v\n", name, offset)
    fmt.Printf("5. Local Zone name: %v, Local Zone offset: %v\n", name2, offset2)
    tLocal := t.Local()
    tUTC := t.UTC()
    fmt.Printf("6. t: %v, Local: %v, UTC: %v\n", t, tLocal, tUTC)
    fmt.Printf("7. t: %v, Local: %v, UTC: %v\n", t.Format(TIME_LAYOUT), tLocal.Format(TIME_LAYOUT), tUTC.Format(TIME_LAYOUT))
    fmt.Printf("8. Local.Unix: %v, UTC.Unix: %v\n", tLocal.Unix(), tUTC.Unix())
    str2 := "1969-12-31 23:59:59"
    t2, _ := time.Parse(TIME_LAYOUT, str2)
    fmt.Printf("9. str2:%v,time: %v, Unix: %v\n", str2, t2, t2.Unix())
    fmt.Printf("10. %v, %v\n", tLocal.Format(time.ANSIC), tUTC.Format(time.ANSIC))
    fmt.Printf("11. %v, %v\n", tLocal.Format(time.RFC822), tUTC.Format(time.RFC822))
    fmt.Printf("12. %v, %v\n", tLocal.Format(time.RFC822Z), tUTC.Format(time.RFC822Z))

    //指定时区
    parseWithLocation("America/Cordoba", str)
    parseWithLocation("Asia/Shanghai", str)
    parseWithLocation("Asia/Beijing", str)
}
testTime()

输出:

0. now:  2018-09-19 19:06:07.3642781 +0800 CST m=+0.005995601
1. str:  2018-09-10 00:00:00
2. Parse time:  2018-09-10 00:00:00 +0000 UTC
3. Format time str:  2018-09-10 00:00:00
4. Zone name: UTC, Zone offset: 0
5. Local Zone name: CST, Local Zone offset: 28800
6. t: 2018-09-10 00:00:00 +0000 UTC, Local: 2018-09-10 08:00:00 +0800 CST, UTC: 2018-09-10 00:00:00 +0000 UTC
7. t: 2018-09-10 00:00:00, Local: 2018-09-10 08:00:00, UTC: 2018-09-10 00:00:00
8. Local.Unix: 1536537600, UTC.Unix: 1536537600
9. str2:1969-12-31 23:59:59,time: 1969-12-31 23:59:59 +0000 UTC, Unix: -1
10. Mon Sep 10 08:00:00 2018, Mon Sep 10 00:00:00 2018
11. 10 Sep 18 08:00 CST, 10 Sep 18 00:00 UTC
12. 10 Sep 18 08:00 +0800, 10 Sep 18 00:00 +0000
America/Cordoba 2018-09-10 00:00:00 -0300 -03
Asia/Shanghai 2018-09-10 00:00:00 +0800 CST
cannot find Asia/Beijing in zip file C:\Go\/lib/time/zoneinfo.zip

从以上代码的测试结果可以得出几点:

  1. time.Now 得到的当前时间的时区跟电脑的当前时区一样。
  2. time.Parse 把时间字符串转换为Time,时区是UTC时区。
  3. 不管Time变量存储的是什么时区,其 Unix() 方法返回的都是距离UTC时间:1970年1月1日0点0分0秒的秒数。
  4. Unix() 返回的秒数可以是负数,如果时间小于1970-01-01 00:00:00的话。
  5. Zone 方法可以获得变量的时区和时区与UTC的偏移秒数,应该支持夏令时和冬令时。
  6. time.LoadLocation 可以根据时区名创建时区 Location ,所有的时区名字可以在 $GOROOT/lib/time/zoneinfo.zip 文件中找到,解压 zoneinfo.zip 可以得到一堆目录和文件,我们只需要目录和文件的名字,时区名是目录名+文件名,比如 "Asia/Shanghai" 。中国时区名只有 "Asia/Shanghai" 和 "Asia/Chongqing" ,而没有 "Asia/Beijing" 。
  7. time.ParseInLocation 可以根据时间字符串和指定时区转换Time。
  8. 感谢中国只有一个时区而且没有夏令时和冬令时,可怕的美国居然有6个时区,想想都可怕。

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。

Writing Windows VxDs and Device Drivers, Second Edition

Writing Windows VxDs and Device Drivers, Second Edition

Karen Hazzah / CMP / 1996-01-12 / USD 54.95

Software developer and author Karen Hazzah expands her original treatise on device drivers in the second edition of "Writing Windows VxDs and Device Drivers." The book and companion disk include the a......一起来看看 《Writing Windows VxDs and Device Drivers, Second Edition》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

URL 编码/解码
URL 编码/解码

URL 编码/解码

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试