zoukankan      html  css  js  c++  java
  • Python之路【第十八篇】:time与datatime模块

    UTC时间:协调世界时,又称为世界统一时间、世界标准时间、国际协调时间,不属于任意时区

    time模块

    time模块提供一些与时间有关的操作,时间有三种表示方式:

    • 时间戳:1970年1月1日之后的秒,即time.time()
    • 格式化的字符串:2014-11-11 11:11,即:time.strftime('%Y-%m-%d')
    • 结构化时间:元组包含了:年、日、星期等... time.struct_time    即:time.localtime()
    import time
    
    print time.time()
    print time.mktime(time.localtime())
      
    print time.gmtime()    #utc时间的struct_time对象格式
    print time.localtime() #本地时间的struct_time对象格式
    print(time.altzone)#本地时间与utc时间的时间差,以秒计算 print time.strptime('2014-11-11', '%Y-%m-%d') print time.strftime('%Y-%m-%d') #默认当前时间 print time.strftime('%Y-%m-%d',time.localtime()) #默认当前时间 print time.asctime() print time.asctime(time.localtime()) print time.ctime(time.time())
    time.sleep(5) #程序终止5秒

     datatime模块

    import datetime
    '''
    datetime.date:表示日期的类。常用的属性有year, month, day
    datetime.time:表示时间的类。常用的属性有hour, minute, second, microsecond
    datetime.datetime:表示日期时间
    datetime.timedelta:表示时间间隔,即两个时间点之间的长度
    timedelta([days[, seconds[, microseconds[, milliseconds[, minutes[, hours[, weeks]]]]]]])
    strftime("%Y-%m-%d")
    '''print datetime.datetime.now()
    print datetime.datetime.now() - datetime.timedelta(days=5)

    格式化占位符

    %Y  Year with century as a decimal number.
    %m  Month as a decimal number [01,12].
    %d  Day of the month as a decimal number [01,31].
    %H  Hour (24-hour clock) as a decimal number [00,23].
    %M  Minute as a decimal number [00,59].
    %S  Second as a decimal number [00,61].
    %z  Time zone offset from UTC.
    %a  Locale's abbreviated weekday name.
    %A  Locale's full weekday name.
    %b  Locale's abbreviated month name.
    %B  Locale's full month name.
    %c  Locale's appropriate date and time representation.
    %I  Hour (12-hour clock) as a decimal number [01,12].
    %p  Locale's equivalent of either AM or PM.

     例程

    #程序终止5秒
    import time print('start to sleep...') time.sleep(5)#程序终止5秒 print('wake up!')
    print(time.time())
    输出结果为:1523504246.5461433,输出时间戳
    print(time.ctime())
    返回系统当前时间,也可以把时间戳转换成字符串格式
    输出结果为:Thu Apr 12 11:38:46 2018
    print(time.ctime(time.time()))
    print(time.gmtime(time.time()))#返回struct_time格式时间
    t = time.gmtime(time.time())
    print(t)
    print('%s-%s-%s'%(t.tm_year,t.tm_mon,t.tm_mday))
    输出结果为:

    Thu Apr 12 11:40:32 2018
    time.struct_time(tm_year=2018, tm_mon=4, tm_mday=12, tm_hour=3, tm_min=40, tm_sec=32, tm_wday=3, tm_yday=102, tm_isdst=0)
    time.struct_time(tm_year=2018, tm_mon=4, tm_mday=12, tm_hour=3, tm_min=40, tm_sec=32, tm_wday=3, tm_yday=102, tm_isdst=0)

    2018-4-12

    print(time.localtime())
    print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime()))#将struct_time格式转换为指定格式
    print(time.strptime('2018-3-26','%Y-%m-%d'))
    #time.strptime()将字符串格式转换为struct_time格式
    输出结果为:

    time.struct_time(tm_year=2018, tm_mon=4, tm_mday=12, tm_hour=11, tm_min=42, tm_sec=37, tm_wday=3, tm_yday=102, tm_isdst=0)
    2018-04-12 11:42:37
    time.struct_time(tm_year=2018, tm_mon=3, tm_mday=26, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=85, tm_isdst=-1)

    三样东西有助于缓解生命的疲劳:希望、睡眠和微笑。---康德
  • 相关阅读:
    tech 浅谈 Yield
    Python strip lstrip rstrip使用方法
    Python strip lstrip rstrip使用方
    Python 的学习脚印(1)
    python中列表的赋值
    python的re模块的sub方法
    Python的异常处理机制
    [python] shutil模块
    【ActiveMq】启动amq时遇到java.net.URISyntaxException: Illegal character in hostname at index处理方法
    【sql总结】
  • 原文地址:https://www.cnblogs.com/ronghe/p/8681427.html
Copyright © 2011-2022 走看看