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 | 时区名称(如果不存在时区,则不包含字符) |
%% | 字面的 '%' 字符 |
写到最后,如果本文对你有所帮助的话,别忘记点赞哦 ~