Python 一篇文章看懂時間日期對象
一、時間對象time
time模塊使用的是C語言函數(shù)庫中的函數(shù)。只能處理1970/1/1到2038/12/31之間的數(shù)據(jù)。

1.測量運行時間方法
①process_time()
主要作用就是返回當前進程處理器運行時間
②perf_counter()
返回性能計算器
③monotonic()
返回單項時鐘
2.函數(shù)性能計算器
使用函數(shù)裝飾器結(jié)合time對象,測試排序算法的性能。
from random import *
import time
# -----------------------裝飾器函數(shù)用于計時-----------------------#
def timer(func):
def weapper(*s):
start=time.perf_counter()
func(*s)
end=time.perf_counter()
print("用時:\t\t",end-start)
return weapper
# -----------------------生成隨機列表-----------------------#
def randomlist():
return [randint(0,100) for x in range(10)]
# -----------------------冒泡排序-----------------------#
@timer
def sortA():
lis=randomlist()
print("隨機生成的序列:",lis)
i=0
while i<len(lis):
j=i+1
while j<len(lis)-i:
if lis[j]<lis[i]:
lis[i],lis[j]=lis[j],lis[i]
j+=1
i+=1
print("排序后的序列:\t",lis)
# -----------------------選擇排序-----------------------#
@timer
def sortB():
lis=randomlist()
print("隨機生成的序列:",lis)
i=0
while i<len(lis):
j=0
while j<len(lis)-1:
if lis[j]>lis[j+1]:
lis[j+1],lis[j]=lis[j],lis[j+1]
j+=1
i+=1
print("排序后的序列:\t",lis)
# -----------------------插入排序-----------------------#
'''
將未排序數(shù)列插入左側(cè)已排好隊的序列。
分析需要一個游標記錄應該排序的位置,一個臨時變量進行應該排序數(shù)據(jù)的臨時保存
'''
@timer
def sortC():
lis=randomlist()
print("隨機生成的序列:",lis)
for i in range(1,len(lis)):
temp = lis[i]
j=i
while lis[j-1]>temp:
lis[j]=lis[j-1]
j=j-1
if j==0:
break
lis[j]=temp
print("排序后的序列:\t",lis)
# -----------------------主函數(shù)-----------------------#
def main():
print("-----------------冒泡排序---------------------")
sortA()
print("-----------------選擇排序---------------------")
sortB()
print("-----------------插入排序---------------------")
sortC()
if __name__=="__main__":
main()
二、日期對象datetime
日期對象在處理字符串與日期對象的時候特別常用。所以呢咱們重點學習一下 Python中的字符串轉(zhuǎn)日期、日期轉(zhuǎn)字符串的方法。
1.格式化日期字符串時常用的占位符
- %Y Year with century as a decimal number.
- %m Month as a decimal number [01,12].
- %d Day of the month as a decimal number [01,31].
- %H Hour (24-hour clock) as a decimal number [00,23].
- %M Minute as a decimal number [00,59].
- %S Second as a decimal number [00,61].
- %z Time zone offset from UTC.
- %a Locale's abbreviated weekday name.
- %A Locale's full weekday name.
- %b Locale's abbreviated month name.
- %B Locale's full month name.
- %c Locale's appropriate date and time representation.
- %I Hour (12-hour clock) as a decimal number [01,12].
- %p Locale's equivalent of either AM or PM.
2.日期對象
datetime.date.today() #輸出年月日
datetime.datetime.now() #輸出年月日時分秒毫秒
可以通過datetime.date.today() 獲取到時間對象使用相應的實例方法可以獲取到年月日
可以通過datetime.datetime.now()獲取到時間對象用相應的實例方法可以獲取到年月日時分秒
屬性是year(年),month(月)day(日),hour(時),minute(分),second(秒)
st=datetime.datetime.now()
st.year #獲取年份
3.日期轉(zhuǎn)字符串
函數(shù)strftime()參數(shù)為format對象,占位符使用的就是1中提到的那幾個。 將日期按照指定格式進行格式化,并返回出來。
代碼如下:
import datetime
datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
4.字符串轉(zhuǎn)日期
通過的是strptime()參數(shù)是一個字符串,還有字符串的格式化(哪里是年哪里是月) eg:
import datetime
str = datetime.strptime(“20200202”,"%Y%m%d")
這里str就是2020年02月02日的日期對象
切記時間對象可以直接比較大?。〞r間的前后)
總結(jié)
主要講述了Python中日期與時間常用到的一些函數(shù),以及日期格式化為字符串、字符串轉(zhuǎn)換為日期對象。雖然篇幅短小但是非常精悍。對于時間日期對象重點掌握時間差怎么求,怎么處理字符串與日期之間的關(guān)系就足夠了。其余功能在我們使用的時候再去官方文檔上查找。

到此這篇關(guān)于Python 一篇文章看懂時間日期對象的文章就介紹到這了,更多相關(guān)Python 時間日期對象內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Pandas實現(xiàn)groupby分組統(tǒng)計的實踐
本文主要介紹了Pandas實現(xiàn)groupby分組統(tǒng)計的實踐,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-01-01
python三種數(shù)據(jù)結(jié)構(gòu)及13種創(chuàng)建方法總結(jié)
拿Python來說,數(shù)據(jù)結(jié)構(gòu)的概念也是超級重要,不同的數(shù)據(jù)結(jié)構(gòu),有著不同的函數(shù),供我們調(diào)用,接下來,我們分別來介紹字符串、列表、字典的創(chuàng)建方法2021-09-09
tensorflow 獲取變量&打印權(quán)值的實例講解
今天小編就為大家分享一篇tensorflow 獲取變量&打印權(quán)值的實例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-06-06
如何通過Python的pyttsx3庫將文字轉(zhuǎn)為音頻
pyttsx3是一個開源的Python文本轉(zhuǎn)語音庫,可以將文本轉(zhuǎn)換為自然的人類語音,這篇文章主要介紹了如何通過Python的pyttsx3庫將文字轉(zhuǎn)為音頻,需要的朋友可以參考下2023-04-04

