转换时间为时间元组
import time
s = '2019-01-01'
print(time.strptime(s, '%Y-%m-%d'))
s_time = '09:00:00'
print(time.strptime(s_time, '%H:%M:%S'))
元组类型时间
tuple_time = time.localtime()
print(tuple_time)
元组时间转换为字符串时间
print(time.strftime('%m-%d', tuple_time))
print(time.strftime('%Y-%m-%d', tuple_time))
print(time.strftime('%F', tuple_time))
print(time.strftime('%T', tuple_time))
时间戳类型转换为字符串时间
pwd_time = os.path.getctime('/home/kiosk/test')
pwd_time = os.path.getatime('/home/kiosk/test')
pwd_time = os.path.getmtime('/home/kiosk/test')
print(time.ctime(pwd_time))
将时间戳类型转换为元组
print(time.localtime(pwd_time))
元组转换为时间戳时间
tuple_time = time.localtime()
print(time.mktime(tuple_time))
导入
from datetime import datetime
from datetime import date
import os
from datetime import timedelta
现在的时间
print(date.today())
print(datetime.now())
几天前后
d = date.today()
delta = timedelta(days=3)
print(d+delta)
print(d-delta)
几个小时前后
d = datetime.now()
delta = timedelta(hours=3)
print(d+delta)
print(d-delta)
文件的修改时间
now_time = datetime.now()
pwd_time = os.path.getmtime('/etc/passwd')
pwd_time_obj = datetime.fromtimestamp(pwd_time)
delta = now_time - pwd_time_obj
print(delta)
系统运行时间(Linux—>uptime)
now_time = datetime.now()
boot_time = psutil.boot_time()
boot_time_obj = datetime.fromtimestamp(boot_time)
print(boot_time_obj)
delta_time = datetime.now()
dalta_time1 = now_time - boot_time_obj