c - How can I print a date using the current locale settings? -


i'm trying write c program on linux prints current date. here in brazil want print in dd/mm/yyyy format , in print in mm/dd/yyyy format.

i thought "%x" specifier strftime supposed trick printing in mm/dd/yyyy format.

#include <stdio.h> #include <time.h>  int main(){    struct tm local_now;   {     time_t now;     time(&now);     struct tm *tmp = localtime(&now);     local_now = *tmp;   }    char buf[20];   strftime(buf, sizeof buf, "%x", &local_now);    printf("%s\n", buf);    return 0; } 

and here output running locale command:

lang=en_us.utf-8 language=en_us lc_ctype="en_us.utf-8" lc_numeric=pt_br.utf-8 lc_time=pt_br.utf-8 lc_collate="en_us.utf-8" lc_monetary=pt_br.utf-8 lc_messages="en_us.utf-8" lc_paper=pt_br.utf-8 lc_name=pt_br.utf-8 lc_address=pt_br.utf-8 lc_telephone=pt_br.utf-8 lc_measurement=pt_br.utf-8 lc_identification=pt_br.utf-8 lc_all= 

apparently lc_time=pt_br.utf-8 setting being ignored?

strftime , other locale-dependent functions use "c" locale default. in order use users locale must explicitly call setlocale function during program initialization:

#include <locale.h>  int main() {     setlocale(lc_all, "");      // ... } 

if pass empty string setlocale choose locale according standard locale environment variables (lc_all, lc_time, etc).


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -