Golang time.Time.In()函数及示例
在Go语言中,time包提供了一些用于确定和显示时间的功能。在Go语言中, In() 函数的作用是查找一个与“t”相同但位置数据设置为“loc”的时间副本,以便于显示。如果“loc”为nil,则返回报警。此外,此函数是定义在time包中的。因此,在使用这些函数之前,需要导入“time”包。
Syntax:
func (t Time) In(loc *Location) Time
在此,”t”是已设置的时间,而”loc”是一个指向位置的指针。
返回值: 返回一个与“t”相同但位置数据设置为“loc”的时间副本,以便于显示。如果“loc”为nil,则返回警报。
示例1:
//演示如何使用Time.In()函数
//导入main包
package main
//导入fmt和time包
import "fmt"
import "time"
//调用main函数
func main() {
//为In方法定义t
t := time.Date(2019, 12, 13, 3, 23, 43, 02, time.UTC)
//定义In方法的loc参数
loc := time.FixedZone("UTC", 6*54*44)
//调用In()方法
res := t.In(loc)
//打印输出
fmt.Printf("%v\n", res)
}
输出:
2019-12-13 07:21:19.000000002 +0357 UTC
示例2:
//演示如何使用Time.In()函数
//导入main包
package main
//导入fmt和time包
import "fmt"
import "time"
//调用main函数
func main() {
//为In方法定义t
t := time.Date(2022, 23, 45, 36, 67, 88, 667, time.UTC)
//定义In方法的loc参数
loc := time.FixedZone("UTC-6", -3*66*77)
//调用In()方法
res := t.In(loc)
//打印输出
fmt.Printf("%v\n", res)
}
输出:
2023-12-16 08:54:22.000000667 -0414 UTC-6
在上述示例代码中,上述“t”的值超出了正常范围,但它们在转换时被规范化。