diff --git a/src/date_time_handling.c b/src/date_time_handling.c index d6717bd..248a649 100644 --- a/src/date_time_handling.c +++ b/src/date_time_handling.c @@ -5,37 +5,87 @@ #include #include #include +#include // buffer needs to contain a string with a strlen of 15 (format: "xxxxxxxxTxxxxxx") // or a strlen of 16 (format: "YYYYmmddTHHMMSSZ") -void get_date(char buffer[]) { +void get_date (char buffer[]) { // add 1 because strlen does not include the null character - size_t buffer_size = strlen(buffer) + 1; - time_t my_unix_ts = time(NULL); - struct tm* my_tm_local = localtime(&my_unix_ts); - if (strlen(buffer) == 15) { - strftime(buffer, buffer_size, "%Y%m%dT%H%M%S", my_tm_local); - } else if (strlen(buffer) == 16) { - strftime(buffer, buffer_size, "%Y%m%dT%H%M%SZ", my_tm_local); + size_t buffer_size = strlen (buffer) + 1; + time_t my_unix_ts = time (NULL); + struct tm* my_tm_local = localtime (&my_unix_ts); + if (strlen (buffer) == 15) { + strftime (buffer, buffer_size, "%Y%m%dT%H%M%S", my_tm_local); + } + else if (strlen (buffer) == 16) { + strftime (buffer, buffer_size, "%Y%m%dT%H%M%SZ", my_tm_local); } } // 20230823T194138 -> 2023-08-23 19:41:38 -void pretty_print_date_time(char date_time[]) { +// 20230823T194138Z -> 2023-08-23 19:41:38 +// 20241209 -> 2024-12-09 +// caller has to free() the returned char array +char* pretty_date_time (char date_time[]) { + int pdt_len = 20; + if (!strchr (date_time, 'T')) { + pdt_len = 11; + } + + char* pretty_dt = malloc (pdt_len); + memset(pretty_dt, '\0', pdt_len); + int dt_counter = 0; + + if (!strcmp(date_time, "")) { + return pretty_dt; + } + + for (int i = 0; i < (pdt_len-1); i++) { + if (i == 4 || i == 7) { + pretty_dt[i] = '-'; + continue; + } + if (i == 13 || i == 16) { + pretty_dt[i] = ':'; + continue; + } + + if (date_time[dt_counter] == 'T') { + pretty_dt[i] = ' '; + } else { + pretty_dt[i] = date_time[dt_counter]; + } + dt_counter++; + } + + // null-terminate string + pretty_dt[strlen(pretty_dt)] = '\0'; + + return pretty_dt; +} + +// 20230823T194138 -> 2023-08-23 19:41:38 +void pretty_print_date_time (char date_time[]) { + char* pdt = pretty_date_time(date_time); + + printf("%s", pdt); + + free(pdt); + /* // copy date_time because strtok will destroy it char date_time_copy[15] = ""; - strcpy(date_time_copy, date_time); + strcpy (date_time_copy, date_time); - char *date = strtok(date_time, "T"); - char *time = strtok(NULL, "T"); + char* date = strtok (date_time, "T"); + char* time = strtok (NULL, "T"); if (date == NULL) { printf ("\nError: date points to NULL!\n"); - exit(1); + exit (1); } printf ("%c%c%c%c-", date[0], date[1], date[2], date[3]); printf ("%c%c-", date[4], date[5]); printf ("%c%c", date[6], date[7]); - + if (time != NULL) { printf (" %c%c:", time[0], time[1]); printf ("%c%c:", time[2], time[3]); @@ -43,63 +93,71 @@ void pretty_print_date_time(char date_time[]) { } // put variable date_time back together - strcpy(date_time, date_time_copy); + strcpy (date_time, date_time_copy); + */ } -void marshall_date_time(char date_time[]) { - char transformed_string[strlen(date_time)]; +void marshall_date_time (char date_time[]) { + char transformed_string[strlen (date_time)]; int j = 0; - remove_nl_and_cr(date_time); - for (int i = 0; i<=strlen(date_time); i++) { + remove_nl_and_cr (date_time); + for (int i = 0; i <= strlen (date_time); i++) { if (date_time[i] != ':' && date_time[i] != '-') { if (date_time[i] == ' ') { transformed_string[j] = 'T'; - } else { + } + else { transformed_string[j] = date_time[i]; } - + j++; } - + } - memset(date_time, '\0', strlen(date_time)); - strcpy(date_time, transformed_string); + memset (date_time, '\0', strlen (date_time)); + strcpy (date_time, transformed_string); } -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); +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) { + if (strlen (end_date) == 8) { if (time_difference == 86400) { return; - } else { + } + else { printf (" - "); 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); + 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 { + } + else { // end_date is not an all day event - char *end_date_chunk = strtok(end_date, "T"); - char *end_time_chunk = strtok(NULL, "T"); - char *start_date_chunk = strtok(start_date, "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 (" - "); // 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]); + 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]); @@ -107,40 +165,41 @@ void print_end_date(char end_date[], char start_date[]) { } // YYYYmmdd -> unix timestamp -time_t transform_date_to_unix_ts(char date_str[]) { +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}; + 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); + unix_stamp = mktime (&my_tm); 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); +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); +char* get_tz () { + char* timezone_path = malloc (256); + ssize_t bytes_read = readlink ("/etc/localtime", timezone_path, 255); if (bytes_read != -1) { // Null-terminate the string - timezone_path[bytes_read] = '\0'; - } else { - perror("readlink"); - exit(1); + timezone_path[bytes_read] = '\0'; + } + else { + perror ("readlink"); + exit (1); } return timezone_path; diff --git a/src/date_time_handling.h b/src/date_time_handling.h index ed35db5..ee5d487 100644 --- a/src/date_time_handling.h +++ b/src/date_time_handling.h @@ -5,6 +5,7 @@ void get_date(char buffer[]); char *get_tz(); +char* pretty_date_time(char date_time[]); 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[]); diff --git a/unit-tests/test_pretty_print_date_time.c b/unit-tests/test_pretty_print_date_time.c index 8d8f58e..adef443 100644 --- a/unit-tests/test_pretty_print_date_time.c +++ b/unit-tests/test_pretty_print_date_time.c @@ -1,7 +1,38 @@ #include "../src/date_time_handling.h" #include +#include +#include int main() { + // 1 + char* pdt = pretty_date_time("20251215T080000Z"); + + assert(!strcmp(pdt, "2025-12-15 08:00:00")); + + free(pdt); + + // 2 + char* pdt2 = pretty_date_time("20251215T080000"); + + assert(!strcmp(pdt2, "2025-12-15 08:00:00")); + + free(pdt2); + + // 3 + char* pdt3 = pretty_date_time(""); + + assert(!strcmp(pdt3, "")); + + free(pdt3); + + // 4 + char* pdt4 = pretty_date_time("20251215"); + + assert(!strcmp(pdt4, "2025-12-15")); + + free(pdt4); + + // 5 char current_date[] = "20240710T103000"; printf("current_date: %s\n", current_date); @@ -13,6 +44,7 @@ int main() { printf("strlen(current_date): %ld\n", strlen(current_date)); pretty_print_date_time("20251215T080000Z"); + printf("\n"); return 0; } diff --git a/unit-tests/test_print_upcoming.c b/unit-tests/test_print_upcoming.c index badf053..e867d57 100644 --- a/unit-tests/test_print_upcoming.c +++ b/unit-tests/test_print_upcoming.c @@ -6,6 +6,8 @@ int main() { // initialize empty list struct event *head = NULL; + // 1 + printf("\nTesting tests/failed_cal.ics:\n\n"); char *current_date = "20240710T113000"; printf("DEBUG - current_date: %s\n\n", current_date); @@ -13,4 +15,10 @@ int main() { parse_ics_file("../../tests/failed_cal.ics", &head); print_upcoming(head, current_date, 0); + + // 2 + printf("\nTesting tests/calendar.ics:\n\n"); + parse_ics_file("../../tests/calendar.ics", &head); + + print_upcoming(head, current_date, 0); }