由于日期时间比较,根据时间范围过滤 icalendar 事件
Filter icalendar events based on a time frame breaks because of datetime comparison
我试图在下个月内从 ics 文件中提取事件,但我自己制作日期时间并与 ics 文件中的日期时间进行比较似乎不起作用并且出现错误 TypeError: can't compare offset-naive and offset-aware datetimes
我尝试了 here 找到的答案,但仍然出现相同的错误。下面是我正在使用的代码。
def read_from_cal():
g = open('calendar.ics', 'rb')
gcal = Calendar.from_ical(g.read())
year = datetime.now().year
month = datetime.now().month
day = datetime.now().day
hour = datetime.now().strftime("%H")
minute = datetime.now().strftime("%M")
next_month = datetime(int(year), int(month)+1, int(day), int(hour), int(minute), 0, tzinfo=pytz.UTC)
#next_month = next_month.replace(tzinfo=pytz.UTC)
for component in gcal.walk():
if component.name == "VEVENT":
# time printed out in format:
# year-month-day hour:min:sec
summ = component.get('summary')
start = component.get('dtstart').dt
end = component.get('dtend').dt
if now <= start <= next_month:
print("Worked")
print(summ, start, end)
我已经尝试过使用 replace
将我的时间更改为 utc 并将其放入 next_month
变量本身,它们都给出了上面相同的错误。
我试过使用此处生成的 .ics 文件,所以不会是同一个问题,但在某些情况下开始是 datetime.datetime
,在其他情况下是 datetime.date
。
此解决方案适用于我的 .ics 文件
from icalendar import Calendar
from datetime import datetime
from dateutil.relativedelta import relativedelta
def read_from_cal():
g = open('example.ics', 'rb')
gcal = Calendar.from_ical(g.read())
today = datetime.now()
next_month = today + relativedelta(months=1)
for component in gcal.walk():
if component.name == "VEVENT":
summ = component.get('summary')
start = component.get('dtstart').dt
end = component.get('dtend').dt
if isinstance(start, datetime):
start = start.replace(tzinfo=None)
if start <= next_month:
print("Worked (datetime)")
print(summ, start, end)
else:
# some events are stored as a date
if start <= next_month.date():
print("Worked (date)")
print(summ, start, end)
read_from_cal()
我试图在下个月内从 ics 文件中提取事件,但我自己制作日期时间并与 ics 文件中的日期时间进行比较似乎不起作用并且出现错误 TypeError: can't compare offset-naive and offset-aware datetimes
我尝试了 here 找到的答案,但仍然出现相同的错误。下面是我正在使用的代码。
def read_from_cal():
g = open('calendar.ics', 'rb')
gcal = Calendar.from_ical(g.read())
year = datetime.now().year
month = datetime.now().month
day = datetime.now().day
hour = datetime.now().strftime("%H")
minute = datetime.now().strftime("%M")
next_month = datetime(int(year), int(month)+1, int(day), int(hour), int(minute), 0, tzinfo=pytz.UTC)
#next_month = next_month.replace(tzinfo=pytz.UTC)
for component in gcal.walk():
if component.name == "VEVENT":
# time printed out in format:
# year-month-day hour:min:sec
summ = component.get('summary')
start = component.get('dtstart').dt
end = component.get('dtend').dt
if now <= start <= next_month:
print("Worked")
print(summ, start, end)
我已经尝试过使用 replace
将我的时间更改为 utc 并将其放入 next_month
变量本身,它们都给出了上面相同的错误。
我试过使用此处生成的 .ics 文件,所以不会是同一个问题,但在某些情况下开始是 datetime.datetime
,在其他情况下是 datetime.date
。
此解决方案适用于我的 .ics 文件
from icalendar import Calendar
from datetime import datetime
from dateutil.relativedelta import relativedelta
def read_from_cal():
g = open('example.ics', 'rb')
gcal = Calendar.from_ical(g.read())
today = datetime.now()
next_month = today + relativedelta(months=1)
for component in gcal.walk():
if component.name == "VEVENT":
summ = component.get('summary')
start = component.get('dtstart').dt
end = component.get('dtend').dt
if isinstance(start, datetime):
start = start.replace(tzinfo=None)
if start <= next_month:
print("Worked (datetime)")
print(summ, start, end)
else:
# some events are stored as a date
if start <= next_month.date():
print("Worked (date)")
print(summ, start, end)
read_from_cal()