89 lines
2.2 KiB
C
89 lines
2.2 KiB
C
#include "date_time_handling.h"
|
|
#include "string_handling.h"
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <time.h>
|
|
#include <unistd.h>
|
|
|
|
// 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[]) {
|
|
// 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);
|
|
}
|
|
}
|
|
|
|
// 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);
|
|
}
|
|
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]);
|
|
printf ("%c%c", time[4], time[5]);
|
|
}
|
|
}
|
|
|
|
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++) {
|
|
if (date_time[i] != ':' && date_time[i] != '-') {
|
|
if (date_time[i] == ' ') {
|
|
transformed_string[j] = 'T';
|
|
} else {
|
|
transformed_string[j] = date_time[i];
|
|
}
|
|
|
|
j++;
|
|
}
|
|
|
|
}
|
|
memset(date_time, '\0', strlen(date_time));
|
|
strcpy(date_time, transformed_string);
|
|
}
|
|
|
|
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]);
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
return timezone_path;
|
|
}
|