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


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。