29

golang中的时间和时区

 5 years ago
source link: https://studygolang.com/articles/14933?amp%3Butm_medium=referral
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

先写一段测试代码:

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个时区,想想都可怕。

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK