首页 > 其他分享 >go的timeDuration

go的timeDuration

时间:2023-03-04 22:55:56浏览次数:36  
标签:Millisecond Duration Seconds var duration time 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()
}

标签:Millisecond,Duration,Seconds,var,duration,time,go,timeDuration
From: https://www.cnblogs.com/heris/p/17179431.html

相关文章

  • go的sync,未完成timeDuration
    packagemainimport( "fmt" "sync" "time")varproducer=func(wg*sync.WaitGroup,lsync.Locker){ deferwg.Done() fori:=5;i>0;i--{ l.Lock()......
  • Paper Reading: How good are query optimizers, really?
    Title“HowGoodAreQueryOptimizers,Really?”(Leis等,2015,p.204)「查询优化器到底有多好?大概就是通过比较查询优化器的有无时,查询执行的性能,来得到查询优化......
  • django终章篇
    从django终章篇1.Cookie和Session1.1基于cookie的登录cookie设置在响应头上,获取在请求头上设置cookie时要先获得一个响应对象(HttpResponse),然后在这个响应对象上......
  • Golang HTTP编程及源码解析-请求/响应处理
    1.HTTP协议HTTP协议是HyperTextTransferProtocol(超文本传输协议)的缩写,基于TCP/IP通信协议来传递数据(HTML文件、图片文件、查询结果等)。HTTP是无连接的:无连接的......
  • django基础部分
    django基础部分1.django的安装与执行django安装pip3installdjango==1.11.11利用命令行创建django项目django-adminstartproject项目名利用命令行创建app......
  • django进阶
    django进阶一.Django模板系统1.常用语法只需要记两种特殊符号:{{}}和{%%}变量相关的用{{}},逻辑相关的用{%%}。1.1.变量在Django的模板语言中按此语法使用:{{......
  • 跨屏建站平台2023.3.4发布更新,启用了新logo
    跨屏建站平台2023.3.4发布更新,启用了新logo,网站整体风格布局进行了比较大的调整,走的是极简设计风格,最大的变化是网站变得更加简洁了,网站采用极简设计的好处很多,不仅耐看,而......
  • Django --python manage
    Django--pythonmanage.pycollectstatic应用场景:线上部署(Linux)注意:执行时,需要与manag.py处于同一路径下作用:执行该命令时,将所有STATICFILES_DIRS中的所有文件夹......
  • 工作室LOGO发布!!!
    工作室LOGO发布!!!现在,FengyunStudio的LOGO终于发布了!!感谢大家的支持欢迎加入官方QQ群:点击加入......
  • 手刷算法day1(2)(go语言实践)
    344. 反转字符串 编写一个函数,其作用是将输入的字符串反转过来。输入字符串以字符数组 s 的形式给出。不要给另外的数组分配额外的空间,你必须原地修改输入数组、使......