谷歌日历有什么方法可以简单地为我提供事件发生的星期几的名称?例如,如果我检索一个日期范围内的事件列表,如下所示:
events = service.events().list(calendarId='primary', timeMin='2012-12-24T00:00:00Z',
timeMax='2012-12-30T23:59:59Z').execute()
有没有办法在该列表中查找特定事件并找出它在哪一天?现在我正在使用一个尴尬的技巧,涉及 Google Calendar API 中的“date”和“dateTime”以及 Python 的 calendar 模块:
for calendar_list_entry in events['items']:
try:
year, month, day = calendar_list_entry['start']['date'].split('-')
dayNum = calendar.weekday(int(year), int(month), int(day))
print dayNum
dayName = createDayName(dayNum)
dayDict[dayName].append(calendar_list_entry['summary'])
print dayDict[dayName]
except:
print calendar_list_entry['start']['dateTime'][:10].split('-')
year, month, day = calendar_list_entry['start']['dateTime'][:10].split('-')
dayNum = calendar.weekday(int(year), int(month), int(day))
print dayNum
dayName = createDayName(dayNum)
dayDict[dayName].append(calendar_list_entry['summary'])
print dayDict[dayName]
createDayName 函数很简单:
def createDayName(dayNum):
'''
Takes as input a number generated from calendar.weekday and outputs the weekday name
that is associated with that number.
'''
dayNameList = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
return dayNameList[dayNum]
当然有一个不那么麻烦的方法来做到这一点?对于跨越多天的事件,我也遇到了严重的问题,即周四至周六。我意识到我可以做一些荒谬的数学来划分日子,但必须有更好的方法来完成如此简单的操作。