clinuxiosystems-programmingctime

I would like to obtain time (ctime(..)?) of a file in host language


I have a function like this:

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("             ");
}

I'm trying get an output like ls -l does. But ctime(const time_t *clock) will return a string in english (so months are displayed like "Dec" "Jan" "Aug" ...), while ls -l outputs months in the host language format (for example french).

Example:

./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

How could I have the output in host language format?

For instance, ttime is equal to stat.st_atimespec or stat.st_mtimespec returned by stat(filename, &stat) or lstat(filename, &stat) I know I will have to change most of my ft_display_time() funtion, but I would like to know if there is any way of getting ctime() output in the right language.


Solution

  • Scusa, but I don't know a way to do this by using another ctime()-like function only. If I were you though, I would try a different approach:

    Execute /bin/ls with execv(), just like in How to list first level directories only in C? For maximum pleasure, combine it with Redirecting exec output to a buffer or file.


    If you want to do a universal change, then you could use what Petesh said:

    setlocale(LC_ALL, NULL)
    

    which, quoting the ref,:

    Sets locale information to be used by the current program, either changing the entire locale or portions of it.