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)
0tm_year(年)比如2011
1tm_mon(月)1 - 12
2tm_mday(日)1 - 31
3tm_hour(时)0 - 23
4tm_min(分)0 - 59
5tm_sec(秒)0 - 61
6tm_wday(weekday)0 - 6(0表示周日)
7tm_yday(一年中的第几天)1 - 366
8tm_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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值