閑來無事,做做整理。
time模塊:
從事綿陽服務(wù)器托管,服務(wù)器租用,云主機(jī),雅安服務(wù)器托管,域名與空間,CDN,網(wǎng)絡(luò)代維等服務(wù)。
import time
# 睡眠 參數(shù)(秒) 可以為小數(shù)
time.sleep(0)
# 獲取當(dāng)前時(shí)間戳,從1970年1月1日0時(shí)0分0秒開始計(jì)算
t = time.time() # 1543626884.3397386
# 獲取當(dāng)前時(shí)區(qū)的時(shí)間元組time.struct_time對象
# 東八區(qū)
# time.struct_time(tm_year=2018, tm_mon=12, tm_mday=1, tm_hour=9, tm_min=17, tm_sec=21, tm_wday=5, tm_yday=335, tm_isdst=0)
t = time.localtime()
# 獲取0時(shí)區(qū)的時(shí)間元組time.struct_time對象
# 0時(shí)區(qū) 格林尼治時(shí)間
# time.struct_time(tm_year=2018, tm_mon=12, tm_mday=1, tm_hour=1, tm_min=29, tm_sec=6, tm_wday=5, tm_yday=335, tm_isdst=0)
t = time.gmtime()
# 根據(jù)時(shí)間元組time.struct_time對象創(chuàng)建時(shí)間戳
t = time.mktime((2018, 12, 1, 9, 17, 21, 5, 335, 0)) # 1543627041.0
# 0時(shí)區(qū)與當(dāng)前時(shí)區(qū)相差的秒數(shù)
t = time.timezone
# 根據(jù)時(shí)間元組對象time.struct_time格式化輸出時(shí)間
t = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) # 2018-12-01 09:44:48
# 將時(shí)間戳轉(zhuǎn)化為標(biāo)準(zhǔn)輸出字符串
t = time.ctime(time.time()) # Sat Dec 1 09:46:04 2018
# time clock() 函數(shù)以浮點(diǎn)數(shù)計(jì)算的秒數(shù)返回當(dāng)前的CPU時(shí)間。用來衡量不同程序的耗時(shí),比time.time()更有用。
# 這個(gè)需要注意,在不同的系統(tǒng)上含義不同。在UNIX系統(tǒng)上,它返回的是"進(jìn)程時(shí)間",它是用秒表示的浮點(diǎn)數(shù)(時(shí)間戳。
# 而在WINDOWS中,第一次調(diào)用,返回的是進(jìn)程運(yùn)行的實(shí)際時(shí)間。而第二次之后的調(diào)用是自第一次調(diào)用以后到現(xiàn)在的運(yùn)行時(shí)間
# 第一次:4.1053936661986517e-07
# 第二次:8.210787332397303e-07
t = time.clock()
calendar模塊:
import calendar
# 獲取某一年的日歷
# w:以幾個(gè)字母表示周幾
# l:每行間隔
# c:每月之間左右間隔
# m:每行顯示幾個(gè)月
# w, l, c, m都有默認(rèn)值
c = calendar.calendar(2018, w=3, l=1, c=6, m=10)
# 獲取某一年某一月的日歷
c = calendar.month(2018, 12)
# 判斷某一年是否是閏年
c = calendar.isleap(2018)
# 計(jì)算兩年之間有多少閏年,左閉右開
c = calendar.leapdays(2000, 2008) # 2
datetime.date模塊:
from datetime import date
import time
# 創(chuàng)建date對象
# 根據(jù)年月日創(chuàng)建
# 參數(shù)都有指定范圍
d = date(2018, 12, 1) # 2018-12-01
# 創(chuàng)建今天日期對象
d = date.today() # 2018-12-01
# 根據(jù)時(shí)間戳創(chuàng)建
d = date.fromtimestamp(time.time()) # 2018-12-01
# 提取年月日
print(d.year, d.month, d.day) # 2018 12 1
# 標(biāo)準(zhǔn)格式日期
d1 = date.isoformat(d) # 2018-12-01
# 日期元組:年份,一年第幾周,星期幾1-7
d1 = date.isocalendar(d) # (2018, 48, 6)
# 提取標(biāo)準(zhǔn)星期1-7
w = d.isoweekday() # 6
# 星期0-6
w = d.weekday() # 5
# 格式化日期
d2 = d.strftime('%Y/%m/%d') # 2018/12/01
# 將日期轉(zhuǎn)化為時(shí)間元組
# time.struct_time(tm_year=2018, tm_mon=12, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=5, tm_yday=335, tm_isdst=-1)
tt = d.timetuple()
datetime.time:
from datetime import time
# 創(chuàng)建時(shí)間對象
t = time(10, 42, 59) # 10:42:59
# 提取時(shí)分秒
print(t.hour, t.minute, t.second) # 10 42 59
# 格式化
t = t.strftime('%H %M %S') # 10 42 59
datetime.datetime:
from datetime import datetime
import time
# 創(chuàng)建對象
dt = datetime(2018, 12, 1, 11, 10, 59) # 2018-12-01 11:10:59
# 根據(jù)時(shí)間戳創(chuàng)建對象
dt = datetime.fromtimestamp(time.time())
# 獲取當(dāng)前時(shí)間,帶時(shí)區(qū)
dt = datetime.now() # 2018-12-01 10:52:38.860742
# 獲取當(dāng)前時(shí)間,不帶時(shí)區(qū)
dt = datetime.utcnow() # 2018-12-01 02:53:37.564099
# 提取年月日時(shí)分秒
print(dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second) # 2018 12 1 2 56 59
# 格式化輸出
dt1 = dt.strftime('%Y-%m-%d %H:%M:%S') # 2018-12-01 02:58:34
# 轉(zhuǎn)化為時(shí)間戳
t = dt.timestamp() # 1543604449.06578
# 轉(zhuǎn)化為時(shí)間元組
# time.struct_time(tm_year=2018, tm_mon=12, tm_mday=1, tm_hour=3, tm_min=1, tm_sec=31, tm_wday=5, tm_yday=335, tm_isdst=-1)
t = dt.timetuple()
datetime.timedelta:
from datetime import datetime, timedelta
# 創(chuàng)建對象
# 可以指定很多參數(shù),一般指定seconds或者days
td1 = timedelta(days=1) # 1 day, 0:00:00
td2 = timedelta(seconds=3600) # 1:00:00
dt1 = datetime(2018, 11, 11, 11, 11, 11)
dt2 = datetime(2018, 11, 15, 11, 11, 11)
# 計(jì)算時(shí)間差
td3 = dt2 - dt1 # 4 days, 0:00:00
td4 = dt1 - dt2 # -4 days, 0:00:00
# 獲取新時(shí)間
dt3 = dt1 + td1 # 2018-11-12 11:11:11
dt4 = dt1 - td2 # 2018-11-11 10:11:11
# 提取天數(shù),秒數(shù) 天數(shù)秒數(shù)分開
print(td1.days, td1.seconds) # 1 0
# 提取總共秒數(shù)
s = td1.total_seconds() # 86400.0