什麼是刻度?
時間間隔以秒為單位的浮點數。特別是在時間瞬間在自1970年1月1日(紀元)12點的表示。
在Python提供一個流行時間 time 模組,提供時間的的 函數功能,並可轉換表示。函數 time.time()返回當前系統時間,從1970年1月1日12點起來的秒數。
示例
#!/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
日期計算使用刻度線是很容易做到的。 然而,新紀元日期之前不能以這種形式表示。在未來的一個日期也不能表示這種方式 - 分界點是在某個時候,在 UNIX和Windows上為 2038年。
什麼是TimeTuple?
| 索引 | 字段 | 值 |
|---|---|---|
| 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 |
獲取當前時間
劃時代浮點值從秒時刻轉換生成為時間元組,浮點值傳遞給一個函數返回時間的元組並帶上所有九個專案無效(例如,本地時間)。
#!/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
time模組
Python提供一個流行時間模組,為時間和表示之間轉換提供了相應函數。這裏是所有可用方法的列表 -
| SN | 函數及描述 |
|---|---|
| 1 |
如果定義則在當地的DST時區偏移,在UTC西部秒數。這是負值,如果當地的MST時區是UTC以東 (在西歐,包括英國).只使用這個,如果白天不為零。 |
| 2 |
接受時間元組,並返回一個可讀24個字元的字串,如 'Tue Dec 11 18:07:14 2008'. |
| 3 |
返回當前CPU時間為幾秒鐘的浮點數。要測量計算成本的不同的方法,time.clock()的值比time.time更有用。 |
| 4 |
類似 asctime(localtime(secs))和不帶參數的 ctime()函數
|
| 5 |
接受從新紀元用碼錶示瞬間和返回用UTC表示時間元組t。注: t.tm_isdst 總是為 0 |
| 6 |
接受從新紀元用碼錶示瞬間和返回本地時間的時間元組t(t.tm_isdst 為 0 或 1, 根據DST是否通過局部規則適用於時刻秒) |
| 7 |
接受表示為本地時間的時間元組並返回從新紀元以碼錶示瞬間的一個浮點值
|
| 8 |
線程暫停secs秒調用
|
| 9 |
time.strftime(fmt[,tupletime]) 接受表示為本地時間的時間元組瞬間,並返回表示由字串指定 fmt 瞬間的字串 |
| 10 |
time.strptime(str,fmt='%a %b %d %H:%M:%S %Y')
解析 str 按照格式字串格式化並返回時間元組格式的時間
|
| 11 |
返回當前時刻,從新紀元以來的秒的浮點數
|
| 12 |
通過重置庫例程使用時間轉換規則。 環境變數TZ指定如何做到這一點 |
| SN |
屬性及說明
|
|---|---|
| 1 |
time.timezone
屬性time.timezone是在本地時區以UTC的秒偏移量(不包括DST) |
| 2 |
time.tzname
屬性time.tzname是一對區域設置相關的字串
|
calendar模組
默認情況下,日曆採用星期一作為一周的第一天,周日是最後一個。如要改變這種情況,調用calendar.setfirstweekday()函數。
| SN | 方法及描述 |
|---|---|
| 1 |
calendar.calendar(year,w=2,l=1,c=6)
返回一個多行字串使用格式化成用c空格分隔的三列逐年的日曆 |
| 2 |
calendar.firstweekday( )
返回每週工作日開始的當前設置。默認情況下,當日曆被首次導入是0,這意味著星期一 |
| 3 |
calendar.isleap(year)
如果 year 是閏年返回True;否則為false |
| 4 |
calendar.leapdays(y1,y2)
返回跨越潤年在範圍內(y1,y2)的總數
|
| 5 |
calendar.month(year,month,w=2,l=1)
返回一個多行字串以及逐年月月份的日曆,每週一行加上兩個標題行。 |
| 6 |
calendar.monthcalendar(year,month)
返回列表的列表整數。
|
| 7 |
calendar.monthrange(year,month)
返回兩個整數。 |
| 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)
設定每個星期的第一天工作日代碼。工作日代碼是0(星期一)至6(星期日)
|
| 11 |
calendar.timegm(tupletime)
time.gmtime反轉:接受時間元組形式的時刻,並返回同一時刻作為自新紀元以來秒的浮點數 |
| 12 |
calendar.weekday(year,month,day)
返回給定日期是星期幾代碼。 工作日代碼是0(星期一)至60(星期日);月數是1(1月)到12(12月) |
