分享兴趣,传播快乐,增长见闻,留下美好! 亲爱的您,这里是LearningYard学苑。 今天小编为大家带来今日分享(三十九),欢迎您的访问! Shareinterests,spreadhappiness,increaseknowledge, andleaveagoodlegacy! Dearyou,thisisTheLearningYardAcademy。 TodayXiaobianbringsyoutodayssharing(thirtynine),welcomeyourvisit! time库基本介绍 time库是python中处理时间的标准 time库包含的三类函数: 时间获取:time()、ctime()、gmtime() 时间格式化:strftime()、strptime() 程序计时:sleep()、perfcounter() 下面小编将为你们逐一介绍 1、时间获取(Timeacquisition) time():获取当前时间戳,即计算机内部时间值,但时间戳是一个很长的浮点数,不好理解。 time():Getthecurrenttimestamp,thatis,theinternaltimevalueofthecomputer,butthetimestampisaverylongfloatingpointnumber,whichisdifficulttounderstand。 ctime():获取当前时间并以易读方式表示,返回字符串,是获取时间最简单的方式 ctime():Getsthecurrenttimeandrepresentsitinaneasytoreadmanner,returnsastring,istheeasiestwaytogetthetime gmtime():获取当前时间,表示为计算机可处理的时间格式 gmtime():Getsthecurrenttime,expressedasacomputerprocessabletimeformat 2、时间格式化(Timeformatting) 格式化类似于字符串格式化,需要有展示模板。展示模板由特定的格式化控制符组成 Formattingissimilartostringformattingandrequiresapresentationtemplate。Presentationtemplatesconsistofspecificformattingcontrols strftime(tpl,ts):tpl是格式化模板字符串,用来定义输出效果;ts是计算机内部时间类型变量 strftime(tpl,ts):TPLisaformattedtemplatestringusedtodefinetheoutputeffect;tsisacomputerinternaltimetypevariable strptime(tpl,str):str是字符串形式的时间值,tpl是格式化模板字符串,用来定义输入效果。因此,strftime(tpl,ts)与strptime(tpl,str)是反向过程。 strptime(tpl,str):strisatimevalueintheformofastring,andtplisaformattedtemplatestringthatdefinestheinputeffect。Therefore,strftime(tpl,ts)andstrptime(tpl,str)arereverseprocesses。 下面是时间格式化对应字符串的表示: Thefollowingistherepresentationofthecorrespondingstringforthetimeformat: 3、程序计时应用(Programtimingapplication) 程序计时指测量起止动作所经历的时间过程 Programtimingreferstothetimeprocessthatmeasuresthestartandendoftheaction 测量时间函数:perfcounter():返回一个CPU级别的精确时间计数值,单位为秒由于这个计数值起点不确定,连续调用差值才有意义 Measurementtimefunction:perfcounter():ReturnsanaccuratetimecountvalueattheCPUlevel,measuredinseconds,andsincethestartingpointofthiscountvalueisuncertain,thedifferenceinconsecutivecallsismeaningful。 产生时间函数:sleep(s):s拟休眠时间,单位是秒,可以是浮点数 今天的分享就到这里了。 如果您对今天的文章有什么独特的想法, 欢迎评论留言,让我们相约明天, 祝您今天过得开心快乐! Thatsitfortodayssharing。 Ifyouhaveanyuniqueideasfortodaysarticle,pleaseleaveacomment,letusmeettomorrow, Iwishyouahappyday! 参考资料:《python程序设计》 翻译:谷歌翻译 本文由learningyard新学苑原创,部分文字图片来源于他处,如有侵权,请联系删除