Python中的时间函数?
Python 提供了使用“time”模块以多种方式读取、表示和重置时间信息的库。日期、时间和日期时间在 Python 中是对象,因此无论何时对它们进行任何操作,实际上操作的是对象,而不是字符串或时间戳。
在本节中,我们将讨论“time”模块,它允许我们处理时间上的各种操作。
time 模块遵循“纪元”约定,该约定指的是时间开始的点。在 Unix 系统中,“纪元”时间从 1970 年 1 月 1 日凌晨 12:00 开始,持续到 2038 年。
要在您的系统上确定纪元时间值,只需键入以下代码 -
>>> 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中的Tick?
Tick 指的是一个时间间隔,它是一个浮点数,以秒为单位测量。有时我们得到的时间是夏令时 (DST),在夏令时期间,时钟向前移动 1 小时,秋季再向后移动。
Python 时间模块中最常用的函数 -
1. time.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 函数来计算两点之间经过的挂钟时间。
以下是计算挂钟时间的程序
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 模块中唯一存在的 数据结构。它具有命名的元组接口,可以通过索引或属性名称访问。
语法
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 格式返回本地(非夏令时)时区的偏移量。
>>> time.timezone -19800
2. time.tzname – 它返回一个包含本地非夏令时和夏令时区的元组。
>>> time.tzname
('India Standard Time', 'India Daylight Time')
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP