From 9e08553232f00b2736222d2f112087c9dad2e75f Mon Sep 17 00:00:00 2001 From: Max G Date: Tue, 26 Mar 2019 22:30:19 +0100 Subject: [PATCH] Added centralized formatting --- Calendar/TextFormatter.py | 53 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 Calendar/TextFormatter.py diff --git a/Calendar/TextFormatter.py b/Calendar/TextFormatter.py new file mode 100644 index 0000000..3931911 --- /dev/null +++ b/Calendar/TextFormatter.py @@ -0,0 +1,53 @@ +from settings import hours + +first_occurrence_char = '[' +middle_occurrence_char = '|' +last_occurrence_char = ']' + +def time_str (dt): + if hours is "12": + return dt.strftime("%I:%M %p") + elif hours is "24": + return dt.strftime("%H:%M") + else: + return str(dt) + +def event_prefix_str (event, relative_date=None): + if relative_date is None: + relative_date = event.begin_datetime.date() + + #Is multiday event + if event.begin_datetime.day is not event.end_datetime.day or \ + event.begin_datetime.month is not event.end_datetime.month: + return event_time_summary(event) + + #Relative to + #First day + if event.begin_datetime.day is relative_date.day and \ + event.begin_datetime.month is relative_date.month: + return event_time_summary(event) + first_occurrence_char + + #Last day + elif event.end_datetime.day is relative_date.day and \ + event.end_datetime.month is relative_date.month: + event.begin_datetime = event.end_datetime + return event_time_summary(event) + last_occurrence_char + + #Some day + else: + event.allday = True + return event_time_summary(event) + +def event_time_summary (event): + if event.allday: + return "•" + else: + return time_str(event.begin_datetime) + +def date_str(dt): + return self.__remove_leading_zero__(dt.strftime('%d %b')) + +def remove_leading_zero (text): + while text[0] is '0': + text = text[1:] + return text \ No newline at end of file