Python3时间处理之time模块

本文详细介绍了Python3中time模块的使用方法,包括时间戳、格式化时间字符串和struct_time的转换,以及如何利用time模块进行时间处理和格式化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Python3时间处理之time模块

Python3中表示时间的方式有三种:

  1. 时间戳(timestamp):时间戳表示从1970年1月1日00:00:00开始按秒计算的偏移量。
  2. 格式化的时间字符串
  3. 元组(struct_time):元素与C的struct tm结构体相同。

在Python3中,与时间处理有关的模块就包括:time,datetime以及calendar。time模块的实现调用的C库,相关的方法基本类似于C中的时间函数。

使用dir(time)可以查看到time模块的属性和方法:

>>> import time
>>> dir(time)
['_STRUCT_TM_ITEMS', '__doc__', '__loader__', '__name__', '__package__', '__spec__',
 'altzone', 'asctime', 'clock', 'ctime', 'daylight', 'get_clock_info', 'gmtime',
  'localtime', 'mktime', 'monotonic', 'perf_counter', 'process_time', 'sleep', 
  'strftime', 'strptime', 'struct_time', 'time', 'timezone', 'tzname']

按时间表示方式,time模块常用方法可归类为:
返回时间戳的方法
返回struct_time的方法
返回格式化时间串的方法
其他

1. 返回时间戳

  1. time():
    返回当前时间戳
>>> time.time()
1560604558.572602
  1. mktime(t):
    将一个struct_time转化为时间戳
>>> time.mktime(time.localtime())
1560606197.0

2. 返回格式化时间串

  1. asctime([t]):
    把一个表示时间的元组或者struct_time表示为这种形式:‘Sun Jun 20 23:21:05 1993’。如果没有参数,将会将time.localtime()作为参数传入。
>>> time.asctime()
'Sat Jun 15 22:09:42 2019'
>>> time.asctime(time.gmtime(12345.0))
'Thu Jan  1 03:25:45 1970'
  1. ctime([secs]):
    把一个时间戳(按秒计算的浮点数)转化为time.asctime()的形式。如果参数未给或者为None的时候,将会默认time.time()为参数。它的作用相当于time.asctime(time.localtime(secs))。
>>> time.ctime()
'Sat Jun 15 22:11:09 2019'
>>> time.ctime(12345.0)
'Thu Jan  1 11:25:45 1970'
  1. strftime(format[, t]):
    参数说明:
    format – 格式字符串。
    t – 可选的参数t是一个struct_time对象

把一个struct_time对象转化为格式化的时间字符串。如果t未指定,将传入time.localtime()。

>>> time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
'2019-06-15 22:16:46'
>>> time.strftime("%Y-%m-%d", time.localtime())
'2019-06-15'
>>> t = (2009, 2, 17, 17, 3, 38, 1, 48, 0)
>>>>  time.strftime("%Y-%m-%d",t)
  File "<stdin>", line 1
    time.strftime("%Y-%m-%d",t)
    ^
IndentationError: unexpected indent
>>> t1 = time.struct_time(t)
>>> t1
time.struct_time(tm_year=2009, tm_mon=2, tm_mday=17, tm_hour=17, tm_min=3, tm_sec=38, tm_wday=1, tm_yday=48, tm_isdst=0)
>>> time.strftime("%Y-%m-%d",t1)
'2009-02-17'
>>>

3. 返回struct_time

  1. localtime([secs]):
    将一个时间戳转换为当前时区的struct_time。secs参数未提供,则以当前时间为准。
>>> time.localtime()
time.struct_time(tm_year=2019, tm_mon=6, tm_mday=15, tm_hour=21, tm_min=54, tm_sec=27, tm_wday=5, tm_yday=166, tm_isdst=0)
>>> time.localtime(12345.0)
time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=11, tm_min=25, tm_sec=45, tm_wday=3, tm_yday=1, tm_isdst=0)
  1. gmtime([secs]):
    和localtime()方法类似,gmtime()方法是将一个时间戳转换为UTC时区(0时区)的struct_time
>>> time.gmtime(12345.0)
time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=3, tm_min=25, tm_sec=45, tm_wday=3, tm_yday=1, tm_isdst=0)
  1. strptime(string[, format]):
    把一个格式化时间字符串转化为struct_time。与strftime()是逆操作。
>>> time.strptime("2019-01-01", "%Y-%m-%d")
 time.struct_time(tm_year=2019, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=1, tm_yday=1, tm_isdst=-1)

4. 其他

  1. timesleep(secs):
    线程推迟指定的时间运行。单位为秒
import time
print("Start : %s" % time.ctime())
time.sleep(5)
print("End : %s" % time.ctime())

输出:

Start : Sat Jun 15 22:58:19 2019
End : Sat Jun 15 22:58:24 2019
  1. clock():
    在不同的系统上含义不同。在UNIX系统上,它返回的是“进程时间”,它是用秒表示的浮点数(时间戳)。而在WINDOWS中,第一次调用,返回的是进程运行的实际时间。而第二次之后的调用是自第一次调用以后到现在的运行时间。
    >>> time.clock()
    1.2307672110486793e-06
    >>> time.clock()
    2.377962866932731
    >>> time.clock()
    5.1665326497925745
    >>> time.clock()
    9.957191454865304
    >>> time.clock()
    67.49946174447304
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值