commit de08ce096e91aba22b1817e322b24106077a6401
parent 38df544b0ff759c50bb4a0c9424a489189d9fa24
Author: Matthias Balk <mbalk@mbalk.de>
Date: Fri, 5 Feb 2021 17:30:01 +0100
Display all-day-events without time
Diffstat:
| M | src/utils.py | | | 47 | ++++++++++++++++++++++++++++++++--------------- |
1 file changed, 32 insertions(+), 15 deletions(-)
diff --git a/src/utils.py b/src/utils.py
@@ -27,14 +27,22 @@ def get_ya3_version():
return '0.2-dev'
-def _conv2local(dt):
- if not isinstance(dt, datetime):
- return datetime(dt.year, dt.month, dt.day, tzinfo=gettz())
- if dt.tzinfo is None:
- return datetime(dt.year, dt.month, dt.day,
- dt.hour, dt.minute, dt.second,
- tzinfo=gettz())
- return dt.astimezone(gettz())
+def _conv2local(dt, force_datetime=False):
+
+ if isinstance(dt, datetime):
+ if dt.tzinfo is None:
+ return datetime(dt.year, dt.month, dt.day,
+ dt.hour, dt.minute, dt.second,
+ tzinfo=gettz())
+ return dt.astimezone(gettz())
+
+ # date only
+ else:
+ if force_datetime:
+ return datetime(dt.year, dt.month, dt.day,
+ 0, 0, 0, tzinfo=gettz())
+ else:
+ return dt
def get_events(icsfilenames, ignore_configured_range=False):
@@ -42,8 +50,8 @@ def get_events(icsfilenames, ignore_configured_range=False):
def _is_in_range(event):
if ignore_configured_range:
return True
- start = _conv2local(event.dtstart.value)
- end = _conv2local(event.dtend.value)
+ start = _conv2local(event.dtstart.value, force_datetime=True)
+ end = _conv2local(event.dtend.value, force_datetime=True)
return (end >= datetime.now(gettz()) - config.MAX_AGE and
start < datetime.now(gettz()) + config.MAX_AHEAD)
@@ -53,7 +61,8 @@ def get_events(icsfilenames, ignore_configured_range=False):
for cal in vobject.readComponents(f):
events.extend(filter(_is_in_range, cal.vevent_list))
- return sorted(events, key=lambda ev: _conv2local(ev.dtstart.value))
+ return sorted(events, key=lambda ev: _conv2local(ev.dtstart.value,
+ force_datetime=True))
def event2string(event):
@@ -65,7 +74,15 @@ def event2string(event):
except AttributeError:
location = ""
- return ('%s - %s %s%s' % (start.strftime('%Y-%m-%d %H:%M'),
- end.strftime('%H:%M'),
- event.summary.value,
- location))
+ if isinstance(start, datetime):
+ return ('%s - %s %s%s' % (start.strftime('%Y-%m-%d %H:%M'),
+ end.strftime('%H:%M'),
+ event.summary.value,
+ location))
+
+ # date only
+ else:
+ return '{start} {summary}{location}'.format(
+ start=start.strftime('%Y-%m-%d'),
+ summary=event.summary.value,
+ location=location)