在 Go 语言中,时间包提供了确定和查看时间的功能。 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)
}
输出:
The stated hour is: 12 hours
The stated minute is: 45 minutes
The stated second is: 55 seconds
示例 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)
}
输出:
The stated hour is: 2 hours
The stated minute is: 7 minutes
The stated second is: 40 seconds
此处,所述的小时、分钟和日期超出了通常的范围,但它们在转换时已标准化。