c - 我想获取宿主语言文件的时间(ctime(..)?)

标签 c linux io systems-programming ctime

我有这样一个函数:

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_atimespecstat.st_mtimespecstat(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/

相关文章:

c - C 中的字符串和 strcpy

linux - 获取刚刚启动的 screen session 的PID

linux - 在Linux中复制目录最快的方法是什么?

python - 启动 playonlinux 时出现错误 "No module named wxversion"

java - java.io 中最常用的模式是什么?

c - 从内存中读取数据mmap

c++ - 具有 C/C++ 接口(interface)的人脸检测库

c - 用户想玩多少次游戏 c 语言

c - 保存当前的阅读位置,以便我以后可以寻找它

c - 如何用Python编写二进制数据并用C读取它?