#计算精确时间差 #----------------------------- # High Resolution Timerst1=time.clock()# Do Stuff Heret2=time.clock()print t2-t1# 2.27236813618# Accuracy will depend on platform and OS,# but time.clock() uses the most accurate timer it cantime.clock(); time.clock()# 174485.51365466841# 174485.55702610247#----------------------------- # Also useful;import timeitcode='[x for x in range(10) if x % 2 == 0]'eval(code)# [0, 2, 4, 6, 8]t=timeit.Timer(code)print"10,000 repeats of that code takes:",t.timeit(10000),"seconds"print"1,000,000 repeats of that code takes:",t.timeit(),"seconds"# 10,000 repeats of that code takes: 0.128238644856 seconds# 1,000,000 repeats of that code takes: 12.5396490336 seconds#----------------------------- import timeitcode='import random; l = random.sample(xrange(10000000), 1000); l.sort()'t=timeit.Timer(code)print"Create a list of a thousand random numbers. Sort the list. Repeated a thousand times."print"Average Time:",t.timeit(1000) /1000# Time taken: 5.24391507859 #time , datetime , string 类型互相转换 # string -> time > time.strptime(publishDate,"%Y-%m-%d %H:%M:%S") # time -> string >time.strftime("%y-%m-%d",t)#string ->datetime datetime.strptime(date_string,format) #datetime->string datetime.strftime("%Y-%m-%d %H:%M:%S") strptime formating
#两日期相减 时间差: d1 = datetime.datetime(2005, 2, 16) d2 = datetime.datetime(2004, 12, 31) print (d1 - d2).days starttime = datetime.datetime.now() endtime = datetime.datetime.now() print (endtime - starttime).seconds#计算当前时间向后10天的时间。 #如果是小时 days 换成 hours d1 = datetime.datetime.now() d3 = d1 + datetime.timedelta(days =10) print str(d3) print d3.ctime() import datetime, calendar#昨天 def getYesterday(): today=datetime.date.today() oneday=datetime.timedelta(days=1) yesterday=today-oneday return yesterday #今天 def getToday(): return datetime.date.today() #获取给定参数的前几天的日期,返回一个list def getDaysByNum(num): today=datetime.date.today() oneday=datetime.timedelta(days=1) li=[] for i in range(0,num): #今天减一天,一天一天减 today=today-oneday #把日期转换成字符串 #result=datetostr(today) li.append(datetostr(today)) return li #将字符串转换成datetime类型 def strtodatetime(datestr,format): return datetime.datetime.strptime(datestr,format) #时间转换成字符串,格式为2008-08-02 def datetostr(date): return str(date)[0:10] #两个日期相隔多少天,例:2008-10-03和2008-10-01是相隔两天 def datediff(beginDate,endDate): format="%Y-%m-%d"; bd=strtodatetime(beginDate,format) ed=strtodatetime(endDate,format) oneday=datetime.timedelta(days=1) count=0 while bd!=ed: ed=ed-oneday count+=1 return count #获取两个时间段的所有时间,返回list def getDays(beginDate,endDate): format="%Y-%m-%d"; bd=strtodatetime(beginDate,format) ed=strtodatetime(endDate,format) oneday=datetime.timedelta(days=1) num=datediff(beginDate,endDate)+1 li=[] for i in range(0,num): li.append(datetostr(ed)) ed=ed-oneday return li #获取当前年份 是一个字符串 def getYear(): return str(datetime.date.today())[0:4] #获取当前月份 是一个字符串 def getMonth(): return str(datetime.date.today())[5:7] #获取当前天 是一个字符串 def getDay(): return str(datetime.date.today())[8:10] def getNow(): return datetime.datetime.now() print getToday() print getYesterday() print getDaysByNum(3) print getDays('2008-10-01','2008-10-05') print '2008-10-04 00:00:00'[0:10] print str(getYear())+getMonth()+getDay() print getNow() |