• 欢迎光临~

Go-16 Golang语言time时间统计

开发技术 开发技术 2022-12-19 次浏览
package main
// time 时间统计
import (
	"fmt"
	"log"
	"time"
)

func main() {
	println()

	now_t := time.Now()
	fmt.Printf("current now time:%v n", now_t)

	time.Sleep(3 * time.Second) // 这个咋就没有生效呢?,少了*time.Second
	now_ti := time.Now()
	fmt.Printf("current now_ti time:%v n", now_ti)
	sub_time := now_ti.Sub(now_t)
	fmt.Println(sub_time)
	println()

	now := time.Now()

	// 方法一:

	beginningOfDay := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
	log.Println("今日已过(纳秒):", now.UnixNano()-beginningOfDay.UnixNano())
	log.Println("今日已过(微秒):", now.UnixMicro()-beginningOfDay.UnixMicro())
	log.Println("今日已过(毫秒):", now.UnixMilli()-beginningOfDay.UnixMilli())
	log.Println("今日已过(秒):", now.Unix()-beginningOfDay.Unix())

	// 方法二:

	passedTimeNs := now.UnixNano() - beginningOfDay.UnixNano()
	log.Println("今日已过(纳秒):", passedTimeNs)
	log.Println("今日已过(微秒):", passedTimeNs/1e3)
	log.Println("今日已过(毫秒):", passedTimeNs/1e6)
	log.Println("今日已过(秒):", passedTimeNs/1e9)

        /*	上述代码全部执行结果是:
		current now time:2022-12-19 17:17:52.9564406 +0800 CST m=+0.002694601
		current now_ti time:2022-12-19 17:17:55.9685528 +0800 CST m=+3.014751401
		3.0120568s

		2022/12/19 17:17:55 今日已过(纳秒): 62275968611600
		2022/12/19 17:17:55 今日已过(微秒): 62275968611
		2022/12/19 17:17:55 今日已过(毫秒): 62275968
		2022/12/19 17:17:55 今日已过(秒): 62275
		2022/12/19 17:17:55 今日已过(纳秒): 62275968611600
		2022/12/19 17:17:55 今日已过(微秒): 62275968611
		2022/12/19 17:17:55 今日已过(毫秒): 62275968
		2022/12/19 17:17:55 今日已过(秒): 62275

	*/

}



程序员灯塔
转载请注明原文链接:Go-16 Golang语言time时间统计
喜欢 (0)