Golang time.Time.Clock() 函数及示例
Go 语言的 time 包提供用于确定和查看时间的功能。在 Go 语言中, Time.Clock() 函数用于检查指定的“t”所在的当天的小时、分钟和秒数。此外,该函数是在 time 包下定义的。在使用这些函数时,需要导入“time”包。
语法:
func (t Time) Clock() (hour, min, sec int)
在此,”t” 表示指定的时间。
返回值: 该函数返回指定“t”的小时、分钟和秒数。
示例 1:
// Golang program to illustrate the usage of
// Time.Clock() function
// Including main package
package main
// Importing fmt and time
import "fmt"
import "time"
// Calling main
func main() {
// Declaring t in UTC
t := time.Date(2020, 13, 34, 12, 45, 55, 0, time.UTC)
// Calling Clock method
hour, minute, second := t.Clock()
// Prints hours
fmt.Printf("The stated hour is: %v hours\n", hour)
// Prints minutes
fmt.Printf("The stated minute is: %v minutes\n", minute)
// Prints seconds
fmt.Printf("The stated second is: %v seconds\n", second)
}
输出:
指定的小时是: 12 小时
指定的分钟是: 45 分钟
指定的秒钟是: 55 秒
示例 2:
// Golang program to illustrate the usage of
// Time.Clock() function
// Including main package
package main
// Importing fmt and time
import "fmt"
import "time"
// Calling main
func main() {
// Declaring t in UTC
t := time.Date(2020, 13, 34, 25, 66, 100, 0, time.UTC)
// Calling Clock method
hour, minute, second := t.Clock()
// Prints hours
fmt.Printf("The stated hour is: %v hours\n", hour)
// Prints minutes
fmt.Printf("The stated minute is: %v minutes\n", minute)
// Prints seconds
fmt.Printf("The stated second is: %v seconds\n", second)
}
输出:
指定的小时是: 2 小时
指定的分钟是: 7 分钟
指定的秒钟是: 40 秒
在这里,指定的小时、分钟和日超出了通常的范围,但在转换时进行了归一化。