Skip to content

Instantly share code, notes, and snippets.

@dbremner
Last active October 28, 2021 13:08
Show Gist options
  • Save dbremner/39c0ffe666bea42e180c4a42dafe956c to your computer and use it in GitHub Desktop.
Save dbremner/39c0ffe666bea42e180c4a42dafe956c to your computer and use it in GitHub Desktop.
Replacing ctime() with strftime()
// 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