Search code examples
cposixlocaleglibc

Access locale information


What is the most portable way to access locale information?

I'm interested in time locale data, such as month names, day of week names, local time format etc.

Ideally I'd like a POSIX interface, but if it doesn't exist, glibc-specific one will do.

If possible, getting the information about the locale X shouldn't require setting it (using uselocale() or similar).

Calling strftime() many times with all sorts of parameters is considered a hack, not a solution.

If there's nothing better, I'm willing to consider directly parsing glibc's locale files if there's a reliable way to determine their location.


Solution

  • nl_langinfo is a POSIX-standard interface for returning that information and appears to have available all of the things that you're looking for. Sadly, it does require that you call setlocale before calling it. I don't see an interface that lets you query an arbitrary locale without first making it the current locale.