Python定义了 单元 ,“时间”,它允许我们处理与时间有关的各种操作,它的转换和表示,在生活中的各种应用中找到了它的用途。时间的开始是从测量开始的 1970年1月1日上午12:00 这一次被称为“ 纪元 “在Python中。
准时操作:
1.时间 :-此函数用于计算 从大纪元开始几秒钟过去了 . 2.gmtime(秒) :-此函数返回 结构有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()) |
输出:
Seconds elapsed since the epoch are : 1470121951.9536893Time 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.时间(“时间”) :-此函数接受gmtime()生成的时间属性字符串,并返回 表示时间的24个字符的字符串 . 4.时间(秒) :-此函数返回 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 2016Time calculated using ctime() is : Tue Aug 2 07:47:02 2016
5.睡眠(秒) :-此方法用于 停止程序执行 在参数中指定的时间内。
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 2016Stop Execution : Tue Aug 2 07:59:07 2016
本文由 曼吉星 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。