Golang time.Time.Local()函数的使用及示例
在Golang语言中,time包提供了确定和查看时间的功能。 Time.Local() 函数是Golang语言中用于查找具有本地时间设置的“t”的函数。此外,此函数是在time包下定义的。在这里,您需要导入“time”包才能使用这些函数。
语法:
func (t Time) Local() Time
在这里,“t”是指定的时间。
返回值: 返回 “t”以及设置为本地时间的位置。
示例1:
// Golang程序示例,说明了Time.Local()函数的用法
// 包含主包
package main
// 导入fmt和time
import "fmt"
import "time"
// 调用主体
func main() {
// 定义Local方法的t参数
t := time.Date(2019, 2, 11, 10,
03, 00, 00, time.UTC)
// 调用Local方法
local := t.Local()
// 打印输出
fmt.Printf("%v\n", local)
}
输出:
2019-02-11 10:03:00 +0000 UTC
示例2:
// Golang程序示例,说明了Time.Local()函数的用法
// 包含主包
package main
// 导入fmt和time
import "fmt"
import "time"
// 调用主体
func main() {
// 使用FixedZone方法定义位置
location := time.FixedZone("UTC-7", -6*56*34)
// 为调用Local方法定义t
t := time.Date(2019, 2, 11, 10, 03, 00, 00, location)
// 调用Local方法
local := t.Local()
// 打印输出
fmt.Printf("%v\n", local)
}
输出:
2019-02-11 13:13:24 +0000 UTC
在这里,FixedZone()方法用于定义Date()方法的位置参数,因此输出中的时间根据该位置返回。