在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ Python/ Python3日期和時(shí)間
Python3文件操作
Python3日期和時(shí)間
Python3基礎(chǔ)語(yǔ)法
Python3字典
Python3元組
Python3文件方法
Python3字符串
Python3引入什么新的東西?
Python3異常處理
Python3模塊
Python3數(shù)字
Python3變量類型
Python3函數(shù)
Python3循環(huán)
Python3 os文件目錄的方法
Python3 while循環(huán)語(yǔ)句
Python3斷言
Python3基本運(yùn)算符
Python3環(huán)境安裝設(shè)置
Python3標(biāo)準(zhǔn)異常
Python3嵌套循環(huán)
Python3教程
Python3決策
Python3 for循環(huán)語(yǔ)句
Python3列表

Python3日期和時(shí)間

Python程序可以通過多種方式來處理日期和時(shí)間。日期格式之間的轉(zhuǎn)換是計(jì)算機(jī)的一個(gè)共同核心。Python的時(shí)間和日歷模塊能夠幫助我們跟蹤的日期和時(shí)間。

什么是刻度?

時(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)
這將產(chǎn)生一個(gè)結(jié)果,如下 -
Number of ticks since 12:00am, January 1, 1970: 1455508609.34375 

日期計(jì)算使用刻度線是很容易做到的。 然而,新紀(jì)元日期之前不能以這種形式表示。在未來的一個(gè)日期也不能表示這種方式 - 分界點(diǎn)是在某個(gè)時(shí)候,在 UNIX和Windows上為 2038年。

什么是TimeTuple?

許多Python時(shí)間的函數(shù)處理時(shí)間9個(gè)數(shù)字的元組,如下圖所示 -
索引 字段
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())
這將產(chǎn)生一個(gè)結(jié)果如下 -
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)
上述元組相當(dāng)于struct_time結(jié)構(gòu)。這種結(jié)構(gòu)具有以下屬性 -
索引 屬性
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

獲取當(dāng)前時(shí)間

劃時(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)
這將產(chǎn)生以下結(jié)果,這可能會(huì)在任何其他像樣形式被格式化 -
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)

獲取格式化時(shí)間

可以在格式化任何時(shí)候按您的要求,但用來獲得時(shí)間可讀格式的簡(jiǎn)單方法是asctime() -
#!/usr/bin/python3
import time

localtime = time.asctime( time.localtime(time.time()) )
print ("Local current time :", localtime)
這將產(chǎn)生以下結(jié)果 -
Local current time : Mon Feb 15 09:34:03 2016

獲取一個(gè)月份的日歷

日歷模塊提供各種各樣年度和月度的日歷方法。在這里,我們將打印給定月份(2008年1月)的日歷 -

#!/usr/bin/python3
import calendar

cal = calendar.month(2016, 2)
print ("Here is the calendar:")
print (cal)
這將產(chǎn)生以下結(jié)果 -
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提供一個(gè)流行時(shí)間模塊,為時(shí)間和表示之間轉(zhuǎn)換提供了相應(yīng)函數(shù)。這里是所有可用方法的列表 -

SN 函數(shù)及描述
1

time.altzone

如果定義則在當(dāng)?shù)氐腄ST時(shí)區(qū)偏移,在UTC西部秒數(shù)。這是負(fù)值,如果當(dāng)?shù)氐腗ST時(shí)區(qū)是UTC以東 (在西歐,包括英國(guó)).只使用這個(gè),如果白天不為零。

2

time.asctime([tupletime])

接受時(shí)間元組,并返回一個(gè)可讀24個(gè)字符的字符串,如 'Tue Dec 11 18:07:14 2008'.

3

time.clock( )

返回當(dāng)前CPU時(shí)間為幾秒鐘的浮點(diǎn)數(shù)。要測(cè)量計(jì)算成本的不同的方法,time.clock()的值比time.time更有用。

4

time.ctime([secs])

類似 asctime(localtime(secs))和不帶參數(shù)的 ctime()函數(shù)
5

time.gmtime([secs])

接受從新紀(jì)元用秒表示瞬間和返回用UTC表示時(shí)間元組t。注: t.tm_isdst 總是為 0

6

time.localtime([secs])

接受從新紀(jì)元用秒表示瞬間和返回本地時(shí)間的時(shí)間元組t(t.tm_isdst 為 0 或 1, 根據(jù)DST是否通過局部規(guī)則適用于時(shí)刻秒)

7

time.mktime(tupletime)

接受表示為本地時(shí)間的時(shí)間元組并返回從新紀(jì)元以秒表示瞬間的一個(gè)浮點(diǎn)值
8

time.sleep(secs)

線程暫停secs秒調(diào)用
9

time.strftime(fmt[,tupletime])

接受表示為本地時(shí)間的時(shí)間元組瞬間,并返回表示由字符串指定 fmt 瞬間的字符串

10

解析 str 按照格式字符串格式化并返回時(shí)間元組格式的時(shí)間
11

time.time( )

返回當(dāng)前時(shí)刻,從新紀(jì)元以來的秒的浮點(diǎn)數(shù)
12

time.tzset()

通過重置庫(kù)例程使用時(shí)間轉(zhuǎn)換規(guī)則。 環(huán)境變量TZ指定如何做到這一點(diǎn)

讓我們通過簡(jiǎn)單的功能 -
下列是時(shí)間模塊提供兩個(gè)重要屬性:
SN
屬性及說明
1 time.timezone

屬性time.timezone是在本地時(shí)區(qū)以UTC的秒偏移量(不包括DST)

2 time.tzname
屬性time.tzname是一對(duì)區(qū)域設(shè)置相關(guān)的字符串

calendar模塊

日歷 calendar 模塊提供日歷相關(guān)的功能,包括函數(shù)用來打印給定月份或年份文本日歷。

默認(rèn)情況下,日歷采用星期一作為一周的第一天,周日是最后一個(gè)。如要改變這種情況,調(diào)用calendar.setfirstweekday()函數(shù)。

這里是 calendar 模塊可用的函數(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月)

其它模塊和功能:

如果你有興趣,那么在這里你會(huì)發(fā)現(xiàn)其他重要的模塊和功能的列表,包括在Python日期和時(shí)間的使用: