I have a variable which uses time_t
data type. I would like to convert this type into "YYYY-MM-DD HH:MM:SS". I only know that it works in localtime()
per this example:
char buff[20];
time_t now = time(NULL);
strftime(buff, 20, "%Y-%m-%d %H:%M:%S", localtime(&now));
Any suggestion on how to convert it? I have the time which always increases every minute, not the fixed one like the localtime()
. I need this conversion for matching with datetime
type in the MySQL database.
The functions gmtime
and localtime
(for UTC and local time respectively) will turn an arbitrary time_t
into a struct tm
with individual fields for year, month and so on.
You can then just use strftime
as you have, or sprintf
to create strings from them, such as with:
char buff[11];
sprintf (buff, "%04d-%02d-%02d",
mytm.tm_year + 1900,
mytm.tm_mon + 1,
mytm.tm_mday);