一开始我是这么做的
import time
import datetime def conv_time(t):
min = re.findall('\d+', t)[0]
if u'分钟' in t:
c = time.time() - int(min) * 60 elif u'小时' in t:
c = time.time() - int(min)*60*60 elif u'天' in t:
c = time.time() - int(min)*60*60*24
else:
return None
c = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(c))
c = datetime.datetime.strptime(c, "%Y-%m-%d %H:%M:%S")
return str(int(time.mktime(c.timetuple()))) + ""
后面看到了datetime的timedelta, 所以代码改成了这样
from datetime import datetime, timedelta current_year = datetime.today().strftime("%Y") def conv_time(t):
min = int(re.findall('\d+', t)[0])
if u'秒' in t:
s = (datetime.now() - timedelta(seconds=min))
elif u'分钟' in t:
s = (datetime.now() - timedelta(minutes=min)) elif u'小时' in t:
s = (datetime.now() - timedelta(hours=min)) elif u'天' in t:
s = (datetime.now() - timedelta(days=min))
else:
t += ", " + current_year
s = datetime.strptime(t, "%m-%d, %Y")
return str(int(time.mktime(s.timetuple()))) + ""