Commits

Mathias Panzenböck committed f1bb037

handle holidays expressed as muliti-day events

Comments (0)

Files changed (1)

 
 	def quota(self):
 		none_work_days = set()
+		one_day = timedelta(days=1)
+		zero = timedelta()
 		if self.holidays:
 			from icalendar import Calendar
 			if self.holidays.startswith("http:") or self.holidays.startswith("https:"):
 					cals = Calendar.from_string(f.read(), True)
 			for cal in cals:
 				for holiday in cal.subcomponents:
-					dt = holiday['dtstart'].dt
-					if isinstance(dt, datetime):
-						dt = dt.date()
-					if not isinstance(dt, date):
-						raise ValueError('Unknown date type: %s' % repr(dt))
-					none_work_days.add(dt)
+					start = holiday['dtstart'].dt
+					if isinstance(start, datetime):
+						startdate = startdate.date()
+					elif not isinstance(start, date):
+						raise ValueError('Unknown date type: %s' % repr(start))
+					else:
+						startdate = start
+					none_work_days.add(startdate)
+					if 'dtend' in holiday:
+						end = holiday['dtend'].dt
+					elif 'duration' in holiday:
+						end = start + holiday['duration'].td
+					else:
+						sys.stderr.write('*** error: no end date or duration for holiday: %s %s\n' %
+							(start, holiday['summary']))
+						continue
+
+					if isinstance(end, datetime):
+						if end - datetime(end.year, end.month, end.day) > zero:
+							end = end.date() + one_day
+						else:
+							end = end.date()
+					elif not isinstance(end, date):
+						raise ValueError('Unknown date type: %s' % repr(end))
+					dt = startdate + one_day
+					while dt < end:
+						none_work_days.add(dt)
+						dt += one_day
 		
 		day = self.first_day
 		today = datetime.now().date()
 		workdays = 0
 		quota = 0
-		one_day = timedelta(days=1)
 		while day <= today:
 			if day not in none_work_days:
 				wd = day.isoweekday()
 	parser.add_option('--repeat','-r',action='store_true',default=False,
 		help='Repeat last non-empty non-end message (may start a new session).')
 	parser.add_option('--holidays','-H',type='string',metavar='CALENDAR',
-		help='Use dates of the ical file CALENDAR as holidays as holidays (can be url).',
+		help='Use dates of the ical file CALENDAR as holidays (can be url).',
 		default=None)
 	parser.add_option('--set-quota',type='string',metavar='START_DATE END_DATE HOURS',
 		help='Set quota in h/workday for a given date range.',default=None)