Created
June 20, 2026 08:51
-
-
Save wh13371/8317bb877f2fd3fbf972facdf8453350 to your computer and use it in GitHub Desktop.
c timestamp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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