Python datetime timedelta问题

时间:2016-12-01 03:52:22

标签: python date datetime timedelta python-datetime

我试图将predict_day变量设置为当前日期,如果timems datetime的时间介于0:00:00和9:29:59之间。

如果时间介于两者之间9:30:00和15:59:59,我想继续下一个项目。

如果时间是16:00:00到23:59:59我想加一个day_predict使用timedelta(days = 1),其中timems.weekday()= 0,1,2,3。

当timems.weekday()= 4,时间在16:00之间:00和23:59:59,我需要day_predict使用timedelta(days = 3)添加3天。

当timems.weekday()= 5时,无论什么时候,我都需要day_predict来添加2天umeng itmedelta(天= 2)。

当timems.weekday()= 6时,无论时间,我都需要day_predict添加1天。

# Format Datetime Stamps
timems = datetime.datetime.fromtimestamp(float(item['TimeMs']['$numberLong']) / 1e3)
time = timems.time()
hour = time.hour
minute = time.minute
second = time.second

# Sets predict_day to current day if tweet occurred between 12am and 930am
if timems.weekday() == 0 or timems.weekday() == 1 or timems.weekday() == 2 or timems.weekday() == 3 or timems.weekday() == 4 and (datetime.time(0,0,0) <= datetime.time(hour, minute, second) and datetime.time(hour, minute, second) <= datetime.time(9,29,59)):
    predict_day = timems.date()
# Sets predict_day to current day + 1 if tweet occurred between 4pm and 12am on Monday, Tuesday, Wednesday, Thursday
if timems.weekday() == 0 or timems.weekday() == 1 or timems.weekday() == 2 or timems.weekday() == 3 and (datetime.time(15,59,59) <= datetime.time(hour, minute, second) and datetime.time(hour, minute, second) <= datetime.time(23,59,59)):
    predict_day = timems.date() + datetime.timedelta(days = 1)
# Sets perdict_day to current day + 3 (Monday) if tweet occurerd after 4pm on Friday
if timems.weekday() == 4 and (datetime.time(15,59,59) <= datetime.time(hour, minute, second) and datetime.time(hour, minute, second) <= datetime.time(23,59,59)):
    predict_day = timems.date() + datetime.timedelta(days = 3)
# Sets predict_day to current day + 2 (Monday) if tweet occurred on Saturday
if timems.weekday() == 5 and (datetime.time(0,0,0) <= datetime.time(hour, minute, second) or datetime.time(hour, minute, second) <= datetime.time(23,59,59)):
    predict_day = timems.date() + datetime.timedelta(days = 2)
# Sets predict_day to current day + 1 (Monday) if tweet occured on Sunday
if timems.weekday() == 6 and (datetime.time(0,0,0) <= datetime.time(hour, minute, second) or datetime.time(hour, minute, second) <= datetime.time(23,59,59)):
    predict_day = timems.date() + datetime.timedelta(days = 1)

我的结果被写入.csv文件,我遇到了让它正常运行的问题。

0 个答案:

没有答案