當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。