教程集 www.jiaochengji.com
教程集 >  Python编程  >  Python入门  >  正文 Python time模块详解

Python time模块详解

发布时间:2021-12-21   编辑:jiaochengji.com
教程集为您提供Python time模块详解等资源,欢迎您收藏本站,我们将为您提供最新的Python time模块详解资源

time 模块主要包含各种提供日期、时间功能的类和函数。该模块既提供了把日期、时间格式化为字符串的功能,也提供了从字符串恢复日期、时间的功能。

在 Python 的交互式解释器中先导入 time 模块,然后输入 [e for e in dir(time) if not e.startswith('_')] 命令,即可看到该模块所包含的全部属性和函数:

<pre class="brush:html;toolbar:false">>>> [e for e in dir(time) if not e.startswith('_')] ['altzone', 'asctime', 'clock', 'ctime', 'daylight', 'get_clock_info', 'gmtime', 'localtime', 'mktime',  'monotonic',  'perf_counter', 'process_time', 'sleep', 'strftime', 'strptime', 'struct_time', 'time', 'timezone', 'tzname']</pre>

在 time 模块内提供了一个 time.struct_time 类,该类代表一个时间对象,它主要包含 9 个属性,每个属性的信息如下表所示:

<table><caption>表 1 time.struct_time 类中各属性的含义</caption><tbody><tr class="firstRow"><th>字段名</th><th>字段含义</th><th>值</th></tr><tr><td>tm_year</td><td>年</td><td>如 2017、2018 等</td></tr><tr><td>tm_mon</td><td>月</td><td>如 2、3 等,范围为 1~12</td></tr><tr><td>tm_mday</td><td>日</td><td>如 2、3 等,范围为 1~31</td></tr><tr><td>tm_hour</td><td>时</td><td>如 2、3 等,范围为 0~23</td></tr><tr><td>tm_min</td><td>分</td><td>如 2、3 等,范围为 0~59</td></tr><tr><td>tm_sec </td><td>秒</td><td>如 2、3 等,范围为 0~59</td></tr><tr><td>tm_wday</td><td>周</td><td>周一为 0,范围为 0~6</td></tr><tr><td>tm_yday</td><td> 一年内第几天</td><td>如 65,范围 1~366</td></tr><tr><td>tm_isdst</td><td>夏时令</td><td>0、1 或 -1</td></tr></tbody></table>

比如,Python 可以用 time.struct_time(tm_year=2018, tm_mon=5, tm_mday=2, tm_hour=8, tm_min=0, tm_sec=30, tm_wday=3, tm_yday=1, tm_isdst=0) 很清晰地代表时间。

此外,Python 还可以用一个包含 9 个元素的元组来代表时间,该元组的 9 个元素和 struct_time 对象中 9 个属性的含义是一一对应的。比如程序可以使用(2018, 5, 2, 8, 0, 30, 3, 1, 0)来代表时间。

在日期、时间模块内常用的功能函数如下:

time.asctime([t]):将时间元组或 struct_time 转换为时间字符串。如果不指定参数 t,则默认转换当前时间。

time.ctime([secs]):将以秒数代表的时间转换为时间宇符串。

time.gmtime([secs]):将以秒数代表的时间转换为 struct_time 对象。如果不传入参数,则使用当前时间。

time.localtime([secs]):将以秒数代表的时间转换为代表当前时间的 struct_time 对象。如果不传入参数,则使用当前时间。

time.mktime(t):它是 localtime 的反转函数,用于将 struct_time 对象或元组代表的时间转换为从 1970 年 1 月 1 日 0 点整到现在过了多少秒。

time.perf_counter():返回性能计数器的值。以秒为单位。

time.process_time():返回当前进程使用 CPU 的时间。以秒为单位。

time.sleep(secs):暂停 secs 秒,什么都不干。

time.strftime(format[, t]):将时间元组或 struct_time 对象格式化为指定格式的时间字符串。如果不指定参数 t,则默认转换当前时间。

time.strptime(string[, format]):将字符串格式的时间解析成 struct_time 对象。

time.time():返回从 1970 年 1 月 1 日 0 点整到现在过了多少秒。

time.timezone:返回本地时区的时间偏移,以秒为单位。

time.tzname:返回本地时区的名字。

下面程序示范了 time 棋块的功能函数:

<pre class="brush:html;toolbar:false">import time # 将当前时间转换为时间字符串 print(time.asctime()) # 将指定时间转换时间字符串,时间元组的后面3个元素没有设置 print(time.asctime((2018, 2, 4, 11, 8, 23, 0, 0 ,0))) # Mon Feb  4 11:08:23 2018 # 将以秒数为代表的时间转换为时间字符串 print(time.ctime(30)) # Thu Jan  1 08:00:30 1970 # 将以秒数为代表的时间转换为struct_time对象。 print(time.gmtime(30)) # 将当前时间转换为struct_time对象。 print(time.gmtime()) # 将以秒数为代表的时间转换为代表当前时间的struct_time对象 print(time.localtime(30)) # 将元组格式的时间转换为秒数代表的时间 print(time.mktime((2018, 2, 4, 11, 8, 23, 0, 0 ,0))) # 1517713703.0 # 返回性能计数器的值 print(time.perf_counter()) # 返回当前进程使用CPU的时间 print(time.process_time()) #time.sleep(10) # 将当前时间转换为指定格式的字符串 print(time.strftime('%Y-%m-%d %H:%M:%S')) st = '2018年3月20日' # 将指定时间字符串恢复成struct_time对象。 print(time.strptime(st, '%Y年%m月%d日')) # 返回从1970年1970年1月1日0点整到现在过了多少秒。 print(time.time()) # 返回本地时区的时间偏移,以秒为单位 print(time.timezone) # 在国内东八区输出-28800</pre>

运行上面程序,可以看到如下输出结果:

<pre class="brush:html;toolbar:false">Fri Feb 22 11:28:39 2019 Mon Feb  4 11:08:23 2018 Thu Jan  1 08:00:30 1970 time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=30, tm_wday=3, tm_yday=1,  tm_isdst=0) time.struct_time(tm_year=2019, tm_mon=2, tm_mday=22, tm_hour=3, tm_min=28, tm_sec=39, tm_wday=4, tm_yday=53,  tm_isdst=0) time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=8, tm_min=0, tm_sec=30, tm_wday=3, tm_yday=1,  tm_isdst=0) 1517713703.0 0.0 0.140625 2019-02-22 11:28:39 time.struct_time(tm_year=2018, tm_mon=3, tm_mday=20, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=1, tm_yday=79,  tm_isdst=-1) 1550806119.4960592 -28800</pre>

time 模块中的 strftime() 和 strptime() 两个函数互为逆函数,其中 strftime() 用于将 struct_time 对象或时间元组转换为时间字符串;而 strptime() 函数用于将时间字符串转换为 struct_time 对象。这两个函数都涉及编写格式模板,比如上面程序中使用 %Y 代表年、%m 代表月、%d 代表日、%H 代表时、%M 代表分、%S 代表秒。这两个函数所需要的时间格式字符串支持的指令如下表所示:

<table style="width: 828px;"><tbody><tr class="firstRow"><th style="word-break: break-all;">

<span style="font-weight: 400; white-space: nowrap;">指</span>

<span style="font-weight: 400; white-space: nowrap;">
</span>

<span style="font-weight: 400; white-space: nowrap;">令</span>

</th><th style="word-break: break-all;">

<span style="font-weight: 400; white-space: nowrap;">
</span>

<span style="font-weight: 400; white-space: nowrap;">含义</span>

</th></tr><tr><td style="word-break: break-all;"><span style="white-space: nowrap;">%a</span></td><td style="word-break: break-all;"><span style="white-space: nowrap;">本地化的星期几的缩写名,比如 Sun 代表星期天</span></td></tr><tr><td style="word-break: break-all;"><span style="white-space: nowrap;">%A</span></td><td style="word-break: break-all;"><span style="white-space: nowrap;">本地化的星期几的完整名</span></td></tr><tr><td style="word-break: break-all;"><span style="white-space: nowrap;">%b</span></td><td><span style="white-space: nowrap;">本地化的月份的缩写名,比如 Jan 代表一月</span></td></tr><tr><td><span style="white-space: nowrap;">%B</span></td><td><span style="white-space: nowrap;">本地化的月份的完整名</span></td></tr><tr><td><span style="white-space: nowrap;">%c</span></td><td><span style="white-space: nowrap;">本地化的日期和时间的表示形式</span></td></tr><tr><td><span style="white-space: nowrap;">%d</span></td><td><span style="white-space: nowrap;">代表一个月中第几天的数值,范固: 01~31</span></td></tr><tr><td><span style="white-space: nowrap;">%H</span></td><td><span style="white-space: nowrap;">代表 24 小时制的小时,范围:00~23</span></td></tr><tr><td><span style="white-space: nowrap;">%I</span></td><td><span style="white-space: nowrap;">代表 12 小时制的小时,范围:01~12</span></td></tr><tr><td><span style="white-space: nowrap;">%j</span></td><td><span style="white-space: nowrap;">一年中第几天,范围:001~366</span></td></tr><tr><td><span style="white-space: nowrap;">%m</span></td><td><span style="white-space: nowrap;">代表月份的数值,范围:01~12</span></td></tr><tr><td><span style="white-space: nowrap;">%M</span></td><td><span style="white-space: nowrap;">代表分钟的数值,范围:00~59</span></td></tr><tr><td><span style="white-space: nowrap;">%p</span></td><td><span style="white-space: nowrap;">上午或下午的本地化方式。当使用 strptime() 函数并使用 %I 指令解析小时时,%p 只影响小时字段</span></td></tr><tr><td><span style="white-space: nowrap;">%S</span></td><td><span style="white-space: nowrap;">代表分钟的数值,范围:00~61。该范围确实是 00~61,60 在表示闰秒的时间戳时有效,而 61 则是由于一些历史原因造成的</span></td></tr><tr><td><span style="white-space: nowrap;">%U</span></td><td style="word-break: break-all;">

<span style="white-space: nowrap;">代表一年中表示第几周,以星期天为每周的第一天,范围:00~53。在这种方式下,一年中第一个星期天被认为处于第一周</span>

<span style="white-space: nowrap;">。当使用 strptime() 函数解析时间字符串时,只有同时指定了星期几和年份该指令才会有效</span>

</td></tr><tr><td><span style="white-space: nowrap;">%w</span></td><td><span style="white-space: nowrap;">代表星期几的数值,范围:0~6,其中 0 代表周日</span></td></tr><tr><td><span style="white-space: nowrap;">%W</span></td><td style="word-break: break-all;">

<span style="white-space: nowrap;">代表一年小第几周,以星期一为每周的第一天,范围:00~53。在这种方式下,一年中第一个星期一被认为处于第一周</span>

<span style="white-space: nowrap;">。当使用 strptime() 函数解析时间字符串时,只有同时指定了星期几和年份该指令才会有效</span>

</td></tr><tr><td><span style="white-space: nowrap;">%x</span></td><td><span style="white-space: nowrap;">本地化的日期的表示形式</span></td></tr><tr><td><span style="white-space: nowrap;">%X</span></td><td><span style="white-space: nowrap;">本地化的时间的表示形式</span></td></tr><tr><td><span style="white-space: nowrap;">%y</span></td><td><span style="white-space: nowrap;">年份的缩写,范围:00~99,比如 2018 年就简写成 18</span></td></tr><tr><td><span style="white-space: nowrap;">%Y</span></td><td><span style="white-space: nowrap;">年份的完整形式。如 2018</span></td></tr><tr><td><span style="white-space: nowrap;">%z</span></td><td><span style="white-space: nowrap;">显示时区偏移</span></td></tr><tr><td><span style="white-space: nowrap;">%Z</span></td><td><span style="white-space: nowrap;">时区名(如果时区不行在,则显示为空)</span></td></tr><tr><td><span style="white-space: nowrap;">%%</span></td><td><span style="white-space: nowrap;">用于代表%符号</span></td></tr></tbody></table>

您可能感兴趣的文章:
python怎么查看安装的模块有哪些
python导入模块的关键字是什么
python中time模块需要安装么
Python解析nginx日志示例
python setup是什么
时间戳在Python3中转换为指定格式日期的方法
Python time时间模块用法详解
python代码块是什么
python怎么播放音乐
python如何实现读秒功能

[关闭]
~ ~