以下代码中计算日期的最佳方法是什么?

时间:2019-04-18 00:57:07

标签: python-3.x icalendar

我需要计算假期的日期而不是学期的日期,但是日历文件(ics)就是这样!

我需要这样的结果: 第一学期 开始日期= 2019-04-05 结束日期= 2019-04-23

因此,第1学期的结束是假期的开始,而第2学期的开始就是假期的结束。这也给第四学期带来了一个问题,第四学期的假期是下一年第一学期的开始。

这是日历文件的示例

SUMMARY:Term 3 starts
END:VEVENT
BEGIN:VEVENT
DTSTAMP:20190206T011107Z
UID:20190206T011107Z-1367275628@marudot.com
DTSTART;VALUE=DATE:20190920
DTEND;VALUE=DATE:20190920
SUMMARY:Term 3 ends
END:VEVENT
BEGIN:VEVENT
DTSTAMP:20190206T011107Z
UID:20190206T011107Z-225094268@marudot.com
DTSTART;VALUE=DATE:20191007
DTEND;VALUE=DATE:20191007
SUMMARY:Term 4 starts
END:VEVENT
BEGIN:VEVENT
DTSTAMP:20190206T011107Z
UID:20190206T011107Z-2103345721@marudot.com
DTSTART;VALUE=DATE:20191220
DTEND;VALUE=DATE:20191220
SUMMARY:Term 4 ends
END:VEVENT
BEGIN:VEVENT
DTSTAMP:20190206T011107Z
UID:20190206T011107Z-507236695@marudot.com
DTSTART;VALUE=DATE:20200128
DTEND;VALUE=DATE:20200128
SUMMARY:Term 1 starts
DESCRIPTION:Students start 29 January in government schools
END:VEVENT
BEGIN:VEVENT
DTSTAMP:20190206T011107Z
UID:20190206T011107Z-277640896@marudot.com
DTSTART;VALUE=DATE:20200327
DTEND;VALUE=DATE:20200327
SUMMARY:Term 1 ends
END:VEVENT
BEGIN:VEVENT
DTSTAMP:20190206T011107Z
UID:20190206T011107Z-927611244@marudot.com
DTSTART;VALUE=DATE:20200414
DTEND;VALUE=DATE:20200414

from icalendar import Calendar, Event
g = open(VSH,'rb')
mycal = Calendar.from_ical(g.read())
for component in mycal.walk():
    if component.name == "VEVENT":
        mySummary = component.get('summary')

        if component.get('summary').find("starts") > 1:
            print(component.get('summary').replace("starts",""))
            print("we have a start event", component.get('dtstart').dt)
        elif component.get('summary').find("ends") > 1:
            print("we have a end event", component.get('dtend').dt)
g.close()

0 个答案:

没有答案