环境:win10+python3x
V:1.0
简单实现了一下功能本地测试可用,记录下次接着二次开发用
import datetime
import re
class TimeDifferenceC(object):
'''
1 开始日期和结束日期格式必须相同
如: 2019-07-23 2019-07-24
或如:08:04:12 08:19:53
或如:"2019-07-06 07:08:08", "2019-07-07 08:08:18"
但是不可以日期对时间 2019-07-23和08:04:12
2 格式固定 日期必须使用 "-" 连接 时间必须使用 ":" 连接,日期和时间之间用 " " 连接
'''
def __init__(self, old_time="", new_time="", type_=None):
self.old_time = old_time
self.new_time = new_time
self.type_ = type_
self.Check()
self.split_time()
def Check(self):
'''检查输入的数据是否符合规范'''
sign = True
#对比日期和时间
rule_dt = re.compile(r"^\d{4}-\d{1,2}-\d{1,2}\s\d{1,2}:\d{1,2}:\d{1,2}$")
if rule_dt.findall(self.old_time) and rule_dt.findall(self.new_time):
print("xxxx-xx-xx h:m:s 格式确认都正确")
self.dt_type = "all"
sign = False
rule_date = re.compile(r"^\d{4}-\d{1,2}-\d{1,2}$")
if sign and rule_date.findall(self.old_time) and rule_date.findall(self.new_time):
print("xxxx-xx-xx 格式确认都正确")
self.dt_type = "date"
sign = False
rule_time = re.compile(r"^\d{1,2}:\d{1,2}:\d{1,2}$")
if sign and rule_time.findall(self.old_time) and rule_time.findall(self.new_time):
print("h:m:s 格式确认都正确")
self.dt_type = "time"
sign = False
if sign:
print(f"{self.old_time} or {self.new_time} 格式不对。格式如下: \nxxxx-xx-xx h:m:s 或者 xxxx-x-x h:m:s")
exit()
def split_time(self):
self.old_time_list = self.old_time.split()
self.new_time_list = self.new_time.split()
def Get(self, time_, type_=None):
if self.dt_type == "all":
if type_ == "y":
return datetime.datetime.strptime(time_,"%Y-%m-%d").year
elif type_ == "md":
return datetime.datetime.strptime(time_,"%Y-%m-%d").month
elif type_ == "h" or type_ == "m" or type_ == "s":
return datetime.datetime.strptime(time_,"%Y-%m-%d %H:%M:%S")
return datetime.datetime.strptime(time_,"%Y-%m-%d")
elif self.dt_type == "date":
#日期类型只能处理 年 月 日
y_m_d = datetime.datetime.strptime(time_[0:10],"%Y-%m-%d")
if type_ == "y":
return y_m_d.year
elif type_ == "md":
return y_m_d.month
else:
return y_m_d
else:
#时间类型只能处理时分秒
return datetime.datetime.strptime(time_,"%H:%M:%S")
def Days(self):
if self.dt_type == "time":
return "只能获得时间类型差值"
date1 = self.Get(self.old_time_list[0])
date2 = self.Get(self.new_time_list[0])
return (date2-date1).days
def Months(self):
if self.dt_type == "time":
return "只能获得时间类型差值"
month1 = self.Get(self.old_time_list[0], "md")
month2 = self.Get(self.new_time_list[0], "md")
return (self.Years())*12+(month2-month1)
def Years(self):
if self.dt_type == "time":
return "只能获得时间类型差值"
year1 = self.Get(self.old_time_list[0], "y")
year2 = self.Get(self.new_time_list[0], "y")
return year2-year1
def Hours(self):
if self.dt_type == "date":
return "只能获得日期类型差值"
return int(self.Seconds()/60/60)
def Seconds(self):
if self.dt_type == "date":
return "只能获得日期类型差值"
sec_old = self.Get(self.old_time, "s")
sec_new = self.Get(self.new_time, "s")
# return (sec_new-sec_old).seconds #不包含日期上的差
return (sec_new-sec_old).total_seconds() #包含日期上的时间差
def Minutes(self):
if self.dt_type == "date":
return "只能获得日期类型差值"
return int(self.Seconds()/60)
测试:
# 计算两个日期差 格式:2019-07-06 07:08:08 或者 2019-07-06
# 计算两个日期差 格式:2019-07-06 07:08:08 或者 07:08:08
tdc = TimeDifferenceC("2019-07-06 07:08:08", "2019-07-07 08:08:18")
# tdc = TimeDifferenceC("2019-07-06", "2019-07-07")
# tdc = TimeDifferenceC("07:08:08", "08:08:18")
print(tdc.Days())
print(tdc.Years())
pritt(tdc.Months())
print(tdc.Seconds())
print(tdc.Minutes())
print(tdc.Hours())