Generating timestamps for application log files or system status monitors in C relies on POSIX <time.h> functions. time() fetches seconds since Epoch, localtime() breaks it down into a struct tm, and strftime() formats it cleanly.
Date & Time Formatting C Code Example
#include <stdio.h>
#include <time.h>
int main(void) {
time_t raw_time;
struct tm *time_info;
char buffer[80];
// Fetch seconds elapsed since Jan 1, 1970 UTC
time(&raw_time);
// Convert raw time to local timezone structure
time_info = localtime(&raw_time);
// Format into ISO 8601 string: YYYY-MM-DD HH:MM:SS
strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", time_info);
printf("Current Local Timestamp: %s\n", buffer);
return 0;
}
Comments and corrections