zoukankan      html  css  js  c++  java
  • python中time库的使用

      本篇博客将介绍python的内置库time,我们将从如下几个方面介绍:

      时间获取:time() ctime() gmtime()

      时间格式化:strftime() strptime()

      程序计时:sleep(),perf_counter()

      时间获取

      time() :获取从1970年1月1日0:00开始到当前时间点的时间,返回一个浮点数,单位为s

      ctime() :获取当前时间,返回一个字符串,格式是星期 月 日 时:分:秒 年

      gmtime();获取当前时间,返回一个可被计算机直接处理的时间,这种格式被称为struct_time格式

      代码示例:

      #time.py

      #coding=gbk

      import time

      def main():

      print("time():{}".format(time.time()))

      print("ctime():{}".format(time.ctime()))

      print("gmtime():{}".format(time.gmtime()))

      main()

      '''

      time():1583564826.0054252

      ctime():Sat Mar 7 15:07:06 2020

      gmtime():time.struct_time(tm_year=2020, tm_mon=3, tm_mday=7, tm_hour=7, tm_min=7, tm_sec=6, tm_wday=5, tm_yday=67, tm_isdst=0)

      '''

      时间格式化

      strftime(tpl,ts):将struct_time格式的时间ts按照tpl格式格式化为直观的时间

      格式化字符如下:

      格式化字符串  含义  取值

      %Y  年份  0000~9999

      %m  月份  01~12

      %B  月份名称  January~December

      %b  月份名称缩写  Jan~Dec

      %d  日期  01~31

      %A  星期  Monday~Sunday

      %a  星期  Mon~Sun

      %H  24H制小时  01~23

      %I  12H制小时  01~12

      %p  上午/下午  AM/PM

      strptime(str,tpl):将给定的字符串时间str按照tpl格式解析成struct_time格式的时间:

      示例代码:

      #time.py

      #coding=gbk

      import time

      def main():

      t=time.gmtime()

      strTime=time.strftime("%Y-%m-%d %H:%M:%S",t)

      structTime=time.strptime(strTime,"%Y-%m-%d %H:%M:%S")

      print(t)

      print(strTime)

      print(structTime)

      main()

      '''

      time.struct_time(tm_year=2020, tm_mon=3, tm_mday=7, tm_hour=7, tm_min=31, tm_sec=40, tm_wday=5, tm_yday=67, tm_isdst=0)

      2020-03-07 07:31:40

      time.struct_time(tm_year=2020, tm_mon=3, tm_mday=7, tm_hour=7, tm_min=31, tm_sec=40, tm_wday=5, tm_yday=67, tm_isdst=-1)

      '''郑州做人流多少钱 http://mobile.120zzzzyy.com/

      程序计时

      perf_counter():返回一个CPU级别的精确时间计数值,单位为秒,可精确到10^-6ns以上,也就是说精确到10^-15s,由于这个计数值起点不确定,连续调用差值才有意义

      代码示例:

      #time.py

      #coding=gbk

      import time

      def main():

      print(strTime)

      print(structTime)

      start=time.perf_counter()

      print('waste of time')

      end=time.perf_counter()

      print("the differTime={}".format(end-start))

      main()

      '''

      waste of time

      the differTime=0.00012259999999999355

      '''

      sleep(t):休眠ts时间

      代码举例:

      #time.py

      #coding=gbk

      import time

      def main():

      start=time.perf_counter()

      time.sleep(2.555)

      end=time.perf_counter()

      print("the differTime={}".format(end-start))

      main()

      '''

      the differTime=2.5542359

      '''

  • 相关阅读:
    计算机组成原理_存储器
    常用CMD命令
    swiper及其父级隐藏之后轮播失效问题
    canvas生成海报
    移动端h5 实现多个音频播放
    vuex的一些学习
    关于H5的一些杂思细想(一)
    vue Error: No PostCSS Config found in
    vue路由传参的三种方式区别(params,query)
    vue-cli+mock.js+axios模拟前后台数据交互
  • 原文地址:https://www.cnblogs.com/djw12333/p/12449360.html
Copyright © 2011-2022 走看看