8
問題是我希望它忽略日期和時間因素。以下是我有:從日期時間提取時間並確定時間(不是日期)是否在範圍內?
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'
我知道我正在使用兩個diff erent庫或whatnot,但我不知道如何轉換它們之間或完成我想要做的只使用一個。我只想讓它忽略日期,只檢查時間是否在指定範圍內。由於我在代碼中的其他地方使用了一個庫,所以Python 2.6是必須的。
謝謝!我很感激! – Dan 2013-04-22 03:52:07