问题是我希望它忽略日期而只考虑时间。这是我所拥有的:
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 是必须的,因为我在代码的其他地方使用了一个库。