Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/70.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 以与“相同的方式设置日期/时间”;ls-l";命令_C_Linux_Macos_Unix_Posix - Fatal编程技术网

C 以与“相同的方式设置日期/时间”;ls-l";命令

C 以与“相同的方式设置日期/时间”;ls-l";命令,c,linux,macos,unix,posix,C,Linux,Macos,Unix,Posix,我正在OS X中实现一个版本的ls-la命令。我碰巧注意到ls命令列出了一些文件的年份和其他文件的时间,如下所示: -rwxr-xr-x 1 Jenna 197609 3584 Mar 13 2015 untitled1.exe* drwxr-xr-x 1 Jenna 197609 0 Mar 2 07:48 Videos/ 经过一些研究,我发现超过6个月的文件可以得到一年的时间,而那些没有得到实际时间的文件。如何使这些文件的日期和时间正确显示年份或时间?目前,我有以

我正在OS X中实现一个版本的
ls-la
命令。我碰巧注意到
ls
命令列出了一些文件的年份和其他文件的时间,如下所示:

-rwxr-xr-x 1 Jenna 197609     3584 Mar 13  2015 untitled1.exe*
drwxr-xr-x 1 Jenna 197609        0 Mar  2 07:48 Videos/
经过一些研究,我发现超过6个月的文件可以得到一年的时间,而那些没有得到实际时间的文件。如何使这些文件的日期和时间正确显示年份或时间?目前,我有以下代码,始终显示时间:

int main()
{
   struct stat *fileInfo;
   char dir[] = "~/Desktop/file.txt";
   stat(dir, &fileInfo); 
   printf("%.12s ", 4+ctime(&fileInfo->st_mtimespec));

   return 0;

}

strftime
是你的朋友。将其格式化为
ls
并不是那么简单。coreutils中ls使用的两种格式是

"%b %e  %Y"
"%b %e %H:%M"
以下是coreutils中ls文件中的一段

/*翻译器:ls输出需要对齐以便于阅读, 因此,要小心使用区域设置中的可变宽度字段。 注意%b由ls专门处理并正确对齐。 还请注意,将宽度指定为%5b与strftime一样是错误的 将在多字节区域设置中计数字节而不是字符*/ N_(“%b%e%H:%M”)

他们考虑了诸如区域设置(即多字节字符)等问题。他们还讨论了诸如当文件超过N个月时,他们会更改文件的显示方式等问题。您可以在这里找到很多信息

为了满足您的需要,一些简单的东西可能会很好,这里有一些代码可以为您指明正确的方向。通过将第三个参数更改为
strftime
,可以将格式更改为所需的任何格式

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
int main(void) {
  time_t t;
  srand((unsigned) time(&t));
  size_t i = 0;
  struct timespec ttime;
  while(i < 0xffffffff) {\
    ttime.tv_sec  = i;
    i += 3600;
    struct tm time_struct;
    localtime_r(&ttime.tv_sec, &time_struct);
    char time_str[1024];
    if(rand() > RAND_MAX/2) {
      strftime(time_str, sizeof(time_str), "a == %b %e  %Y", &time_struct);
    }
    else {
      strftime(time_str, sizeof(time_str), "b == %b %e %H:%M", &time_struct);
    }
    printf("%s\n", time_str);
  }
  exit(0);
}
函数的作用是:将timeptr中的信息格式化为 缓冲区s,根据格式指向的字符串


如果我没有记错的话,
ls-l
输出日期/时间或输出日期/年的决定因素是
stat
返回的
mtime
年是否与当前年匹配。如果文件
mtime
year与
current
year匹配,则提供
date/time
信息,如果
mtime
year不是当前年份,则提供
date/year
信息

为了进行比较,您需要为文件
mtime
now
填写
struct tm
。下面的示例使用
localtime\r
填充每个
struct tm
,进行比较,然后相应地输出文件详细信息

(代码将读取/比较
mtimes
s,并输出作为参数提供的所有文件名的时间格式。(占位符字符串用于提供文件权限、编号、用户、组、大小等的基本格式。)

程序使用/输出

$ ls -l tmp.c ls_time_date_fmt.c
-rw-r--r-- 1 david david 1312 Apr  6 03:15 ls_time_date_fmt.c
-rw-r--r-- 1 david david 2615 May 23  2014 tmp.c
$ ./bin/ls_time_date_fmt ls_time_date_fmt.c tmp.c
permission 1 user group 12345 Apr  6 03:15 ls_time_date_fmt.c
permission 1 user group 12345 May 23  2014 tmp.c

如果您有任何问题,请告诉我。

看一下。您可能还想使用
difftime
来计算文件的年代。代码只是从
3600
-
0xffffff
生成随机数,根据
rand()是否输出日期格式的短版本或长版本>RAND_MAX/2
。这与ls-l决定是否在日期中显示年份的方式完全无关。让我在Linux和Mac上试试。我刚刚在Linux和Mac上试用过,在Linux上得到一条警告,但它显示日期等。你能提供更多信息吗?
$ ls -l tmp.c ls_time_date_fmt.c
-rw-r--r-- 1 david david 1312 Apr  6 03:15 ls_time_date_fmt.c
-rw-r--r-- 1 david david 2615 May 23  2014 tmp.c
$ ./bin/ls_time_date_fmt ls_time_date_fmt.c tmp.c
permission 1 user group 12345 Apr  6 03:15 ls_time_date_fmt.c
permission 1 user group 12345 May 23  2014 tmp.c