#计算精确时间差
#-----------------------------
# 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
Directive
|
Meaning
|
Notes
|
%a
|
Locale’s abbreviated weekday name.
|
|
%A
|
Locale’s full weekday name.
|
|
%b
|
Locale’s abbreviated month name.
|
|
%B
|
Locale’s full month name.
|
|
%c
|
Locale’s appropriate date and time representation.
|
|
%d
|
Day of the month as a decimal number [01,31].
|
|
%f
|
Microsecond as a decimal number [0,999999], zero-padded on the left
|
-1
|
%H
|
Hour (24-hour clock) as a decimal number [00,23].
|
|
%I
|
Hour (12-hour clock) as a decimal number [01,12].
|
|
%j
|
Day of the year as a decimal number [001,366].
|
|
%m
|
Month as a decimal number [01,12].
|
|
%M
|
Minute as a decimal number [00,59].
|
|
%p
|
Locale’s equivalent of either AM or PM.
|
-2
|
%S
|
Second as a decimal number [00,61].
|
-3
|
%U
|
Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0.
|
-4
|
%w
|
Weekday as a decimal number [0(Sunday),6].
|
|
%W
|
Week number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0.
|
-4
|
%x
|
Locale’s appropriate date representation.
|
|
%X
|
Locale’s appropriate time representation.
|
|
%y
|
Year without century as a decimal number [00,99].
|
|
%Y
|
Year with century as a decimal number.
|
|
%z
|
UTC offset in the form +HHMM or -HHMM (empty string if the the object is naive).
|
-5
|
%Z
|
Time zone name (empty string if the object is naive).
|
|
%%
|
A literal'%'character.
|
|
#两日期相减 时间差:
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()