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


GO ParseInLocation用法及代碼示例

GO語言"time"包中"ParseInLocation"函數的用法及代碼示例。

用法:

func ParseInLocation(layout, value string, loc *Location)(Time, error)

ParseInLocation 與 Parse 類似,但在兩個重要方麵有所不同。首先,在沒有時區信息的情況下,Parse 將時間解釋為 UTC; ParseInLocation 將時間解釋為給定位置。其次,當給定區域偏移量或縮寫時,Parse 會嘗試將其與本地位置進行匹配; ParseInLocation 使用給定的位置。

例子:

package main

import (
    "fmt"
    "time"
)

func main() {
    loc, _ := time.LoadLocation("Europe/Berlin")

    // This will look for the name CEST in the Europe/Berlin time zone.
    const longForm = "Jan 2, 2006 at 3:04pm (MST)"
    t, _ := time.ParseInLocation(longForm, "Jul 9, 2012 at 5:02am (CEST)", loc)
    fmt.Println(t)

    // Note: without explicit zone, returns time in given location.
    const shortForm = "2006-Jan-02"
    t, _ = time.ParseInLocation(shortForm, "2012-Jul-09", loc)
    fmt.Println(t)

}

輸出:

2012-07-09 05:02:00 +0200 CEST
2012-07-09 00:00:00 +0200 CEST

相關用法


注:本文由純淨天空篩選整理自golang.google.cn大神的英文原創作品 ParseInLocation。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。