Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 如何以特定格式打印时间?_C_Linux_Time_Timezone_Ls - Fatal编程技术网

C 如何以特定格式打印时间?

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

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 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);