标签:python 时间处理
__author__ = ‘root‘ from datetime import * TIMEFORAT = "%Y-%m-%d %H:%M:%S" class CTimeHandle(): def __init__(self, timeformat=TIMEFORAT): self._format = timeformat self._year = "" self._datetime = datetime.now() def diffTwoTimeSeconds(self,time1,time2,format=TIMEFORAT): datetime1 = datetime.strptime(time1,format) datetime2 = datetime.strptime(time2,format) k=1 if self.CompareTwoTime(time1,time2,format): temp = datetime1 datetime1 = datetime2 datetime2 = temp k=-1 diff = (datetime1 - datetime2).seconds result = int(diff)*k print result return result def CompareTwoTime(self,time1,time2,format=TIMEFORAT): datetime1 = datetime.strptime(time1,format) datetime2 = datetime.strptime(time2,format) return datetime1 < datetime2 def SetTimeFormat(self, timefrmat): self._format = timefrmat def GetNow(self): try: return self._datetime.now().strftime(self._format) except: print "there is error" def updateNow(self): self._datetime = self._datetime.now() return self.GetNow() def SetTime(self,time,format=TIMEFORAT): self._datetime =datetime.strptime(time,format) def GetTime(self): return self._datetime.strftime(self._format) def GetDateinfo(self): return self._datetime.year, self._datetime.month, self._datetime.day def GetTimeinfo(self): return self._datetime.hour, self._datetime.minute, self._datetime.second def AddSecond(self, addsecond=0): self._datetime =self._datetime + timedelta(seconds=addsecond) return self._datetime.strftime(self._format) def AddMinute(self, addMinute=0): self._datetime =self._datetime + timedelta(minutes=addMinute) return self._datetime.strftime(self._format) def AddHour(self, addHour=0): self._datetime =self._datetime + timedelta(hours=addHour) return self._datetime.strftime(self._format) if __name__ == "__main__": now = datetime.now() today = datetime.today() c = CTimeHandle() g = CTimeHandle() g.SetTime(str(c.GetTime())) d =c.GetNow() e = g.AddHour(0.1) g.diffTwoTimeSeconds(c.GetTime(),g.GetTime()) print d print e
本文出自 “风清扬song” 博客,请务必保留此出处http://2309998.blog.51cto.com/2299998/1612728
标签:python 时间处理
原文地址:http://2309998.blog.51cto.com/2299998/1612728