- Doesn't work this way, use
ctime_r
instead. - Works and is correct.
- Wrong type, you'd have to convert this first, e.g. using
localtime
. - Wrong variadic type,
char*
expected buttime_t*
given. - Incorrect free, as
ctime
manages its buffer itself, e.g. using a static one.
In general, most of these functions (i.e. asctime
, ctime
, localtime
, …) come in two variants these days: the historic form which uses some statically allocated buffer to hold the return value, and a more modern version with a _r
suffix which stands for reentrant. For the latter, you have to provide the output buffer yourself, which means you're responsible for allocating it. The great advantage is that multiple threads might use the reentrant versions simultaneously without fear of overwriting one another's results.