Python中的时间函数 |设置 1 (time(), ctime(), sleep()…)
Python定义了一个模块,“时间”,它允许我们处理关于时间的各种操作、它的转换和表示,这些操作在生活中的各种应用程序中都有使用。时间的开始是从1970 年 1 月 1 日凌晨 12:00开始测量的,这个时间在Python中被称为“纪元”。
准时操作:
1. time() :- 此函数用于计算自 epoch 以来经过的秒数。 2. gmtime(sec) :- 该函数返回一个结构,其中包含 9 个值,每个值依次代表一个时间属性。它将秒转换为时间属性(天、年、月等) ,直到从纪元开始的指定秒数。如果没有提到秒,则计算时间直到现在。结构属性表如下。
Index Attributes Values
0 tm_year 2008
1 tm_mon 1 to 12
2 tm_mday 1 to 31
3 tm_hour 0 to 23
4 tm_min 0 to 59
5 tm_sec 0 to 61 (60 or 61 are leap-seconds)
6 tm_wday 0 to 6
7 tm_yday 1 to 366
8 tm_isdst -1, 0, 1 where -1 means
Library determines DST
Python3
# Python code to demonstrate the working of
# time() and gmtime()
# importing "time" module for time operations
import time
# using time() to display time since epoch
print ("Seconds elapsed since the epoch are : ",end="")
print (time.time())
# using gmtime() to return the time attribute structure
print ("Time calculated acc. to given seconds is : ")
print (time.gmtime())
Python3
# Python code to demonstrate the working of
# asctime() and ctime()
# importing "time" module for time operations
import time
# initializing time using gmtime()
ti = time.gmtime()
# using asctime() to display time acc. to time mentioned
print ("Time calculated using asctime() is : ",end="")
print (time.asctime(ti))
# using ctime() to display time string using seconds
print ("Time calculated using ctime() is : ", end="")
print (time.ctime())
Python3
# Python code to demonstrate the working of
# sleep()
# importing "time" module for time operations
import time
# using ctime() to show present time
print ("Start Execution : ",end="")
print (time.ctime())
# using sleep() to hault execution
time.sleep(4)
# using ctime() to show present time
print ("Stop Execution : ",end="")
print (time.ctime())
输出:
Seconds elapsed since the epoch are : 1470121951.9536893
Time calculated acc. to given seconds is :
time.struct_time(tm_year=2016, tm_mon=8, tm_mday=2,
tm_hour=7, tm_min=12, tm_sec=31, tm_wday=1,
tm_yday=215, tm_isdst=0)
3. asctime(“time”) :- 该函数采用由 gmtime() 生成的时间属性字符串,并返回一个表示时间的 24 个字符的字符串。 4. ctime(sec) :- 此函数返回一个24字符的时间字符串,但以秒为参数并计算时间,直到提到的秒。如果没有传递参数,则计算时间直到现在。
Python3
# Python code to demonstrate the working of
# asctime() and ctime()
# importing "time" module for time operations
import time
# initializing time using gmtime()
ti = time.gmtime()
# using asctime() to display time acc. to time mentioned
print ("Time calculated using asctime() is : ",end="")
print (time.asctime(ti))
# using ctime() to display time string using seconds
print ("Time calculated using ctime() is : ", end="")
print (time.ctime())
输出:
Time calculated using asctime() is : Tue Aug 2 07:47:02 2016
Time calculated using ctime() is : Tue Aug 2 07:47:02 2016
5. sleep(sec) :- 此方法用于在参数中指定的时间内停止程序执行。
Python3
# Python code to demonstrate the working of
# sleep()
# importing "time" module for time operations
import time
# using ctime() to show present time
print ("Start Execution : ",end="")
print (time.ctime())
# using sleep() to hault execution
time.sleep(4)
# using ctime() to show present time
print ("Stop Execution : ",end="")
print (time.ctime())
输出:
Start Execution : Tue Aug 2 07:59:03 2016
Stop Execution : Tue Aug 2 07:59:07 2016