title: c - setlocale() not working as expect
body: <p>The following program use <code>setlocale()</code> to take locale settings from environment variable, and print time.</p>
  
  <p><strong>locale_test.c</strong>:</p>
  
  <pre><code>// locale test
  
  #include &lt;stdio.h&gt;
  #include &lt;locale.h&gt;
  #include &lt;time.h&gt;
  
  // locale test
  void locale_test() {
      // use environment variable to get locale setting,
      setlocale(LC_ALL, "");
  
      char buf[26];
      time_t now = time(NULL);
      ctime_r(&amp;now, buf);
      printf("time: %s", buf);
  
      char *time_locale = setlocale(LC_TIME, NULL);
      printf("current LC_TIME locale: %s\n", time_locale);
  }
  
  int main(int argc, char *argv[]) {
      locale_test();
      return 0;
  }
  </code></pre>
  
  <p><strong>Execute:</strong></p>
  
  <ul>
  <li><p><code>LC_ALL=fr_FR LC_TIME=fr_FR ./a.out</code>, output is:</p>
  
  <blockquote>
    <p>time: Wed Oct 14 13:16:35 2015<br />
    current LC_TIME locale: C</p>
  </blockquote></li>
  <li><p><code>LC_ALL=en_US LC_TIME=en_US ./a.out</code>, output is:</p>
  
  <blockquote>
    <p>time: Wed Oct 14 13:17:12 2015<br />
    current LC_TIME locale: C</p>
  </blockquote></li>
  </ul>
  
  <p><strong>The question is:</strong></p>
  
  <ul>
  <li>The result of 2 execution are expected to be different format, due to different locale, but they are the same, why?</li>
  <li>When try to get current <code>LC_TIME</code>, it just print <code>C</code>, but not the value specified from environment variable.</li>
  </ul>
  
  <hr>
  
  <p><strong>Update - Solution:</strong></p>
  
  <p>According to comment &amp; answer, following changes are made:</p>
  
  <ul>
  <li>Use correct locale name from result of <code>locale -a</code></li>
  <li>Use <code>strftime()</code> to generate the time string instead of <code>ctime()</code></li>
  </ul>
  
  <p>The new program:</p>
  
  <pre><code>// locale test
  
  #include &lt;stdio.h&gt;
  #include &lt;locale.h&gt;
  #include &lt;time.h&gt;
  
  // locale test
  void locale_test() {
      // use environment variable to get locale setting,
      if(setlocale(LC_ALL, "") == NULL) {
          printf("error while setlocale()\n");
      }
  
      // get current LC_TIME
      char *time_locale = setlocale(LC_TIME, NULL);
      if(time_locale == NULL) {
          printf("error while setlocale()\n");
      } else {
          printf("LC_TIME: %s\n", time_locale);
      }
  
      // print time in locale,
      size_t buf_size = 50;
      char buf[buf_size];
      // char *format = "%F %T %z";
      char *format = "%A, %d %B %Y, %H:%M:%S %Z";
  
      time_t now = time(NULL);
      struct tm tm_now;
      localtime_r(&amp;now, &amp;tm_now);
  
      strftime(buf, buf_size, format, &amp;tm_now);
      printf("time: %s\n", buf);
  }
  
  int main(int argc, char *argv[]) {
      locale_test();
      return 0;
  }
  </code></pre>
  
  <p>Then execute:</p>
  
  <ul>
  <li><p><code>LC_ALL=en_US.utf8 LC_TIME=en_US.utf8 ./a.out</code>, output is:</p>
  
  <blockquote>
    <p>LC_TIME: en_US.utf8<br />
    time: Wednesday, 14 October 2015, 16:36:36 CST</p>
  </blockquote></li>
  <li><p><code>LC_ALL=zh_CN.utf8 LC_TIME=zh_CN.utf8 ./a.out</code>, output is:</p>
  
  <blockquote>
    <p>LC_TIME: zh_CN.utf8<br />
    time: 星期三, 14 十月 2015, 16:38:10 CST</p>
  </blockquote></li>
  <li><p><code>LC_ALL=en_US.utf8 LC_TIME=en_US.utf8 ./a.out</code>, output is:</p>
  
  <blockquote>
    <p>LC_TIME: ja_JP.utf8<br />
    time: 水曜日, 14 10月 2015, 16:40:05 CST</p>
  </blockquote></li>
  </ul>
  
  <p>That's work as expect.</p>