Reputation: 13
Firstly, new to Python but have 32 years of experience in mainframe programming in z/OS REXX. Surprisingly both are very similar and the learning curve for the basics was small. Having said that the issue I am having is with the GCSA package/library (not up on terminology). Here's the code in question ...
from gcsa.google_calendar import GoogleCalendar
from datetime import date
from datetime import timedelta
calendar = GoogleCalendar('******************************@group.calendar.google.com')
start_date = date.today() - timedelta(days = 1)
start_date = str(start_date) + " 00:00:01"
end_date = date.today()
end_date = str(end_date) + " 23:59:59"
calendar.get_events(start_date, end_date)
for event in calendar:
print(event)
The issue is the above only returns Events that are pending. Everything else is missing. The requirement is to list ALL events from yesterday at midnight to today at midnight. At this point not sure if it's something within the calendar settings or the above code. Note: passing no parameters to (calendar.get_events()
) gives the same result.
Any help would be appreciated. Thanks in advance.
Upvotes: 1
Views: 178
Reputation: 12130
I'm the author of the library :)
You are almost there. Iterating over the calendar
as you do, will go through events from today until the same date next year.
You need to iterate over the result of calendar.get_events(start_date, end_date)
:
for event in calendar.get_events(start_date, end_date):
print(event)
or you can use:
for event in calendar[start_date:end_date]:
print(event)
But your start_date
and end_date
are strings, they need to be date
/datetime
objects. I suggest you use @user19077881 's suggestion to combine the date and time.
Please, let me know if this helps.
Upvotes: 1
Reputation: 5410
get_events takes Python datetime objects as parameters; you are creating string objects to represent your chosen dates and times. See code below to construct (and also in case useful display using print) datetime objects. These can then be used as arguments in calling the calendar function.
from datetime import datetime
from datetime import date
from datetime import timedelta
start_date = datetime.combine(date.today(), datetime.min.time())
start_date = start_date + timedelta(days= -1, minutes = 1)
end_date = datetime.combine(date.today(), datetime.min.time())
end_date = end_date + timedelta(hours = 23, minutes = 59, seconds = 50)
print(datetime.strftime(start_date, '%y-%m-%d %H:%M:%S'))
print(datetime.strftime(end_date, '%y-%m-%d %H:%M:%S'))
which produces (for today)
22-12-20 00:01:00
22-12-21 23:59:50
Upvotes: 1