go的timeDuration

package main

import (
	"fmt"
	"time"
)
//https://blog.csdn.net/qq_35655945/article/details/82706022
type Duration int64

const (
	Nanosecond  Duration = 1
	Microsecond          = 1000 * Nanosecond
	Millisecond          = 1000 * Microsecond
	Second               = 1000 * Millisecond
	Minute               = 60 * Second
	Hour                 = 60 * Minute
)

func Test0() {
	var waitFiveHundredMillisections int64 = 500
	startingTime := time.Now().UTC()
	time.Sleep(10 * time.Millisecond)
	endingTime := time.Now().UTC()

	var duration time.Duration = endingTime.Sub(startingTime)
	var durationAsInt64 = int64(duration)

	if durationAsInt64 >= waitFiveHundredMillisections {
		fmt.Printf("Time Elapsed : Wait[%d] Duration[%d]\n", waitFiveHundredMillisections, durationAsInt64)
	} else {
		fmt.Printf("Time DID NOT Elapsed : Wait[%d] Duration[%d]\n", waitFiveHundredMillisections, durationAsInt64)
	}
}

func Test1() {
	var duration_Milliseconds time.Duration = 500 * time.Millisecond
	//var duration_Seconds time.Duration = (1250 * 1000000) * time.Millisecond
	//var duration_Seconds time.Duration = (60 * 12 * 60) * time.Second
	var duration_Seconds time.Duration = 60 * 12 * 60 * time.Second
	var duration_Minute time.Duration = 2 * time.Minute
	fmt.Printf("Milli [%v]\nSeconds [%v]\nMinute [%v]\n", duration_Milliseconds, duration_Seconds, duration_Minute)
}

func Test2() {
	var duration_Seconds time.Duration = (1250 * 10) * time.Millisecond
	var duration_Minute time.Duration = 2 * time.Minute
	var float64_Seconds float64 = duration_Seconds.Seconds()
	var float64_Minutes float64 = duration_Minute.Minutes()

	fmt.Printf("Seconds [%.3f]\nMinutes [%.2f]\n", float64_Seconds, float64_Minutes)
}
func Test3() {
	var duration_Milliseconds time.Duration = 500 * time.Millisecond

	var castToInt64 int64 = duration_Milliseconds.Nanoseconds() / 1e6
	var castToFloat64 float64 = duration_Milliseconds.Seconds() * 1e3
	fmt.Printf("Duration [%v]\ncastToInt64 [%d]\ncastToFloat64 [%.0f]\n", duration_Milliseconds, castToInt64, castToFloat64)
}

func Test4() {
	var waitFiveHundredMillisections time.Duration = 500 * time.Millisecond

	startingTime := time.Now().UTC()
	time.Sleep(600 * time.Millisecond)
	endingTime := time.Now().UTC()

	var duration time.Duration = endingTime.Sub(startingTime)

	if duration >= waitFiveHundredMillisections {
		fmt.Printf("Wait %v\nNative [%v]\nMilliseconds [%d]\nSeconds [%.3f]\n", waitFiveHundredMillisections, duration, duration.Nanoseconds()/1e6, duration.Seconds())
	}
}
func main() {
	//Test0()
	//Test1()
	//Test2()
	//Test3()
	Test4()
}
posted @ 2023-03-04 22:45  ty1539  阅读(25)  评论(0编辑  收藏  举报