如何以特定格式打印time_t?
ls命令以以下格式打印时间:
Aug 23 06:07
我如何转换,从接收到的时间stat()
的mtime()
这个格式的本地时间?
回答:
使用strftime(您需要先转换time_t
为struct
tm*):
char buff[20];struct tm * timeinfo;
timeinfo = localtime (&mtime);
strftime(buff, sizeof(buff), "%b %d %H:%M", timeinfo);
格式:
%b - The abbreviated month name according to the current locale.%d - The day of the month as a decimal number (range 01 to 31).
%H - The hour as a decimal number using a 24-hour clock (range 00 to 23).
%M - The minute as a decimal number (range 00 to 59).
这是完整的代码:
struct stat info; char buff[20];
struct tm * timeinfo;
stat(workingFile, &info);
timeinfo = localtime (&(info.st_mtime));
strftime(buff, 20, "%b %d %H:%M", timeinfo);
printf("%s",buff);
以上是 如何以特定格式打印time_t? 的全部内容, 来源链接: utcz.com/qa/431212.html