The problem is that I want it to ignore the date and only factor in the time. Here is what I have:
问题是我想让它忽略日期,只考虑时间因素。以下是我的资料:
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
As is, I get this error:
实际上,我得到了这个错误:
TypeError: argument must be 9-item sequence, not datetime.datetime
When I change the relevant line to:
当我将相关行改为:
str_time = time.strptime(Datetime, "%m/%j/%y %H:%M")
I get this error:
我得到这个错误:
TypeError: descriptor 'time' requires a 'datetime.datetime' object but received a 'int'
I know I'm working with two different libraries or whatnot, but I'm not sure how to convert between them or accomplish what I want to do only using one. I just want it to ignore the date and only check if the time is within the specified ranges. Python 2.6 is a MUST due to a library I'm using elsewhere in the code.
我知道我正在使用两个不同的库,但我不知道如何在它们之间进行转换,或者只使用一个库来完成我想做的事情。我只是想让它忽略日期,只检查时间是否在指定范围内。Python 2.6是必需的,因为我在代码的其他地方使用了一个库。
1 个解决方案
#1
15
This line:
这条线:
str_time = datetime.strptime(Datetime, "%m/%j/%y %H:%M")
returns a datetime
object as per the docs.
根据文档返回一个datetime对象。
You can test this yourself by running the following command interactively in the interpreter:
您可以通过在解释器中交互式地运行以下命令来测试自己:
>>> import datetime
>>> datetime.datetime.strptime('12/31/13 00:12', "%m/%j/%y %H:%M")
datetime.datetime(2013, 1, 31, 0, 12)
>>>
The time portion of the returned datetime can then be accessed using the .time()
method.
然后可以使用.time()方法访问返回的datetime部分。
>>> datetime.datetime.strptime('12/31/13 00:12', "%m/%j/%y %H:%M").time()
datetime.time(0, 12)
>>>
The datetime.time()
result can then be used in your time comparisons.
然后可以在时间比较中使用datetime.time()结果。
#1
15
This line:
这条线:
str_time = datetime.strptime(Datetime, "%m/%j/%y %H:%M")
returns a datetime
object as per the docs.
根据文档返回一个datetime对象。
You can test this yourself by running the following command interactively in the interpreter:
您可以通过在解释器中交互式地运行以下命令来测试自己:
>>> import datetime
>>> datetime.datetime.strptime('12/31/13 00:12', "%m/%j/%y %H:%M")
datetime.datetime(2013, 1, 31, 0, 12)
>>>
The time portion of the returned datetime can then be accessed using the .time()
method.
然后可以使用.time()方法访问返回的datetime部分。
>>> datetime.datetime.strptime('12/31/13 00:12', "%m/%j/%y %H:%M").time()
datetime.time(0, 12)
>>>
The datetime.time()
result can then be used in your time comparisons.
然后可以在时间比较中使用datetime.time()结果。