问题是,我希望它忽略时间的日期和唯一的因素。 以下是我有:
import time
from time import mktime
from datetime import datetime
def getTimeCat(Datetime):
# extract time categories
str_time = datetime.strptime(Datetime, "%m/%j/%y %H:%M")
ts = datetime.fromtimestamp(mktime(str_time))
# --> Morning = 0400-1000
mornStart = datetime.time(4, 0, 1)
mornEnd = datetime.time(10, 0, 0)
# --> Midday = 1000-1600
midStart = datetime.time(10, 0, 1)
midEnd = datetime.time(16, 0, 0)
# --> Evening = 1600-2200
eveStart = datetime.time(16, 0, 1)
eveEnd = datetime.time(22, 0, 0)
# --> Late Night = 2200-0400
lateStart = datetime.time(22, 0, 1)
lateEnd = datetime.time(4, 0, 0)
if time_in_range(mornStart, mornEnd, ts):
timecat = 0 #morning
elif time_in_range(midStart, midEnd, ts):
timecat = 1 #midday
elif time_in_range(eveStart, eveEnd, ts):
timecat = 2 #evening
elif time_in_range(lateStart, lateEnd, ts):
timecat = 3 #late night
return timecat
由于是,我得到这个错误:
TypeError: argument must be 9-item sequence, not datetime.datetime
当我改变相关线路:
str_time = time.strptime(Datetime, "%m/%j/%y %H:%M")
我得到这个错误:
TypeError: descriptor 'time' requires a 'datetime.datetime' object but received a 'int'
我知道我有两个不同的库或诸如此类的东西的工作,但我不知道如何将它们之间的转换或完成我只想用一个做。 我只是希望它忽略的日期,只检查时间是在规定范围内。 Python 2.6中是必须的,由于我使用的代码的其他地方库。