首页 > 编程语言 > 详细

Python之time模块

时间:2017-09-21 20:14:51      阅读:255      评论:0      收藏:0      [点我收藏+]

前言:

环境:python解释器是windows平台3.6.1版本.

 Python中主要使用1.元组(struct_time对象) 2.格式化的时间字符串 3.时间戳 这三种形式表示时间.本文主要讲述利用time模块的内置函数进行三种形式互相转化.

 

from time import *

tuple1 = (2017, 9, 23, 14, 45, 10, 0, 0, 0)


def demo1():
    """
    struct_time构造函数
    """
    struct_time1 = struct_time(tuple1)  # 把9个元素的元组转化为struct_time类的对象
    print(type(struct_time1), struct_time1)


def demo2():
    """
    strftime函数用法:把元组(struct_time类是tuple类的子类)转化为字符串
    """
    strftime1 = strftime(%Y-%m-%d %H:%M:%S %A %p %a %z %b %B %I %c)
    print(type(strftime1), strftime1)
    strftime2 = strftime(%Y-%m-%d, tuple1)
    print(type(strftime2), strftime2)
    strftime2 = strftime(%Y-%m-%d %H:%M:%S, localtime())
    print(type(strftime2), strftime2)


def demo3():
    """
    演示ctime函数的用法
    """
    ctime1 = ctime()  # 直接从本地时间的时间戳转换为时间的字符串表示形式
    print(type(ctime1), ctime1)
    ctime2 = ctime(1500000000)  # 直接从时间戳转换为时间的字符串表示形式
    print(type(ctime2), ctime2)


def demo4():
    """
    演示strptime函数的用法
    """
    strptime1 = strptime(2017-09-21 15:07:38, %Y-%m-%d %H:%M:%S)
    print(type(strptime1), strptime1)
    print(strptime1.tm_yday)


def demo5():
    """
    演示gmtime和localtime函数的用法
    """
    gmtime1 = gmtime(1500000000)  # 把时间戳转化为struct_time类的对象(本初子午线时间)
    print(type(gmtime1), gmtime1)
    gmtime1 = gmtime()  # 把当前时间的时间戳转化为struct_time类的对象(本初子午线时间),
    print(type(gmtime1), gmtime1)
    localtime1 = localtime(1500000000)  # 把时间戳转化为struct_time类的对象(本地时间)
    print(type(localtime1), localtime1)
    print(type(localtime()), localtime())  # 把当前时间的时间戳转化为struct_time类的对象(本地时间)


def demo6():
    """
    演示mktime函数的用法
    """
    mktime1 = mktime(tuple1)  # 把元组转化为时间戳
    print(type(mktime1), mktime1)

def demo7():
"""
演示time函数的用法
"""
print(time())
print(mktime(localtime()))
demo1()
demo2()
demo3()
demo4()
demo5()
demo6()
demo7()
‘‘‘ 
strptime和strftime使用的格式字符:
  • %y 两位数的年份表示(00-99)
  • %Y 四位数的年份表示(000-9999)
  • %m 月份(01-12)
  • %d 月内中的一天(0-31)
  • %H 24小时制小时数(0-23)
  • %I 12小时制小时数(01-12)
  • %M 分钟数(00=59)
  • %S 秒(00-59)
  • %a 本地简化星期名称
  • %A 本地完整星期名称
  • %b 本地简化的月份名称
  • %B 本地完整的月份名称
  • %c 本地相应的日期表示和时间表示
  • %j 年内的一天(001-366)
  • %p 本地A.M.或P.M.的等价符
  • %U 一年中的星期数(00-53)星期天为星期的开始
  • %w 星期(0-6),星期天为星期的开始
  • %W 一年中的星期数(00-53)星期一为星期的开始
  • %x 本地相应的日期表示
  • %X 本地相应的时间表示
  • %Z 当前时区的名称
  • %% %号本身

输出:(部分输出会因程序运行时的时间而改变)

<class ‘time.struct_time‘> time.struct_time(tm_year=2017, tm_mon=9, tm_mday=23, tm_hour=14, tm_min=45, tm_sec=10, tm_wday=0, tm_yday=0, tm_isdst=0)
<class ‘str‘> 2017-09-21 19:16:19 Thursday PM Thu +0800 Sep September 07 Thu Sep 21 19:16:19 2017
<class ‘str‘> 2017-09-23
<class ‘str‘> 2017-09-21 19:16:19
<class ‘str‘> Thu Sep 21 19:16:19 2017
<class ‘str‘> Fri Jul 14 10:40:00 2017
<class ‘time.struct_time‘> time.struct_time(tm_year=2017, tm_mon=9, tm_mday=21, tm_hour=15, tm_min=7, tm_sec=38, tm_wday=3, tm_yday=264, tm_isdst=-1)
264
<class ‘time.struct_time‘> time.struct_time(tm_year=2017, tm_mon=7, tm_mday=14, tm_hour=2, tm_min=40, tm_sec=0, tm_wday=4, tm_yday=195, tm_isdst=0)
<class ‘time.struct_time‘> time.struct_time(tm_year=2017, tm_mon=9, tm_mday=21, tm_hour=11, tm_min=16, tm_sec=19, tm_wday=3, tm_yday=264, tm_isdst=0)
<class ‘time.struct_time‘> time.struct_time(tm_year=2017, tm_mon=7, tm_mday=14, tm_hour=10, tm_min=40, tm_sec=0, tm_wday=4, tm_yday=195, tm_isdst=0)
<class ‘time.struct_time‘> time.struct_time(tm_year=2017, tm_mon=9, tm_mday=21, tm_hour=19, tm_min=16, tm_sec=19, tm_wday=3, tm_yday=264, tm_isdst=0)
<class ‘float‘> 1506149110.0

1505993665.347068
1505993665.0

Python之time模块

原文:http://www.cnblogs.com/zhaoxianglong1987/p/7568916.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!