Python中的时间函数?

Python通过使用“时间”模块以多种方式提供了读取,表示和重置时间信息的库。日期,时间和日期时间是Python中的对象,因此无论何时对它们进行任何操作,我们实际上都在操纵对象,而不是字符串或时间戳。

在本节中,我们将讨论“时间”模块,该模块使我们能够按时处理各种操作。

时间模块遵循“ EPOCH”约定,该约定指的是时间开始的时间点。在Unix系统中,“ EPOCH”的时间从1970年1月1日上午12:00到2038年。

要确定系统上的EPOCH时间值,只需在下面的代码中输入-

>>> import time
>>> time.gmtime(0)

输出结果

time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=0,
tm_min=0, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=0)

勾选python吗?

滴答是指时间间隔,它是一个以秒为单位测量的浮点数。有时我们会获得夏时制(DST)的时间,夏令时将时钟向前移动1小时,秋天则向后移动。

Python时间模块中最常见的功能-

1. time.time()函数

time()是时候了模块的主要功能。它测量自纪元以来的秒数作为浮点值。

语法

time.time()

演示上述功能的程序:

import time
print("Number of seconds elapsed since the epoch are : ", time.time())

输出结果

Number of seconds elapsed since the epoch are : 1553262407.0398576

我们可以使用python time函数来计算两点之间经过的Wall-clock时间。

下面是计算挂钟时间的程序:

import time
start = time.time()
print("Time elapsed on working...")
time.sleep(0.9)
end = time.time()
print("Time consumed in working: ",end - start)

输出结果

Time elapsed on working...
Time consumed in working: 0.9219651222229004

2. time.clock()函数

time.clock()函数返回处理器时间。它用于性能测试/基准测试。

语法

time.clock()

clock()函数返回程序花费的正确时间,并且比相应程序更准确。

让我们使用上面两个时间函数(如上 )编写一个程序来区分:

import time
template = 'time()# {:0.2f}, clock()# {:0.2f}'
print(template.format(time.time(), time.clock()))
for i in range(5, 0, -1):
   print('---Sleeping for: ', i, 'sec.')
time.sleep(i)
print(template.format(time.time(), time.clock())
)

输出结果

time()# 1553263728.08, clock()# 0.00
---Sleeping for: 5 sec.
time()# 1553263733.14, clock()# 5.06
---Sleeping for: 4 sec.
time()# 1553263737.25, clock()# 9.17
---Sleeping for: 3 sec.
time()# 1553263740.30, clock()# 12.22
---Sleeping for: 2 sec.
time()# 1553263742.36, clock()# 14.28
---Sleeping for: 1 sec.
time()# 1553263743.42, clock()# 15.34

3. time.ctime()函数

time.time()函数以“自纪元开始的秒数”作为输入时间,并根据本地时间转换为人类可读的字符串值。如果未传递任何参数,则返回当前时间。

import time
print('The current local time is :', time.ctime())
newtime = time.time() + 60
print('60 secs from now :', time.ctime(newtime))

输出结果

The current local time is : Fri Mar 22 19:43:11 2019
60 secs from now : Fri Mar 22 19:44:11 2019

4. time.sleep()函数

time.sleep()函数在指定的秒数内暂停当前线程的执行。传递浮点值作为输入以获得更精确的睡眠时间。

sleep()在需要等待文件完成关闭或让数据库提交发生的情况下,可以使用该函数。

import time
# using ctime() to display present time
print ("Time starts from : ",end="")
print (time.ctime())
# using sleep() to suspend execution
print ('Waiting for 5 sec.')
time.sleep(5)
# using ctime() to show present time
print ("Time ends at : ",end="")
print (time.ctime())

输出结果

Time starts from : Fri Mar 22 20:00:00 2019
Waiting for 5 sec.
Time ends at : Fri Mar 22 20:00:05 2019

5. time.struct_time类

time.struct_time是时间模块中存在的唯一数据结构。它具有一个命名的元组接口,可以通过索引或属性名称进行访问。

语法

time.struct_time

当您需要访问日期的特定字段时,此类非常有用。

此类提供了许多函数,例如和localtime()gmtime()并返回struct_time对象。

import time
print(' Current local time:', time.ctime())
t = time.localtime()
print('Day of month:', t.tm_mday)
print('Day of week :', t.tm_wday)
print('Day of year :', t.tm_yday)

输出结果

Current local time: Fri Mar 22 20:10:25 2019
Day of month: 22
Day of week : 4
Day of year : 81

6. time.strftime()函数

此函数在第二个参数中接受一个元组或struct_time,并按照第一个参数中指定的格式转换为字符串。

语法

time.strftime()

以下是实现time.strftime()函数的程序-

import time
now = time.localtime(time.time())
print("Current date time is: ",time.asctime(now))
print(time.strftime("%y/%m/%d %H:%M", now))
print(time.strftime("%a %b %d", now))
print(time.strftime("%c", now))
print(time.strftime("%I %p", now))
print(time.strftime("%Y-%m-%d %H:%M:%S %Z", now))

输出结果

Current date time is: Fri Mar 22 20:13:43 2019
19/03/22 20:13
Fri Mar 22
Fri Mar 22 20:13:43 2019
08 PM
2019-03-22 20:13:43 India Standard Time

在python中检查时区

有两个时间属性可为您提供时区信息-

1. time.timezone

它以UTC格式返回本地(非DST)时区的偏移量。

>>> time.timezone
-19800

2. time.tzname –它返回一个包含本地非DST和DST时区的元组。

>>> time.tzname
('India Standard Time', 'India Daylight Time')