C 如何以特定格式打印时间?
ls命令以以下格式打印时间:C 如何以特定格式打印时间?,c,linux,time,timezone,ls,C,Linux,Time,Timezone,Ls,ls命令以以下格式打印时间: Aug 23 06:07 如何将从stat()的mtime()接收的时间转换为本地时间的此格式?使用(您需要先将time\u t转换为struct tm*): 格式: %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
Aug 23 06:07
如何将从stat()
的mtime()
接收的时间转换为本地时间的此格式?使用(您需要先将time\u t
转换为struct tm*
):
格式:
%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);
然后使用
printf(“%s”,buff)代码>?准确-它将用可打印字符串填充buff
数组。我不熟悉不同的时间数据类型。你们能告诉我stat()的步骤是什么吗?时间包含了UTC 1970年1月1日00:00小时以来经过的秒数struct tm
包含分解为其组件的日历日期和时间。不,您不能传递mtime
。您需要将info.st\u mtime
传递给localtime函数。类似于clock\t
:
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);