This repository has been archived on 2025-02-01. You can view files and clone it, but cannot push or open issues or pull requests.
ics_cli/src/date_time_handling.c

47 lines
1.2 KiB
C
Raw Normal View History

2023-08-15 16:31:07 +02:00
#include "date_time_handling.h"
2023-08-23 20:03:26 +02:00
#include <stdio.h>
#include <stdlib.h>
2023-08-15 16:31:07 +02:00
#include <time.h>
#include <string.h>
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);
strftime(buffer, buffer_size, "%Y%m%dT%H%M%S", my_tm_local);
}
2023-08-23 20:03:26 +02:00
// 20230823T194138 -> 2023-08-23 19:41:38
void pretty_print_date_time(char date_time[]) {
char *date = strtok(date_time, "T");
char *time = strtok(NULL, "T");
if (date == NULL) {
printf ("\nError: date points to NULL!\n");
exit(1);
}
2023-08-23 20:03:26 +02:00
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]);
2023-08-23 20:03:26 +02:00
if (time != NULL) {
printf (" %c%c:", time[0], time[1]);
printf ("%c%c:", time[2], time[3]);
printf ("%c%c", time[4], time[5]);
}
}
void print_end_date(char end_date[]) {
// end_date is all day event
if (strlen(end_date) == 8)
return;
strtok(end_date, "T");
char *time = strtok(NULL, "T");
printf (" - %c%c:", time[0], time[1]);
printf ("%c%c:", time[2], time[3]);
printf ("%c%c", time[4], time[5]);
}