From 418e9b25ebf96f3d5597dc41b47399211cbce65c Mon Sep 17 00:00:00 2001 From: dlichtistw Date: Sat, 22 Sep 2018 09:50:35 +0200 Subject: [PATCH] Documentation Added documentation for DST hack. --- icalevents/icalparser.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/icalevents/icalparser.py b/icalevents/icalparser.py index 8cce3fd..edfd713 100644 --- a/icalevents/icalparser.py +++ b/icalevents/icalparser.py @@ -258,13 +258,28 @@ def parse_events(content, start=None, end=None): if component.get('rrule'): rule = parse_rrule(component) dur = e.end - e.start - # Awful hack to adjust timezone throughout DST transitions. - found.extend(e.copy_to(dt.tzinfo.localize(dt.replace(tzinfo=None))) for dt in rule.between(start - dur, end, inc=True)) + found.extend(e.copy_to(adjust_dst(dt)) for dt in rule.between(start - dur, end, inc=True)) elif e.end >= start and e.start <= end: found.append(e) return found +# Awful hack to adjust timezone throughout DST transitions. +def adjust_dst (dt): + """ + Recomputes a datetime's UTC offset without changing the displayed value. + This is useful when shifting dates beyond DST transitions using dateutil's + relativedelta and rrule tools. + + :param dt: a datetime object + :return: adjusted datetime object + """ + if dt.tzinfo: + return dt.tzinfo.localize(dt.replace(tzinfo=None)) + else: + return dt + + def parse_rrule(component): if component.get('rrule'): # Parse the rrule, might return a rruleset instance, instead of rrule