How to get a locale specific date time value in C++

This took me a few hours of searching [c++, culture, locale specific, date time,....]

setlocale is your buddy here. There are sub categories - constants prefixed with LC_. You can change the locale for just date time for example.
setlocale when called with a NULL value for the second param returns the current locale set for the category [first param]
For different format strings, check MSDN for "strftime". %c, %x are formatstrings that take current locale into consideration.

This code snippet runs under VS20005. Tweak as per taste..
e.g. If you modify your locale - Control Panel > Regional Settings > Locale / Language to French

CString sCurrentLocale = setlocale(LC_ALL, NULL);
setlocale(LC_ALL, "");

CString strCurrentTime = CTime::GetCurrentTime().Format(_T("Generated : %#c"));
// produces "Generated : jeudi 31 janvier 2008 20:21:07"

setlocale(LC_ALL, sCurrentLocale );
strCurrentTime = CTime::GetCurrentTime().Format(_T("Generated : %#c"));
// produces "Generated : Thursday, January 31, 2008 20:23:08"

No comments:

Post a Comment