python 时间日期处理汇集
2011-06-10 19:01
711 查看
转:python 时间日期处理汇集
python 时间日期处理汇集2011-03-14 16:52
#计算精确时间差 #----------------------------- # High Resolution Timers t1=time.clock() # Do Stuff Here t2=time.clock() print t2-t1 # 2.27236813618 # Accuracy will depend on platform and OS, # but time.clock() uses the most accurate timer it can time.clock(); time.clock() # 174485.51365466841 # 174485.55702610247 #----------------------------- # Also useful; import timeit code='[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 timeit code='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() |
相关文章推荐
- 转:python 时间日期处理汇集
- python 时间日期处理汇集
- python 日期和时间 处理
- PYTHON-基础-时间日期处理小结
- python日期时间处理
- python+pandas+时间、日期以及时间序列处理
- python time时间,日期,时间戳处理
- Python-基础-时间日期处理小结
- Python常用的日期时间处理方法示例
- Python模块学习 ---- time 日期时间处理
- python selenium 处理时间日期控件(十六)
- PYTHON-基础-时间日期处理小结
- Python中基本的日期时间处理的学习教程
- Python进阶(二十九)-Python时间&日期&时间戳处理
- python time时间,日期,时间戳处理
- python 日期处理(日期间间隔时间)
- Python的时间、日期和时间戳的处理办法
- Python-基础-时间日期处理小结
- python处理时间与日期
- Python简单时间日期处理