跳到主要内容

Python time 模块

提示
  1. 时间操作与转换:Python的time模块提供多种函数来处理时间,包括读取当前时间、格式化时间及暂停执行等。
  2. 时间戳与本地时间:使用time.time()获取自纪元以来的秒数,而time.ctime()将这些秒数转换为本地时间字符串。
  3. 结构化时间处理time模块中的localtime()gmtime()mktime()asctime()函数允许将时间戳转换为结构化时间,或者反过来进行转换。

Python的time模块提供了处理时间相关任务的函数。

这些时间相关的任务包括:

  • 读取当前时间
  • 格式化时间
  • 暂停指定秒数等等。

Python time.time()函数

在Python中,time()函数返回自纪元以来经过的秒数(时间的起点)。

对于Unix系统,1970年1月1日,00:00:00(UTC)是纪元。

让我们看一个示例,

# 导入time模块
import time

# 获取自纪元以来的当前时间(秒)
seconds = time.time()

print("自纪元以来的秒数 =", seconds)

# 输出:自纪元以来的秒数 = 1672214933.6804628

在上述示例中,我们使用time.time()函数获取了自纪元以来的当前时间(秒),然后打印了结果。

Python time.ctime()函数

Python中的time.ctime()函数以自纪元以来经过的秒数为参数,并返回代表本地时间的字符串。

import time

# 自纪元以来经过的秒数
seconds = 1672215379.5045543

# 将自纪元以来的时间(秒)转换为可读格式
local_time = time.ctime(seconds)

print("本地时间:", local_time)

输出

本地时间: Wed Dec 28 08:16:19 2022

在这里,我们使用了time.ctime()函数将自纪元以来的时间(秒)转换为可读格式,然后打印了结果。

Python time.sleep()函数

sleep()函数暂停(延迟)当前线程的执行,为给定的秒数。

import time

print("立即打印。")
time.sleep(2.4)
print("2.4秒后打印。")

输出

立即打印。
2.4秒后打印。

这个程序的工作原理是:

  • 打印"立即打印"
  • time.sleep(2.4)暂停执行2.4秒。
  • 打印"2.4秒后打印"

了解更多关于sleep()的信息,请访问:Python sleep()

Python time.localtime()函数

localtime()函数以自纪元以来经过的秒数为参数,并返回struct_time(一个包含与struct_time对应的9个元素的元组)的本地时间

import time

result = time.localtime(1672214933)
print("result:", result)
print("\nyear:", result.tm_year)
print("tm_hour:", result.tm_hour)

输出

result: time.struct_time(tm_year=2022, tm_mon=12, tm_mday=28, tm_hour=8, tm_min=8, tm_sec=53, tm_wday=2, tm_yday=362, tm_isdst=0)

year: 2022
tm_hour: 8

在这里,如果没有参数或传递了Nonelocaltime(),则使用time()返回的值。

Python time.gmtime()函数

gmtime()函数以自纪元以来经过的秒数为参数,并返回UTCstruct_time

import time

result = time.gmtime(1672214933)
print("result:", result)
print("\nyear:", result.tm_year)
print("tm_hour:", result.tm_hour)

输出

result: time.struct_time(tm_year=2022, tm_mon=12, tm_mday=28, tm_hour=8, tm_min=8, tm_sec=53, tm_wday=2, tm_yday=362, tm_isdst=0)

year: 2022
tm_hour: 8

在这里,如果没有参数或传递了Nonegmtime(),则使用time()返回的值。

Python time.mktime()函数

mktime()函数以struct_time(一个包含与struct_time对应的

9个元素的元组)为参数,并返回自纪元以来的秒数(本地时间)。

struct_time的结构如下:

(year, month, day, hour, minute, second, weekday, year day, daylight saving)

让我们看一个示例,

import time

time_tuple = (2022, 12, 28, 8, 44, 4, 4, 362, 0)

# 将time_tuple转换为自纪元以来的秒数
seconds = time.mktime(time_tuple)

print(seconds)

# 输出:1672217044.0

在这里,我们将time_tuple转换为自纪元以来的秒数。

Python time.asctime()函数

在Python中,asctime()函数以struct_time为参数,并返回代表它的字符串。

类似于mktime()time_tuple的结构如下:

(year, month, day, hour, minute, second, weekday, year day, daylight saving)

让我们看一个示例,

import time

t = (2022, 12, 28, 8, 44, 4, 4, 362, 0)

result = time.asctime(t)
print("Result:", result)

# 输出:Result: Fri Dec 28 08:44:04 2022

在这里,我们可以看到time.asctime()将时间元组转换为人类可读的字符串。

Python time.strftime()函数

strftime()函数以struct_time(或与之对应的元组)为参数,并基于使用的格式代码返回一个代表它的字符串。例如,

import time

named_tuple = time.localtime() # 获取struct_time
time_string = time.strftime("%m/%d/%Y, %H:%M:%S", named_tuple)

print(time_string)

输出

12/29/2022, 08:36:22

在这里,%Y%m%d%H等是格式代码。

  • %Y - 年份[0001,..., 2018, 2019,..., 9999]
  • %m - 月份[01, 02, ..., 11, 12]
  • %d - 天[01, 02, ..., 30, 31]
  • %H - 小时[00, 01, ..., 22, 23
  • %M - 分钟[00, 01, ..., 58, 59]
  • %S - 秒[00, 01, ..., 58, 61]

要了解更多,请访问:time.strftime()

Python time.strptime()函数

strptime()函数解析表示时间的字符串,并返回struct_time

import time

time_string = "14 July, 2023"
result = time.strptime(time_string, "%d %B, %Y")

print(result)

输出

time.struct_time(tm_year=2023, tm_mon=7, tm_mday=14, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=4, tm_yday=195, tm_isdst=-1)

在这里,strptime()解析字符串并将其转换为struct_time对象。