Skip to content

Instantly share code, notes, and snippets.

@wh13371
Created June 20, 2026 08:51
Show Gist options
  • Select an option

  • Save wh13371/8317bb877f2fd3fbf972facdf8453350 to your computer and use it in GitHub Desktop.

Select an option

Save wh13371/8317bb877f2fd3fbf972facdf8453350 to your computer and use it in GitHub Desktop.
c timestamp
#include <stdio.h> // printf()
#include <time.h> // time()
#include <sys/time.h> // localtime()
char timestamp[28];
char *now() {
time_t t = time(NULL);
struct tm* local_time = localtime(&t);
struct timeval epoch;
struct timezone tz;
gettimeofday(&epoch, &tz);
snprintf(timestamp, sizeof(timestamp) - 1, "%04d-%02d-%02d %02d:%02d:%02d.%06ld\n",
local_time->tm_year + 1900, local_time->tm_mon + 1, local_time->tm_mday,
local_time->tm_hour, local_time->tm_min, local_time->tm_sec, (unsigned long)epoch.tv_usec);
return timestamp;
}
int main() {
printf("%s\n", now());
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment