Golang time.Time.Location()函数及示例
在Go语言中,time包提供了确定和查看时间的功能。Go语言中的 Time.Location() 函数用于检查与“t”关联的时区数据。此外,该函数是在time包下定义的。在此,您需要导入“time”包以使用这些函数。
语法:
func (t Time) Location() *Location
在这里,“t”表示所述时间,*Location是指向Location的指针。
返回值: 它返回与“t”关联的时区的信息。
示例1:
//演示使用Time.Location()函数的Golang程序
//包括主要包
package main
//导入fmt和time
import "fmt"
import "time"
//调用main
func main() {
//定义t以调用Location方法
t := time.Date(2019, 2, 11, 10, 03, 00, 00, time.UTC)
//调用Location方法
loc := t.Location()
//打印输出
fmt.Printf("%v\n", loc)
}
输出:
UTC
示例2:
//演示使用Time.Location()函数的Golang程序
//包括主要包
package main
//导入fmt和time
import "fmt"
import "time"
//调用main
func main() {
//使用FixedZone方法定义位置
location := time.FixedZone("UTC-7", -6 * 56 * 34)
//定义t以调用Location方法
t := time.Date(2019, 2, 11, 10, 03, 00, 00, location)
//调用Location方法
loc := t.Location()
//打印输出
fmt.Printf("%v\n", loc)
}
输出:
UTC-7
在这里,使用FixedZone()方法来定义Date()方法的位置参数,所以根据该位置返回输出中的时区数据。