Python time模块

1、在Python中,通常有这几种方式来表示时间:

  • (1)、时间戳

       时间戳(timestamp)的方式:通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量。我们运行“type(time.time())”,返回的是float类型。返回时间戳方式的函数主要有time(),clock()等。

  • (2)、格式化的时间字符串

  • (3)、元组(struct_time)共九个元素

          元组(struct_time)方式:struct_time元组共有9个元素,返回struct_time的函数主要有gmtime(),localtime(),strptime()。下面列出这种方式元组中的几个元素:

索引(Index)属性(Attribute)值(Values)
0 tm_year(年) 比如2011
1 tm_mon(月) 1 - 12
2 tm_mday(日) 1 - 31
3 tm_hour(时) 0 - 23
4 tm_min(分) 0 - 59
5 tm_sec(秒) 0 - 61
6 tm_wday(weekday) 0 - 6(0表示周日)
7 tm_yday(一年中的第几天) 1 - 366
8 tm_isdst(是否是夏令时) 默认为-1

 2、几种表现形式之间的转换关系

 3、Time模块常用函数

time.time():

返回当前时间的时间戳。

#!/usr/bin/env python
# -*- coding: utf-8 -*
# Created by YangYongming at 2018/12/24 12:41
# FileName: 1.py

import time
print(time.time())

"""
1545629801.1879263

Process finished with exit code 0

"""

time.localtime([secs]):

将一个时间戳转换为当前时区的struct_time。secs参数未提供,则以当前时间为准。

#!/usr/bin/env python
# -*- coding: utf-8 -*
# Created by YangYongming at 2018/12/24 12:41
# FileName: 1.py

import time
structtime = time.localtime()
print(structtime)

"""
time.struct_time(tm_year=2018, tm_mon=12, tm_mday=24, tm_hour=13, tm_min=34, tm_sec=23, tm_wday=0, tm_yday=358, tm_isdst=0)

Process finished with exit code 0

"""

 time.gmtime([secs]):

和localtime()方法类似,gmtime()方法是将一个时间戳转换为UTC时区(0时区)的struct_time。

#!/usr/bin/env python
# -*- coding: utf-8 -*
# Created by YangYongming at 2018/12/24 12:41
# FileName: 1.py

import time
print(time.gmtime())

"""
time.struct_time(tm_year=2018, tm_mon=12, tm_mday=24, tm_hour=5, tm_min=38, tm_sec=2, tm_wday=0, tm_yday=358, tm_isdst=0)

Process finished with exit code 0

"""

time.mktime(t):

将一个struct_time转化为时间戳。

#!/usr/bin/env python
# -*- coding: utf-8 -*
# Created by YangYongming at 2018/12/24 12:41
# FileName: 1.py

import time
st = time.localtime()
print(time.mktime(st))

"""
1545629970.0

Process finished with exit code 0
"""

time.asctime([t]):

把一个表示时间的元组或者struct_time表示为这种形式:'Sun Jun 10 13:29:09 2018'。如果没有参数,将会将time.localtime()作为参数传入。

#!/usr/bin/env python
# -*- coding: utf-8 -*
# Created by YangYongming at 2018/12/24 12:41
# FileName: 1.py

import time

print(time.asctime())

"""
Mon Dec 24 13:41:28 2018

Process finished with exit code 0
"""

time.ctime([secs]):

把一个时间戳(按秒计算的浮点数)转化为time.asctime()的形式。如果参数未给或者为None的时候,将会默认time.time()为参数。它的作用相当于time.asctime(time.localtime(secs))。

#!/usr/bin/env python
# -*- coding: utf-8 -*
# Created by YangYongming at 2018/12/24 12:41
# FileName: 1.py

import time

print(time.ctime())
"""
Mon Dec 24 13:42:51 2018

Process finished with exit code 0
"""

time.strftime(format[, t]):

把一个代表时间的元组或者struct_time(如由time.localtime()和time.gmtime()返回)转化为格式化的时间字符串。如果t未指定,将传入time.localtime()。如果元组中任何一个元素越界,ValueError的错误将会被抛出。

#!/usr/bin/env python
# -*- coding: utf-8 -*
# Created by YangYongming at 2018/12/24 12:41
# FileName: 1.py
import time

# 格式化成2016-03-20 11:45:39形式
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))

# 格式化成Sat Mar 28 22:24:24 2016形式
print(time.strftime("%a %b %d %H:%M:%S %Y", time.localtime()))

"""
2018-12-24 14:27:07
Mon Dec 24 14:27:07 2018

Process finished with exit code 0
""" 

time.strptime(string[, format]):

把一个格式化时间字符串转化为struct_time。实际上它和strftime()是逆操作。

#!/usr/bin/env python
# -*- coding: utf-8 -*
# Created by YangYongming at 2018/12/24 12:41
# FileName: 1.py

import time

a = "20181212102030"
print(time.strptime(a, "%Y%m%d%H%M%S"))

"""
time.struct_time(tm_year=2018, tm_mon=12, tm_mday=12, tm_hour=10, tm_min=20, tm_sec=30, tm_wday=2, tm_yday=346, tm_isdst=-1)

Process finished with exit code 0
"""
格式含义备注
%a 本地(locale)简化星期名称  
%A 本地完整星期名称  
%b 本地简化月份名称  
%B 本地完整月份名称  
%c 本地相应的日期和时间表示  
%d 一个月中的第几天(01 - 31)  
%H 一天中的第几个小时(24小时制,00 - 23)  
%I 第几个小时(12小时制,01 - 12)  
%j 一年中的第几天(001 - 366)  
%m 月份(01 - 12)  
%M 分钟数(00 - 59)  
%p 本地am或者pm的相应符
%S 秒(01 - 61)
%U 一年中的星期数。(00 - 53星期天是一个星期的开始。)第一个星期天之前的所有天数都放在第0周。
%w 一个星期中的第几天(0 - 6,0是星期天)
%W 和%U基本相同,不同的是%W以星期一为一个星期的开始。  
%x 本地相应日期  
%X 本地相应时间  
%y 去掉世纪的年份(00 - 99)  
%Y 完整的年份  
%Z 时区的名字(如果不存在为空字符)  
%% ‘%'字符

原文地址:https://www.cnblogs.com/ming5218/p/10168078.html