From d29fcdbec5f55c81a80d0afccb6aecd8dd2a88b5 Mon Sep 17 00:00:00 2001 From: bjoernf Date: Sun, 10 Sep 2023 10:41:36 +0200 Subject: [PATCH 1/2] improved printing of the end date and time --- README.md | 4 +-- docs/ics_format.txt | 2 ++ src/date_time_handling.c | 54 ++++++++++++++++++++++++++++++++++------ src/date_time_handling.h | 3 ++- src/list_handling.c | 2 +- 5 files changed, 53 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index e2a722d..9319563 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,6 @@ sudo make uninstall #### TODO -- add option to print upcoming events only until a certain date -- show end date for events that span over multiple days +- cap the number of upcoming events being printed (and introduce --all option) +- don't show the DTEND day of an all day event, but DTEND - 1 - tests diff --git a/docs/ics_format.txt b/docs/ics_format.txt index 33db92b..8b99bcb 100644 --- a/docs/ics_format.txt +++ b/docs/ics_format.txt @@ -13,6 +13,8 @@ an all day event can span over multiple days and look like this: DTSTART;VALUE=DATE:20230911^M$ DTEND;VALUE=DATE:20230916^M$ +The DTEND of an all day event is exclusively, it is not an all day event anymore! + appointment: DTSTART;TZID=/freeassociation.sourceforge.net/Continent/City: 20230909T090000 diff --git a/src/date_time_handling.c b/src/date_time_handling.c index e122540..f917d9f 100644 --- a/src/date_time_handling.c +++ b/src/date_time_handling.c @@ -59,17 +59,55 @@ void marshall_date_time(char date_time[]) { strcpy(date_time, transformed_string); } -void print_end_date(char end_date[]) { +void print_end_date(char end_date[], char start_date[]) { + time_t start_uts = transform_date_to_unix_ts(start_date); + time_t end_uts = transform_date_to_unix_ts(end_date); + double time_difference = difftime(end_uts, start_uts); + // end_date is all day event - if (strlen(end_date) == 8) - return; + if (strlen(end_date) == 8) { + if (time_difference == 86400) { + return; + } else { + printf (" - "); + pretty_print_date_time(end_date); + return; + } + } else { + // end_date is not an all day event - strtok(end_date, "T"); - char *time = strtok(NULL, "T"); + char *end_date_chunk = strtok(end_date, "T"); + char *end_time_chunk = strtok(NULL, "T"); + char *start_date_chunk = strtok(start_date, "T"); - printf (" - %c%c:", time[0], time[1]); - printf ("%c%c:", time[2], time[3]); - printf ("%c%c", time[4], time[5]); + printf (" - "); + + // only print the end date if it is not the same as the start date + if (strcmp(start_date_chunk, end_date_chunk) != 0) { + printf("%c%c%c%c-", end_date_chunk[0], end_date_chunk[1], \ + end_date_chunk[2], end_date_chunk[3]); + printf("%c%c-", end_date_chunk[4], end_date_chunk[5]); + printf("%c%c ", end_date_chunk[6], end_date_chunk[7]); + } + + printf ("%c%c:", end_time_chunk[0], end_time_chunk[1]); + printf ("%c%c:", end_time_chunk[2], end_time_chunk[3]); + printf ("%c%c", end_time_chunk[4], end_time_chunk[5]); + } +} + +// YYYYmmdd -> unix timestamp +time_t transform_date_to_unix_ts(char date_str[]) { + time_t unix_stamp = 0; + int date_only = atoi(date_str); + struct tm my_tm = {0}; + my_tm.tm_year = date_only / 10000 - 1900; + my_tm.tm_mon = (date_only % 10000) / 100 - 1; + my_tm.tm_mday = date_only % 100; + + unix_stamp = mktime(&my_tm); + + return unix_stamp; } char *get_tz() { diff --git a/src/date_time_handling.h b/src/date_time_handling.h index d76cda1..2f713d9 100644 --- a/src/date_time_handling.h +++ b/src/date_time_handling.h @@ -5,4 +5,5 @@ void get_date(char buffer[]); char *get_tz(); void pretty_print_date_time(char date_time[]); void marshall_date_time(char date_time[]); -void print_end_date(char end_date[]); +void print_end_date(char end_date[], char start_date[]); +time_t transform_date_to_unix_ts(char date_str[]); diff --git a/src/list_handling.c b/src/list_handling.c index d392be7..353d704 100644 --- a/src/list_handling.c +++ b/src/list_handling.c @@ -50,7 +50,7 @@ void print_upcoming(struct event *head, char current_start_date[]) { while (head != NULL) { if (strcmp(head->start_date, current_start_date) >= 0) { pretty_print_date_time(head->start_date); - print_end_date(head->end_date); + print_end_date(head->end_date, head->start_date); printf("\n%s\n", head->summary); if (head->next != NULL) printf("\n"); -- 2.45.2 From 66454922e1857bf92fe2603b4d881d7f16fec5fe Mon Sep 17 00:00:00 2001 From: bjoernf Date: Sun, 10 Sep 2023 11:36:46 +0200 Subject: [PATCH 2/2] fixed printing of excluded DTEND day for all day events --- README.md | 2 +- src/date_time_handling.c | 16 +++++++++++++++- src/date_time_handling.h | 1 + 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9319563..41cc6b4 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,6 @@ sudo make uninstall #### TODO +- fix bug when LOCATION field is present - cap the number of upcoming events being printed (and introduce --all option) -- don't show the DTEND day of an all day event, but DTEND - 1 - tests diff --git a/src/date_time_handling.c b/src/date_time_handling.c index f917d9f..bb35d3d 100644 --- a/src/date_time_handling.c +++ b/src/date_time_handling.c @@ -70,7 +70,10 @@ void print_end_date(char end_date[], char start_date[]) { return; } else { printf (" - "); - pretty_print_date_time(end_date); + end_uts -= 86400; + char *end_date_minus_one = transform_unix_ts_to_date(end_uts); + pretty_print_date_time(end_date_minus_one); + free(end_date_minus_one); return; } } else { @@ -110,6 +113,17 @@ time_t transform_date_to_unix_ts(char date_str[]) { return unix_stamp; } +// unix timestamp -> YYYYmmdd +// make sure to free the returned buffer +char *transform_unix_ts_to_date(time_t unix_ts) { + char *date_buffer = malloc(9); + struct tm *my_tm; + my_tm = localtime(&unix_ts); + strftime(date_buffer, 9, "%Y%m%d", my_tm); + + return date_buffer; +} + char *get_tz() { char *timezone_path = malloc(256); ssize_t bytes_read = readlink("/etc/localtime", timezone_path, 255); diff --git a/src/date_time_handling.h b/src/date_time_handling.h index 2f713d9..509ccaf 100644 --- a/src/date_time_handling.h +++ b/src/date_time_handling.h @@ -7,3 +7,4 @@ void pretty_print_date_time(char date_time[]); void marshall_date_time(char date_time[]); void print_end_date(char end_date[], char start_date[]); time_t transform_date_to_unix_ts(char date_str[]); +char *transform_unix_ts_to_date(time_t unix_ts); -- 2.45.2