時(shí)間間隔以秒為單位的浮點(diǎn)數(shù)。特別是在時(shí)間瞬間在自1970年1月1日(紀(jì)元)12點(diǎn)的表示。
在Python提供一個(gè)流行時(shí)間 time 模塊,提供時(shí)間的的 函數(shù)功能,并可轉(zhuǎn)換表示。函數(shù) time.time()返回當(dāng)前系統(tǒng)時(shí)間,從1970年1月1日12點(diǎn)起來的秒數(shù)。
#!/usr/bin/python3
import time; # This is required to include time module.
ticks = time.time()
print ("Number of ticks since 12:00am, January 1, 1970:", ticks)
Number of ticks since 12:00am, January 1, 1970: 1455508609.34375
日期計(jì)算使用刻度線是很容易做到的。 然而,新紀(jì)元日期之前不能以這種形式表示。在未來的一個(gè)日期也不能表示這種方式 - 分界點(diǎn)是在某個(gè)時(shí)候,在 UNIX和Windows上為 2038年。
| 索引 | 字段 | 值 |
|---|---|---|
| 0 | 4-digit year | 2016 |
| 1 | Month | 1 to 12 |
| 2 | Day | 1 to 31 |
| 3 | Hour | 0 to 23 |
| 4 | Minute | 0 to 59 |
| 5 | Second | 0 to 61 (60 or 61 are leap-seconds) |
| 6 | Day of Week | 0 to 6 (0 is Monday) |
| 7 | Day of year | 1 to 366 (Julian day) |
| 8 | Daylight savings | -1, 0, 1, -1 means library determines DST |
示例
>>> print (time.localtime())
time.struct_time(tm_year=2016, tm_mon=3, tm_mday=15, tm_hour=9, tm_min=29, tm_sec=2, tm_wday=0, tm_yday=46, tm_isdst=0)
| 索引 | 屬性 | 值 |
|---|---|---|
| 0 | tm_year | 2016 |
| 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 (0 is Monday) |
| 7 | tm_yday | 1 to 366 (Julian day) |
| 8 | tm_isdst | -1, 0, 1, -1 means library determines DST |
劃時(shí)代浮點(diǎn)值從秒時(shí)刻轉(zhuǎn)換生成為時(shí)間元組,浮點(diǎn)值傳遞給一個(gè)函數(shù)返回時(shí)間的元組并帶上所有九個(gè)項(xiàng)目無效(例如,本地時(shí)間)。
#!/usr/bin/python3
import time
localtime = time.localtime(time.time())
print ("Local current time :", localtime)
Local current time : time.struct_time(tm_year=2016, tm_mon=2, tm_mday=15, tm_hour=9, tm_min=29, tm_sec=2, tm_wday=0, tm_yday=46, tm_isdst=0)
#!/usr/bin/python3
import time
localtime = time.asctime( time.localtime(time.time()) )
print ("Local current time :", localtime)
Local current time : Mon Feb 15 09:34:03 2016
日歷模塊提供各種各樣年度和月度的日歷方法。在這里,我們將打印給定月份(2008年1月)的日歷 -
#!/usr/bin/python3
import calendar
cal = calendar.month(2016, 2)
print ("Here is the calendar:")
print (cal)
Here is the calendar: February 2016 Mo Tu We Th Fr Sa Su 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
Python提供一個(gè)流行時(shí)間模塊,為時(shí)間和表示之間轉(zhuǎn)換提供了相應(yīng)函數(shù)。這里是所有可用方法的列表 -
| SN | 函數(shù)及描述 |
|---|---|
| 1 |
如果定義則在當(dāng)?shù)氐腄ST時(shí)區(qū)偏移,在UTC西部秒數(shù)。這是負(fù)值,如果當(dāng)?shù)氐腗ST時(shí)區(qū)是UTC以東 (在西歐,包括英國(guó)).只使用這個(gè),如果白天不為零。 |
| 2 |
接受時(shí)間元組,并返回一個(gè)可讀24個(gè)字符的字符串,如 'Tue Dec 11 18:07:14 2008'. |
| 3 |
返回當(dāng)前CPU時(shí)間為幾秒鐘的浮點(diǎn)數(shù)。要測(cè)量計(jì)算成本的不同的方法,time.clock()的值比time.time更有用。 |
| 4 |
類似 asctime(localtime(secs))和不帶參數(shù)的 ctime()函數(shù)
|
| 5 |
接受從新紀(jì)元用秒表示瞬間和返回用UTC表示時(shí)間元組t。注: t.tm_isdst 總是為 0 |
| 6 |
接受從新紀(jì)元用秒表示瞬間和返回本地時(shí)間的時(shí)間元組t(t.tm_isdst 為 0 或 1, 根據(jù)DST是否通過局部規(guī)則適用于時(shí)刻秒) |
| 7 |
接受表示為本地時(shí)間的時(shí)間元組并返回從新紀(jì)元以秒表示瞬間的一個(gè)浮點(diǎn)值
|
| 8 |
線程暫停secs秒調(diào)用
|
| 9 |
time.strftime(fmt[,tupletime]) 接受表示為本地時(shí)間的時(shí)間元組瞬間,并返回表示由字符串指定 fmt 瞬間的字符串 |
| 10 |
解析 str 按照格式字符串格式化并返回時(shí)間元組格式的時(shí)間
|
| 11 |
返回當(dāng)前時(shí)刻,從新紀(jì)元以來的秒的浮點(diǎn)數(shù)
|
| 12 |
通過重置庫(kù)例程使用時(shí)間轉(zhuǎn)換規(guī)則。 環(huán)境變量TZ指定如何做到這一點(diǎn) |
| SN |
屬性及說明
|
|---|---|
| 1 |
time.timezone
屬性time.timezone是在本地時(shí)區(qū)以UTC的秒偏移量(不包括DST) |
| 2 |
time.tzname
屬性time.tzname是一對(duì)區(qū)域設(shè)置相關(guān)的字符串
|
默認(rèn)情況下,日歷采用星期一作為一周的第一天,周日是最后一個(gè)。如要改變這種情況,調(diào)用calendar.setfirstweekday()函數(shù)。
| SN | 方法及描述 |
|---|---|
| 1 |
calendar.calendar(year,w=2,l=1,c=6)
返回一個(gè)多行字符串使用格式化成用c空格分隔的三列逐年的日歷 |
| 2 |
calendar.firstweekday( )
返回每周工作日開始的當(dāng)前設(shè)置。默認(rèn)情況下,當(dāng)日歷被首次導(dǎo)入是0,這意味著星期一 |
| 3 |
calendar.isleap(year)
如果 year 是閏年返回True;否則為false |
| 4 |
calendar.leapdays(y1,y2)
返回跨越潤(rùn)年在范圍內(nèi)(y1,y2)的總數(shù)
|
| 5 |
calendar.month(year,month,w=2,l=1)
返回一個(gè)多行字符串以及逐年月月份的日歷,每周一行加上兩個(gè)標(biāo)題行。 |
| 6 |
calendar.monthcalendar(year,month)
返回列表的列表整數(shù)。
|
| 7 |
calendar.monthrange(year,month)
返回兩個(gè)整數(shù)。 |
| 8 |
calendar.prcal(year,w=2,l=1,c=6)
類似打印 calendar.calendar(year,w,l,c). |
| 9 |
calendar.prmonth(year,month,w=2,l=1)
類似打印 calendar.month(year,month,w,l). |
| 10 |
calendar.setfirstweekday(weekday)
設(shè)定每個(gè)星期的第一天工作日代碼。工作日代碼是0(星期一)至6(星期日)
|
| 11 |
calendar.timegm(tupletime)
time.gmtime反轉(zhuǎn):接受時(shí)間元組形式的時(shí)刻,并返回同一時(shí)刻作為自新紀(jì)元以來秒的浮點(diǎn)數(shù) |
| 12 |
calendar.weekday(year,month,day)
返回給定日期是星期幾代碼。 工作日代碼是0(星期一)至60(星期日);月數(shù)是1(1月)到12(12月) |