Last active
October 28, 2021 13:08
-
-
Save dbremner/39c0ffe666bea42e180c4a42dafe956c to your computer and use it in GitHub Desktop.
Replacing ctime() with strftime()
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
// ctime is an obsolete time function that is present in many older Unix programs. | |
// It's common for programs to call it and then modify the output. | |
// Here's a strftime format string that should produce the same output. | |
// dest_len should be >= 26 | |
static void get_time_str(time_t val, char *dest, size_t dest_len) | |
{ | |
struct tm result; | |
localtime_r(&val, &result); | |
// e.g. Wed Oct 27 18:28:32 2021\n | |
strftime(dest, dest_len, "%a %b %e %H:%M:%S %Y\n", &result); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment