如何以特定格式打印 time_t? [英] How to print time_t in a specific format?
本文介绍了如何以特定格式打印 time_t?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
ls 命令以这种格式打印时间:
ls command prints time in this format:
Aug 23 06:07
如何将 stat()
的 mtime()
收到的时间转换为本地时间格式?
How can I convert time received from stat()
's mtime()
into this format for local time?
推荐答案
使用strftime(需要转换time_t
到 struct tm*
首先):
Use strftime (you need to convert time_t
to struct tm*
first):
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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文