• python编程基础之三十


    时间模块:

    时间戳:就是当前是键距离1970年1月1日0:0:0的秒数,后面还带小数,可以说是非常精确

    时间的表示形式:
     a.以整数或者浮点数表示一个以秒为单位的时间间隔,这个时间的基础值1970.1.1的零时零分零秒(时间戳)
     b.采用Python中的数据结构表示,采用元组,元组中可以有9个不同的元素,表示不同的含义
     c.格式化的时间字符串

    time 模块:

     1 import time
     2 
     3 #1.获取当前时间戳
     4 t = time.time()  #1527506380.6267307
     5 print(t)
     6 
     7 #2.将时间戳转换为元组
     8 tup = time.gmtime(t)
     9 print(tup)
    10 
    11 #3.获取本地时间的时间元组
    12 local = time.localtime()
    13 print(local)
    14 
    15 #4.将元组转为时间戳
    16 t1 = time.mktime(tup)
    17 print(t1)
    18 
    19 #5.格式化时间  元组=>字符串
    20 print(time.strftime('%Y-%m-%d %H:%M:%S',tup))
    21 print(time.strftime('%Y-%m-%d',local))
    22 
    23 #6.时间字符串转元组
    24 print(time.strptime('2018-5-28','%Y-%m-%d'))
    25 
    26 #7.系统休眠
    27 # print('**************')
    28 # time.sleep(3)
    29 # print('--------------')
    30 
    31 #8.计算应用耗时
    32 def test():
    33     sum = 0
    34     print('计算开始')
    35     for x in range(10000000):
    36         sum += x
    37     print('计算结束')
    38 st = time.clock()  #开始时间
    39 test()
    40 end = time.clock() #结束时间
    41 print(end - st)
    42 
    43 #9.一年中的第几天
    44 print(time.strftime('%j',time.localtime()))
    View Code

    datetime模块:这个是对time进行了一些封装

     1 from _datetime import datetime
     2 
     3 #1.获取当前时间
     4 d = datetime.now()
     5 dd = datetime.today()
     6 print(d,dd,type(d))
     7 
     8 #2.获取指定时间
     9 dt = datetime(2018,5,28,20,15,49,0)
    10 print(dt)
    11 
    12 #3 获取日期和时间
    13 print(dt.date())
    14 print(dt.time())
    15 
    16 #4.转换为日期字符串
    17 print(dt.strftime('%Y-%m-%d'))
    18 
    19 #5.转换为元组
    20 print(dt.timetuple())
    21 
    22 print(dt.toordinal())
    23 seconds = dt.timestamp()
    24 print(seconds)
    25 
    26 #6 时间差
    27 d6 = datetime(2018,4,2,10,56,00,3623)
    28 d7 = datetime(2018,4,5,10,56,32,3623)
    29 d8 = d7 - d6
    30 print(d8)  #3 days, 0:00:00
    31 print(d8.days)   #3
    32 
    33 #整数天之外的秒数
    34 print(d8.seconds)
    View Code

    calendar模块:主要是获取日历信息

     1 import  calendar
     2 
     3 #1.直接返回指定年和月的万年历表示形式
     4 print(calendar.month(2018,4))    #********
     5 
     6 
     7 #2.返回万年历的二维列表表示形式
     8 print(calendar.monthcalendar(2018,4))
     9 
    10 #3.直接返回指定年份的万年历表示形式
    11 print(calendar.calendar(2018))   #********
    12 
    13 #4.判断某年是否为闰年
    14 print(calendar.isleap(2010))   #********
    15 print(calendar.leapdays(2000,2020))   #********
    16 
    17 #5.返回指定月的weekday的第一天和这个月的所有的天数
    18 print(calendar.monthrange(2018,4))  #(6, 30)---》(当月开始的第一天为星期几,mon为0,第二个元素表示当月的总天数)
    19 
    20 print(calendar.monthcalendar(2018,4))
    21 
    22 #当前日期表示的星期数
    23 print(calendar.weekday(2018,4,2))   #********
    View Code

    第三方模块安装:可以用pip install + 模块名

    使用pycharm的话,可以在pycharm里面设置里面下载安装,

    因为python自身的版本不同不兼容的问题,很多模块出现一些问题,这个时候也是可以解决的,可以在网上找到解决方法,很多

  • 相关阅读:
    利用python登录网页并自动签到
    链表的学习--创建、添加和删除元素
    Nginx指定IP无须通过认证
    curl命令获取站点的各类响应时间
    django中的Q和F方法
    python subprocess重定向标准输出
    python创建虚拟环境
    flask-sqlalchemy的使用
    sqlachelmy的使用
    wtforms的使用
  • 原文地址:https://www.cnblogs.com/higer666/p/9451586.html
Copyright © 2020-2023  润新知