Search code examples
cstatlocaltime

How to change month format for ls -l custom command


When you make this command :

find ok/ -exec ls -l -d {} \;

The terminal displays :

drwxrwxrwx 2 alexia alexia 4096 8 oct.  15 22:31 ok/

I tried to make my own find command to do the same thing with stat.h :

[...] //Other file information
struct stat fileStat;
stat(path,&fileStat)
char buffer[20];
struct tm *time
time = localtime(&(fileStat.st_mtime));
strftime(buffer, 20, "%b %e  %Y", time);
printf("%s", buffer);

and it displays:

[...]
8 Oct  15 22:31 ok/

The month format is not good, and I do no know how to get it right.

Cheers


Solution

  • By default, C programs run with the C locale. It looks as though you want a different locale, so use the setlocale() function with another locale. The simplest is specified by the empty string. (A value of "C" for locale specifies the minimal environment for C translation; a value of "" for locale specifies the locale-specific native environment. Other implementation-defined strings may be passed as the second argument to setlocale.):

    setlocale(LC_TIME, “”);
    

    This only affects time; you could use LC_ALL to change everything.