我有这样一个函数:
void ft_display_time(struct timespec ttime)
{
char *time_long;
time_long = ctime(&ttime.tv_sec);
if (time_long)
{
ft_printf("%.3s ", time_long + 4);
ft_printf("%.2s ", time_long + 8);
ft_printf("%.5s ", time_long + 11);
}
else
ft_putstr(" ");
}
我正在尝试获得类似 ls -l
的输出做。但是ctime(const time_t *clock)
将返回一个英文字符串(因此月份显示为“Dec”“Jan”“Aug”...),而ls -l
以宿主语言格式(例如法语)输出月份。
例子:
./myprog --long file
-rw-r--r-- 1 lotolo staff 0 Sep 17 19:55 c
/bin/ls -l
-rw-r--r-- 1 lotolo staff 0 17 Set 19:55 c
我怎样才能得到宿主语言格式的输出?
例如,ttime
等于stat.st_atimespec
或 stat.st_mtimespec
由 stat(filename, &stat)
返回或 lstat(filename, &stat)
我知道我将不得不改变我的大部分 ft_display_time()
函数,但我想知道是否有任何方法可以得到 ctime()
以正确的语言输出。
最佳答案
Scusa,但是我不知道如何通过仅使用另一个类似ctime()
的函数来实现这一点。如果我是你,我会尝试不同的方法:
使用execv() 执行/bin/ls
, 就像在 How to list first level directories only in C? 中一样为了获得最大的乐趣,请将其与 Redirecting exec output to a buffer or file 结合使用.
如果你想做一个普遍的改变,那么你可以使用 Petesh 所说的:
setlocale(LC_ALL, NULL)
其中,引用 ref ,:
Sets locale information to be used by the current program, either changing the entire locale or portions of it.
关于c - 我想获取宿主语言文件的时间(ctime(..)?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39550266/