From 578bf0b0f5e180ee7d4ad5a642a16a8a42194d02 Mon Sep 17 00:00:00 2001 From: bjt-user Date: Wed, 10 Jul 2024 16:42:43 +0200 Subject: [PATCH] fixed pretty_print_date_time --- src/date_time_handling.c | 7 +++++++ unit-tests/Makefile | 3 +++ unit-tests/test_pretty_print_date_time.c | 14 ++++++++++++++ unit-tests/test_print_upcoming.c | 4 +++- 4 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 unit-tests/test_pretty_print_date_time.c diff --git a/src/date_time_handling.c b/src/date_time_handling.c index bb35d3d..d6717bd 100644 --- a/src/date_time_handling.c +++ b/src/date_time_handling.c @@ -22,6 +22,10 @@ void get_date(char buffer[]) { // 20230823T194138 -> 2023-08-23 19:41:38 void pretty_print_date_time(char date_time[]) { + // copy date_time because strtok will destroy it + char date_time_copy[15] = ""; + strcpy(date_time_copy, date_time); + char *date = strtok(date_time, "T"); char *time = strtok(NULL, "T"); if (date == NULL) { @@ -37,6 +41,9 @@ void pretty_print_date_time(char date_time[]) { printf ("%c%c:", time[2], time[3]); printf ("%c%c", time[4], time[5]); } + + // put variable date_time back together + strcpy(date_time, date_time_copy); } void marshall_date_time(char date_time[]) { diff --git a/unit-tests/Makefile b/unit-tests/Makefile index b7ddd8a..414c49c 100644 --- a/unit-tests/Makefile +++ b/unit-tests/Makefile @@ -8,6 +8,9 @@ test_parse_ics_file: test_print_upcoming: $(CC) $(CFLAGS) test_print_upcoming.c ../src/parse_ics.c ../src/string_handling.c ../src/list_handling.c ../src/date_time_handling.c ../src/read_until_string.c ../src/read_until_nl.c -o test_print_upcoming.out +test_pretty_print_date_time: + $(CC) $(CFLAGS) test_pretty_print_date_time.c ../src/string_handling.c ../src/date_time_handling.c ../src/read_until_string.c ../src/read_until_nl.c -o test_pretty_print_date_time.out + .PHONY:clean clean: -rm -vf *.out diff --git a/unit-tests/test_pretty_print_date_time.c b/unit-tests/test_pretty_print_date_time.c new file mode 100644 index 0000000..2e4c555 --- /dev/null +++ b/unit-tests/test_pretty_print_date_time.c @@ -0,0 +1,14 @@ +#include "../src/date_time_handling.h" +#include + +int main() { + char current_date[] = "20240710T103000"; + + printf("current_date: %s\n", current_date); + printf("strlen(current_date): %ld\n\n", strlen(current_date)); + + pretty_print_date_time(current_date); + + printf("\n\ncurrent_date: %s\n", current_date); + printf("strlen(current_date): %ld\n", strlen(current_date)); +} diff --git a/unit-tests/test_print_upcoming.c b/unit-tests/test_print_upcoming.c index 287a52d..8054f7f 100644 --- a/unit-tests/test_print_upcoming.c +++ b/unit-tests/test_print_upcoming.c @@ -6,7 +6,9 @@ int main() { // initialize empty list struct event *head = NULL; - char *current_date = "20240710T103000"; + char *current_date = "20240710T113000"; + + printf("DEBUG - current_date: %s\n\n", current_date); parse_ics_file("/home/bf/.local/share/evolution/calendar/system/calendar.ics", &head);