1

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.

LotoLo
  • 327
  • 4
  • 17

1 Answers1

0

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.

Community
  • 1
  • 1
gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • I don't get the connection between my answer and the firsts two refs... I must be tired. Anyway I get setlocale(LC_ALL, NULL), i've tested it but It still output in english... I'm diggin' it – LotoLo Sep 17 '16 at 19:45
  • @LotoLo since `ls` gives you what you want, you could execute that instead of `ctime()`. As for `setlocale()`, I can't think of anything else... :/ – gsamaras Sep 17 '16 at 19:54