Python time 模块,一文搞定!!!

本文详细介绍了Python内置的time模块,包括时间的两种标准表示形式:时间戳和时间元组。时间戳是以秒或纳秒为单位自特定时间点以来的偏移量,而时间元组则包含年、月、日等9个整数。time模块提供了如time(), localtime(), gmtime(), asctime(), ctime(), mktime(), strftime(), strptime()等函数来操作和转换时间。此外,文章还列举了时间格式化字符的映射表,并通过示例展示了如何使用这些函数。

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

Python 版本: 3.8

系统:Ubuntu 20.04

时间的标准表示形式

首先看看time模块的内置文档

import time

print(time.__doc__)

# This module provides various functions to manipulate time values.

# There are two standard representations of time.  One is the number
# of seconds since the Epoch, in UTC (a.k.a. GMT).  It may be an integer
# or a floating point number (to represent fractions of seconds).
# The Epoch is system-defined; on Unix, it is generally January 1st, 1970.
# The actual value can be retrieved by calling gmtime(0).

# The other representation is a tuple of 9 integers giving local time.
# The tuple items are:
#   year (including century, e.g. 1998)
#   month (1-12)
#   day (1-31)
#   hours (0-23)
#   minutes (0-59)
#   seconds (0-59)
#   weekday (0-6, Monday is 0)
#   Julian day (day in the year, 1-366)
#   DST (Daylight Savings Time) flag (-1, 0 or 1)
# If the DST flag is 0, the time is given in the regular time zone;
# if it is 1, the time is given in the DST time zone;
# if it is -1, mktime() should guess based on the date and time.

用Google在线翻译翻译一下

此模块提供各种函数来操作时间值。

时间有两个标准表示形式。 一个是数字
自时代以来的秒数,在 UTC(即格林尼治标准时间)。 它可以是一个整数
或浮点数(表示秒数)。
时代是系统定义的;在 Unix 上, 它通常是 1970 年 1 月 1 日。
可以通过调用 gmtime(0) 检索实际值。

另一个表示形式是给出本地时间的 9 个整数的元组。
元组项是:
  年(包括世纪,例如1998年)
  月 (1-12)
  日 (1-31)
  小时 (0-23)
  分钟 (0-59)
  秒 (0-59)
  工作日 (0-6, 星期一是 0)
  朱利安日(一年中的一天,1-366)
  DST(夏令时)标志(-1、0 或 1)
如果 DST 标志为 0,则在常规时区中给出时间;如果 DST 标志为 0,则在常规时区中给出时间。
如果是 1,则时间在 DST 时区中;如果为 1,则在 DST 时区中给出时间。
如果是 -1,mktime() 应该根据日期和时间进行猜测。

时间有两个标准表示形式。 一个是数字,另一个表示形式是给出本地时间的 9 个整数的元组。time 模块中提供了自定义时间格式的函数,这种自定义格式的时间可称之为格式化时间。

Tips:

  • 时间戳是相对于特定时间点的偏移秒数,通俗的讲就是从这个特定时间开始计时,时间走了几秒,这个特定时间可由函数 time.gmtime(0) 得到。
  • 时间元组的 9 个整数分别代表 年、月、日、小时、分、秒、周几和一年中的第几天。

再看看 time 模块有哪些函数

import time

for key, value in vars(time).items():
    value = value if 'built-in function' in str(value) else ''
    if value:
        print(value)

# <built-in function time>
# <built-in function time_ns>
# <built-in function clock_gettime>
# <built-in function clock_gettime_ns>
# <built-in function clock_settime>
# <built-in function clock_settime_ns>
# <built-in function clock_getres>
# <built-in function pthread_getcpuclockid>
# <built-in function sleep>
# <built-in function gmtime>
# <built-in function localtime>
# <built-in function asctime>
# <built-in function ctime>
# <built-in function mktime>
# <built-in function strftime>
# <built-in function strptime>
# <built-in function tzset>
# <built-in function monotonic>
# <built-in function monotonic_ns>
# <built-in function process_time>
# <built-in function process_time_ns>
# <built-in function thread_time>
# <built-in function thread_time_ns>
# <built-in function perf_counter>
# <built-in function perf_counter_ns>
# <built-in function get_clock_info>

函数有很多啊,但是我们常用的就那么几种:time, localtime, strftime


常用函数用法总结

time

t = time.time()
print(f'time {type(t)} {t}\n')

# time <class 'float'> 1607002512.9829454

time 函数直接生成当前时间的时间戳,不需要加参数。

time_ns

time_ns = time.time_ns()
print(f'time_ns {type(time_ns)} {time_ns}\n')

# time_ns <class 'int'> 1607002512982979169

time_ns 函数生成以纳秒为单位的时间戳。

gmtime

gmtime = time.gmtime()
gmtime_100 = time.gmtime(100)
print(f'gmtime {type(gmtime)} {gmtime}')
print(f'gmtime_100 {type(gmtime_100)} {gmtime_100}\n')

# gmtime <class 'time.struct_time'> time.struct_time(tm_year=2020, tm_mon=12, tm_mday=3, tm_hour=13, tm_min=35, tm_sec=12, tm_wday=3, tm_yday=338, tm_isdst=0)
# gmtime_100 <class 'time.struct_time'> time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=1, tm_sec=40, tm_wday=3, tm_yday=1, tm_isdst=0) 

不带参数的 gmtime 函数返回世界标准时间的时间元组(世界标准时间与北京时间相差8个小时)。

带参数后,返回以参数为偏移时间的时间元组,上面的例子,参数为 100,特定时间为 1970.01.01 00:00:00,偏移 100 秒后时间为 1970.01.01 00:01:40

localtime

# <built-in function localtime>
local_time = time.localtime()
local_time_10 = time.localtime(10)
print(f'localtime {type(local_time)} {local_time}')
print(f'localtime_10 {type(local_time_10)} {local_time_10}\n')

# localtime <class 'time.struct_time'> time.struct_time(tm_year=2020, tm_mon=12, tm_mday=3, tm_hour=21, tm_min=35, tm_sec=12, tm_wday=3, tm_yday=338, tm_isdst=0)
# localtime_10 <class 'time.struct_time'> time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=8, tm_min=0, tm_sec=10, tm_wday=3, tm_yday=1, tm_isdst=0)

localtime 函数不传参数返回本地时间(北京时间)的时间元组。
传参数 10 的话和 gmtime 函数效果一样,不过时间上加了 8 个小时,北京时间比世界标准时间快 8 个小时,所以输出的时间是 1970.01.01 08:00:10


asctime

asctime = time.asctime()
asctime_localtime = time.asctime(time.localtime())
asctime_localtime_10 = time.asctime(time.localtime(10))
print(f'asctime {type(asctime)} {asctime}')
print(f'asctime_10 {type(asctime_10)} {asctime_10}\n')

# asctime <class 'str'> Thu Dec  3 21:35:12 2020
# asctime_localtime <class 'str'> Thu Dec  3 21:35:12 2020
# asctime_localtime_10 <class 'str'> Thu Jan  1 08:00:10 1970

asctime 不带参数,返回本地时间的时间字符串(默认格式)。
可接收时间元组为参数,返回时间元组所表示的时间的时间字符串。


ctime

ctime = time.ctime()
ctime_10 = time.ctime(10)
print(f'ctime {type(ctime)} {ctime}')
print(f'ctime_10 {type(ctime_10)} {ctime_10}\n')

# ctime <class 'str'> Thu Dec  3 21:35:12 2020
# ctime_10 <class 'str'> Thu Jan  1 08:00:10 1970

ctime 功能和 asctime 函数一致,不过参数类型只能为整数。


mktime

mktime = time.mktime(time.localtime())
mktime_10 = time.mktime(time.localtime(10))
print(f'mktime {type(mktime)} {mktime}')
print(f'mktime_10 {type(mktime_10)} {mktime_10}\n')

# mktime <class 'float'> 1607002512.0
# mktime_10 <class 'float'> 10.0

mktime 将时间元组转化为时间戳。


strftime

time_fmt = '%Y-%m-%d %H:%M:%S'
strftime = time.strftime(time_fmt, time.localtime())
strftime_10 = time.strftime(time_fmt, time.localtime(10))
print(f'strftime {type(strftime)} {strftime}')
print(f'strftime_10 {type(strftime_10)} {strftime_10}\n')

# strftime <class 'str'> 2020-12-03 21:35:12
# strftime_10 <class 'str'> 1970-01-01 08:00:10

strftime 顾名思义,string format,输出为格式化时间,输入参数为格式化字符串和时间元组。
格式化字符映射(%Y,%m …)的含义在文末可查表。


strptime

time_fmt = '%Y-%m-%d %H:%M:%S'
strftime = time.strftime(time_fmt, time.localtime())
strftime_10 = time.strftime(time_fmt, time.localtime(10))

strptime = time.strptime(strftime, time_fmt)
strptime_10 = time.strptime(strftime_10, time_fmt)
print(f'strptime {type(strptime)} {strptime}')
print(f'strptime_10 {type(strptime_10)} {strptime_10}\n')

# strptime <class 'time.struct_time'> time.struct_time(tm_year=2020, tm_mon=12, tm_mday=3, tm_hour=21, tm_min=35, tm_sec=12, tm_wday=3, tm_yday=338, tm_isdst=-1)
# strptime_10 <class 'time.struct_time'> time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=8, tm_min=0, tm_sec=10, tm_wday=3, tm_yday=1, tm_isdst=-1)

strptime 函数将格式化时间转化为时间元组,功能和 strftime 相反。

从函数输出可以看出,time 模块所有函数的输出有三种:数字(时间戳)、time.struct_time 类(时间元组)和字符串(格式化时间)

小小地总结一下函数输出的时间类型:

  • time.time, time.time_ns, time.mktime 输出时间戳
  • time.gmtime, time.localtime, time.strptime 输出时间元组
  • time.ctime, time.strftime 输出格式化时间

格式化时间字段映射表
字段含义
%a本地化的缩写星期中每日的名称
%A本地化的星期中每日的完整名称
%b本地化的月缩写名称
%B本地化的月完整名称
%c本地化的适当日期和时间表示
%d十进制数 [01,31] 表示的月中日
%H十进制数 [00,23] 表示的小时(24小时制)
%I十进制数 [01,12] 表示的小时(12小时制)
%j十进制数 [001,366] 表示的年中日
%m十进制数 [01,12] 表示的月
%M十进制数 [00,59] 表示的分钟
%p本地化的 AM 或 PM
%S十进制数 [00,61] 表示的秒
%U十进制数 [00,53] 表示的一年中的周数(星期日作为一周的第一天)作为。在第一个星期日之前的新年中的所有日子都被认为是在第0周
%w十进制数 [0(星期日),6] 表示的周中日
%W十进制数 [00,53] 表示的一年中的周数(星期一作为一周的第一天)作为。在第一个星期一之前的新年中的所有日子被认为是在第0周
%x本地化的适当日期表示
%X本地化的适当时间表示
%y十进制数 [00,99] 表示的没有世纪的年份
%Y十进制数表示的带世纪的年份
%z时区偏移以格式 +HHMM 或 -HHMM 形式的 UTC/GMT 的正或负时差指示,其中H表示十进制小时数字,M表示小数分钟数字 [-23:59, +23:59]
%Z时区名称(如果不存在时区,则不包含字符)
%%字面的 '%' 字符

写到最后,如果本文对你有所帮助的话,别忘记点赞哦 ~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值