Wednesday 15 January 2014

parse dates with icalendar and compare to python datetime -



parse dates with icalendar and compare to python datetime -

i have .ics file extract of events occur on today's day. think i'm having problem converting icalendar dtstart , dtend python datetimes. i've tried follow documentation @ icalendar.readthedocs.org. list i'm getting empty, should not case.

this code:

import urllib2 import json datetime import datetime icalendar import calendar, event, vdatetime def gettodayevents(icsfile): cal = calendar.from_ical(icsfile) today = datetime.now().date() entries = [] event in cal.walk('vevent'): dtstart = event['dtstart'] dtend = event['dtend'] start = vdatetime.from_ical(dtstart) //trouble here? end = vdatetime.from_ical(dtend) if start <= today <= end: entry = {'summary' : event['summary'] } entries.append(entry) output = json.dumps(entries) homecoming output //this list empty

and , ics entry looks like:

begin:vevent summary:jonny smith dtstart;value=date:20140731 dtend;value=date:20150802 uid: 12345 class:public priority:5 dtstamp:20141006t160145z transp:opaque status:confirmed sequence:0 location:mansfield\, ga x-microsoft-cdo-appt-sequence:0 x-microsoft-cdo-busystatus:free x-microsoft-cdo-intendedstatus:busy x-microsoft-cdo-alldayevent:true x-microsoft-cdo-importance:1 x-microsoft-cdo-insttype:0 x-microsoft-disallow-counter:false end:vevent

dtstart, dtend properties have .dt attribute:

#!/usr/bin/env python import json datetime import date import icalendar # $ pip install icalendar today = date.today() calendar = icalendar.calendar.from_ical(ics_file) entries = [dict(summary=event['summary']) event in calendar.walk('vevent') if event['dtstart'].dt <= today <= event['dtend'].dt] print(json.dumps(entries, indent=2, sort_keys=true)) output [ { "summary": "jonny smith" } ]

python datetime icalendar

No comments:

Post a Comment