循环遍历 python 中的事件
Looping through events in python
我开发了一个小程序来检查 google 日历中的双重预订。我的问题是,如果我选择检查 7 月 10 日,它将检查 10 和 11。
这是代码中有问题的部分:
for i in range(3):
# Gets the start and end times and strips them so that days can be added
# and to check if the current day is a weekend
startStrip = datetime.datetime.strptime(event_start, "%Y-%m-%dT%H:%M:%S")
endStrip = datetime.datetime.strptime(event_end, "%Y-%m-%dT%H:%M:%S")
dayOfWeek = startStrip + datetime.timedelta(days=i)
# les bons formats
currentStart = str(startStrip + datetime.timedelta(days=i)).replace(" ", "T")
currentEnd = str(endStrip + datetime.timedelta(days=i)).replace(" ", "T")
calendarEnd = str(endStrip + datetime.timedelta(days=i + 1)).replace(" ", "T")
# If the current day is a weekend, add 2 days
if dayOfWeek.weekday() > 4:
currentStart = str(startStrip + datetime.timedelta(days=i + 2)).replace(" ", "T")
currentEnd = str(endStrip + datetime.timedelta(days=i + 2)).replace(" ", "T")
calendarEnd = str(endStrip + datetime.timedelta(days=i + 3)).replace(" ", "T")
else:
print("Looking...")
# Call the Calendar API
# Finds the event for the current day
now = datetime.datetime.utcnow().isoformat() + 'Z' # 'Z' indicates UTC time
events_result = service.events().list(calendarId='primary', timeMin=currentStart + "-00:00",
maxResults=30, timeMax=calendarEnd + "-00:00",
singleEvents=True, orderBy='startTime').execute()
events = events_result.get('items', [])
currentEmployees = []
for event in events:
currentEmployees.append(event['summary'])
这里的问题是 calendarEnd
。 calendarEnd
=约会结束+1.
所以每当我循环检查所有事件,并查看员工姓名是否在当天注册时,它不仅会检查 7 月 10 日,还会检查 11 日。这会导致问题。
如果我删除 ''+1'' 并只保留它:
calendarEnd = str(endStrip + datetime.timedelta(days=i)).replace(" ", "T")
它甚至无法正确加载 currentEmployees 列表。出于某种原因,在此列表中,它永远不会添加超过下午 1 点的事件,只会添加早上开始的事件。
所需的输出应该是,如果我想为 7 月 10 日添加一个事件,代码应该添加 currentEmployees[]
仅 7 月 10 日的所有摘要事件。
编辑:
正如您在此示例中看到的,它将 return 7 月 16 日开始的所有人,以及 7 月 17 日预订的另外 2 名员工,除了下午 1 点的那个!
问题是这样的:
timeMin=currentStart + "-00:00",
我必须这样做才能在美国东部时间进行转换:
timeMin=currentStart + "-04:00",
我觉得好傻
我还必须在 :
中删除 (days=i + 1)
calendarEnd = str(endStrip + datetime.timedelta(days=i + 1)).replace(" ", "T")
成为:
calendarEnd = str(endStrip + datetime.timedelta(days=i)).replace(" ", "T")
因为+1 增加了一天
我开发了一个小程序来检查 google 日历中的双重预订。我的问题是,如果我选择检查 7 月 10 日,它将检查 10 和 11。
这是代码中有问题的部分:
for i in range(3):
# Gets the start and end times and strips them so that days can be added
# and to check if the current day is a weekend
startStrip = datetime.datetime.strptime(event_start, "%Y-%m-%dT%H:%M:%S")
endStrip = datetime.datetime.strptime(event_end, "%Y-%m-%dT%H:%M:%S")
dayOfWeek = startStrip + datetime.timedelta(days=i)
# les bons formats
currentStart = str(startStrip + datetime.timedelta(days=i)).replace(" ", "T")
currentEnd = str(endStrip + datetime.timedelta(days=i)).replace(" ", "T")
calendarEnd = str(endStrip + datetime.timedelta(days=i + 1)).replace(" ", "T")
# If the current day is a weekend, add 2 days
if dayOfWeek.weekday() > 4:
currentStart = str(startStrip + datetime.timedelta(days=i + 2)).replace(" ", "T")
currentEnd = str(endStrip + datetime.timedelta(days=i + 2)).replace(" ", "T")
calendarEnd = str(endStrip + datetime.timedelta(days=i + 3)).replace(" ", "T")
else:
print("Looking...")
# Call the Calendar API
# Finds the event for the current day
now = datetime.datetime.utcnow().isoformat() + 'Z' # 'Z' indicates UTC time
events_result = service.events().list(calendarId='primary', timeMin=currentStart + "-00:00",
maxResults=30, timeMax=calendarEnd + "-00:00",
singleEvents=True, orderBy='startTime').execute()
events = events_result.get('items', [])
currentEmployees = []
for event in events:
currentEmployees.append(event['summary'])
这里的问题是 calendarEnd
。 calendarEnd
=约会结束+1.
所以每当我循环检查所有事件,并查看员工姓名是否在当天注册时,它不仅会检查 7 月 10 日,还会检查 11 日。这会导致问题。
如果我删除 ''+1'' 并只保留它:
calendarEnd = str(endStrip + datetime.timedelta(days=i)).replace(" ", "T")
它甚至无法正确加载 currentEmployees 列表。出于某种原因,在此列表中,它永远不会添加超过下午 1 点的事件,只会添加早上开始的事件。
所需的输出应该是,如果我想为 7 月 10 日添加一个事件,代码应该添加 currentEmployees[]
仅 7 月 10 日的所有摘要事件。
编辑:
正如您在此示例中看到的,它将 return 7 月 16 日开始的所有人,以及 7 月 17 日预订的另外 2 名员工,除了下午 1 点的那个!
问题是这样的:
timeMin=currentStart + "-00:00",
我必须这样做才能在美国东部时间进行转换:
timeMin=currentStart + "-04:00",
我觉得好傻
我还必须在 :
中删除(days=i + 1)
calendarEnd = str(endStrip + datetime.timedelta(days=i + 1)).replace(" ", "T")
成为:
calendarEnd = str(endStrip + datetime.timedelta(days=i)).replace(" ", "T")
因为+1 增加了一天