zoukankan      html  css  js  c++  java
  • python 之 time模块、datetime模块(打印进度条)

    6.9 time 模块

    方法含义备注
    time.time() 时间戳 1561013092.997079
    time.strftime('%Y-%m-%d %H:%M:%S %p') 结构化时间struct_time 转 格式化的字符串 2019-06-20 10:21:13 AM
    time.strptime('2011-05-05 16:37:06', '%Y-%m-%d %X') 格式化的字符串 转 结构化时间struct_time time.struct_time(tm_year=2011, tm_mon=5...)
    time.localtime() 时间戳转结构化时间struct_time 东八区时间 time.struct_time(tm_year=2019,tm_mon...)
    time.gmtime() 时间戳转结构化时间 UTC时区 time.struct_time(tm_year=2019,tm_mon=6...)
    time.mktime(time.localtime() 将一个struct_time 转 为时间戳 15663646462642646
    time.asctime(time.localtime() 将一个struct_time 转 为Linux显示风格 Thu Jun 20 14:32:05 2019
    time.ctime(12312312321) 将一个时间戳 转 为Linux显示风格 Mon Feb 29 22:45:21 2360

     

    1、时间戳(以秒计算)

    import time
    print(time.time())
    start_time=time.time()
    time.sleep(3)
    stop_time=time.time()
    print(stop_time-start_time)

    2、格式化的字符串

    print(time.strftime('%Y-%m-%d %H:%M:%S %p')) # 2019-06-20 10:21:13 AM
    print(time.strftime('%Y-%m-%d %X %p'))      # 2019-06-20 10:21:13 AM
    strftime(format[, t]) #把一个代表时间的元组或者struct_time(如由time.localtime()和time.gmtime()返回)转化为格式化的时间字符串。如果t未指定,将传入time.localtime()。如果元组中任何一个元素越界,ValueError的错误将会被抛出。
    print(time.strftime("%Y-%m-%d %X", time.localtime())) #2019-06-20 00:49:56

    3、struct_time()对象

    print(time.localtime()) # 上海:东八区 time.struct_time(tm_year=2019,tm_mon=6,tm_mday=20,tm_hour=10,                                       tm_min=24, tm_sec=52, tm_wday=3, tm_yday=171, tm_isdst=0)
    print(time.localtime(1111111111))   #将秒转换成 time.struct_time()格式,不填默认当前时间
    print(time.localtime().tm_year)     # 2019
    print(time.localtime().tm_mday)     # 20
    print(time.gmtime())               #UTC时区 差八个小时 
    #time.struct_time(tm_year=2019,tm_mon=6,tm_mday=20,tm_hour=2,tm_min=29,tm_sec=51,tm_wday=3,tm_yday=171,tm_isdst=0)

    4、 mktime( t ) : 将一个struct_time转化为时间戳

    print(time.mktime(time.localtime()))    #15663646462642646

    5、time.strptime()

    print(time.strptime('2017/04/08','%Y/%m/%d'))
    time.strptime(string[, format]) # 把一个格式化时间字符串转化为struct_time。实际上它和strftime()是逆操作。
    print(time.strptime('2011-05-05 16:37:06', '%Y-%m-%d %X'))
    #time.struct_time(tm_year=2011, tm_mon=5, tm_mday=5, tm_hour=16, tm_min=37, tm_sec=6,
    #  tm_wday=3, tm_yday=125, tm_isdst=-1)
    #在这个函数中,format默认为:"%a %b %d %H:%M:%S %Y"。

    6、time.asctime()

    print(time.asctime(time.localtime()))# Thu Jun 20 14:32:05 2019
    asctime([t]) : 把一个表示时间的元组或者struct_time表示为这种形式:'Sun Jun 20 23:21:05 1993'print(time.asctime())#如果没有参数,将会将time.localtime()作为参数传入。Sun Sep 11 00:43:43 2016

    7、time.ctime()

    print(time.ctime(12312312321)) # Mon Feb 29 22:45:21 2360
    #ctime([secs]) : 把一个时间戳(按秒计算的浮点数)转化为time.asctime()的形式。如果参数未给或者为None的时候,将会默认time.time()为参数。它的作用相当于time.asctime(time.localtime(secs))。
    print(time.ctime())  # Sun Sep 11 00:46:38 2016
    print(time.ctime(time.time()))  # Sun Sep 11 00:46:38 2016

    6.10 datetime 模块

    方法含义备注
    datetime.datetime.now()   2019-06-20 17:06:25.170859
    datetime.datetime.now() + datetime.timedelta(days=3) 当前时间+3天 2019-06-23 17:14:24.660116
    current_time.replace(year=1977) 更改当前时间 1977-06-20 17:18:11.543876
    datetime.date.fromtimestamp(time.time()) 时间戳直接转成日期格式 2019-08-19
    import datetime
    print(datetime.datetime.now() + datetime.timedelta(3)) #当前时间+3天
    print(datetime.datetime.now() + datetime.timedelta(-3)) #当前时间-3天
    print(datetime.datetime.now() + datetime.timedelta(hours=3)) #当前时间+3小时
    print(datetime.datetime.now() + datetime.timedelta(minutes=30)) #当前时间+30分
    current_time=datetime.datetime.now()
    print(current_time.replace(year=1977))#1977-06-20 17:18:11.543876
    print(datetime.date.fromtimestamp(1111111111))#2005-03-18
    print(datetime.date.fromtimestamp(time.time()) )  # 时间戳直接转成日期格式 2016-08-19

    6.11 打印进度条

    def progress(percent,width=50):
        if percent > 1:
            percent=1
        show_str=('[%%-%ds]' %width) %(int(width*percent) * '#')
        print('
    %s %d%%' %(show_str,int(100*percent)),end='')
    ​
    import time
    recv_size=0
    total_size=8097
    while recv_size < total_size:
        time.sleep(0.1)
        recv_size+=80
        percent=recv_size / total_size
        progress(percent)
  • 相关阅读:
    linux常用命令
    mysql 开发基础系列20 事务控制和锁定语句(上)
    sql server 性能调优之 资源等待 CXPACKET
    mysql 开发基础系列19 触发器
    mysql 开发基础系列18 存储过程和函数(下)
    mysql 开发基础系列17 存储过程和函数(上)
    sql server 性能调优之 资源等待PAGEIOLATCH
    mysql 开发基础系列16 视图
    mysql 开发基础系列15 索引的设计和使用
    sql server 性能调优之 当前用户请求分析 (1)
  • 原文地址:https://www.cnblogs.com/mylu/p/11079543.html
Copyright © 2011-2022 走看看