个性化阅读
专注于IT技术分析

python3时间time模块 – Python3教程

点击下载

上一章Python教程请查看:python3将时间戳转为日期,将日期转为时间戳

在本文中,我们将详细讨论time模块,我们将通过例子学习使用time模块中定义的不同的与时间相关的函数。

Python有一个名为time的模块来处理与时间相关的任务,要使用模块中定义的函数,首先需要导入模块。方法如下:

import time

以下是常用的与时间相关的函数。

Python time.time ()

函数的作用是:返回历元以来的秒数。

对于Unix系统,1970年1月1日00:00:00的UTC时间是epoch(时间开始的点)。

import time
seconds = time.time()
print("Seconds since epoch =", seconds) 

Python time.ctime ()

函数的作用是:以历元以来的秒为参数,返回一个表示本地时间的字符串。

import time
seconds = 1545925769.9618232
local_time = time.ctime(seconds)
print("Local time:", local_time)   

Python time.sleep ()

sleep()函数在给定的秒数内挂起(延迟)当前线程的执行。

import time
print("立即打印。")
time.sleep(2.4)
print("这是2.4秒后打印的。")

在讨论其他与时间相关的函数之前,让我们先研究一下时间,简单来说就是time.struct_time类。

time.struct_time

时间模块中的一些函数如gmtime()、asctime()等都需要时间,struct_time对象作为一个参数或返回它。

这是时间的一个例子,struct_time对象。

time.struct_time(tm_year=2020, tm_mon=12, tm_mday=27, 
    tm_hour=6, tm_min=35, tm_sec=17, 
    tm_wday=3, tm_yday=361, tm_isdst=0)
索引 属性
0 tm_year 0000, …., 2020, …, 9999
1 tm_mon 1, 2, …, 12
2 tm_mday 1, 2, …, 31
3 tm_hour 0, 1, …, 23
4 tm_min 0, 1, …, 59
5 tm_sec 0, 1, …, 61
6 tm_wday 0, 1, …, 6; Monday is 0
7 tm_yday 1, 2, …, 366
8 tm_isdst 0, 1 or -1

可以使用索引和属性访问time.struct_time对象的时间的值(元素)。

Python time.localtime ()

函数的作用是:以历元以来的秒数作为参数,在本地时间内返回struct_time。

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

如果没有参数传递给localtime(),则使用time()返回的值。

Python time.gmtime ()

函数的作用是:以历元以来的秒数作为参数,返回UTC中的struct_time。

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

如果没有参数传递给gmtime(),则使用time()返回的值。

Python time.mktime ()

mktime()函数将struct_time(或包含9个与struct_time对应的元素的元组)作为参数,并在本地时间中返回自epoch以来的秒数,基本上,它是localtime()的逆函数。

import time
t = (2020, 12, 28, 8, 44, 4, 4, 362, 0)
local_time = time.mktime(t)
print("Local time:", local_time)

下面的示例显示了mktime()和localtime()之间的关系。

import time
seconds = 1545925769
# returns struct_time
t = time.localtime(seconds)
print("t1: ", t)
# returns seconds from struct_time
s = time.mktime(t)
print("\s:", seconds)

Python time.asctime ()

asctime()函数将struct_time(或包含9个与struct_time对应的元素的元组)作为参数,并返回表示它的字符串。这里有一个例子:

import time
t = (2020, 12, 28, 8, 44, 4, 4, 362, 0)
result = time.asctime(t)
print("Result:", result)

Python time.strftime ()

函数的作用是:将struct_time(或与之对应的tuple)作为参数,并根据使用的格式代码返回表示它的字符串。例如,

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

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

  • %Y -year[0001,…]、2020、2019……,9999)
  • %m -month2,…11、12)
  • %d – day[01, 02,…]、30、31)
  • %H -hour[00,01,…22日,23日
  • %M –minute[0, 1…58,59]
  • %S -second[00,01,…],61)

Python time.strptime ()

函数的作用是:解析表示时间的字符串并返回struct_time。

import time
time_string = "21 June, 2020"
result = time.strptime(time_string, "%d %B, %Y")
print(result)
赞(0)
未经允许不得转载:srcmini » python3时间time模块 – Python3教程

评论 抢沙发

评论前必须登录!