当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


GO Location用法及代码示例

GO语言"time"包中"Location"类型的用法及代码示例。

位置将时间瞬间映射到当时使用的区域。通常,位置表示地理区域中使用的时间偏移的集合。对于许多位置,时间偏移量会有所不同,具体取决于当时是否使用夏令时。

用法:

type Location struct {
    // contains filtered or unexported fields
}

Local 表示系统的本地时区。在 Unix 系统上,Local 查询 TZ 环境变量以查找要使用的时区。无 TZ 表示使用系统默认的 /etc/localtime。 TZ="" 表示使用 UTC。 TZ="foo" 表示使用系统时区目录中的文件 foo。

var Local *Location">Location = &localLoc

UTC 代表世界协调时间 (UTC)。

var UTC *Location">Location = &utcLoc

例子:

package main

import (
    "fmt"
    "time"
)

func main() {
    // China doesn't have daylight saving. It uses a fixed 8 hour offset from UTC.
    secondsEastOfUTC := int((8 * time.Hour).Seconds())
    beijing := time.FixedZone("Beijing Time", secondsEastOfUTC)

    // If the system has a timezone database present, it's possible to load a location
    // from that, e.g.:
    //    newYork, err := time.LoadLocation("America/New_York")

    // Creating a time requires a location. Common locations are time.Local and time.UTC.
    timeInUTC := time.Date(2009, 1, 1, 12, 0, 0, 0, time.UTC)
    sameTimeInBeijing := time.Date(2009, 1, 1, 20, 0, 0, 0, beijing)

    // Although the UTC clock time is 1200 and the Beijing clock time is 2000, Beijing is
    // 8 hours ahead so the two dates actually represent the same instant.
    timesAreEqual := timeInUTC.Equal(sameTimeInBeijing)
    fmt.Println(timesAreEqual)

}

输出:

true

相关用法


注:本文由纯净天空筛选整理自golang.google.cn大神的英文原创作品 Location。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。