The setlocale(), strftime() and gmtime() C functions
Compiler: Visual C++ Express Edition 2005
Compiled on Platform: Windows XP Pro SP2
Target platform: none, just for learning
Header file: Standard and Windows
Additional library: Windows Platform SDK
Additional project setting: Set project to be compiled as C
Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)
Other info: non-CLR or unmanaged
To do: Sets the current locale to "Italian" and "French"
To show: Using the setlocale(), strftime() and gmtime() C functions
/* Sets the current locale to "Italian" and "French" using the setlocale() function */
#include <stdio.h>
#include <locale.h>
#include <time.h>
// The gmtime() is deprecated. You may use _gmtime_s(), _gmtime32_s(), _gmtime64_s() for the secure versions
int main(void)
{
time_t ltime;
struct tm *testime;
unsigned char locstr[100];
/* Set the locale to Italian */
setlocale(LC_ALL, "italian");
time(<ime);
testime = gmtime(<ime);
/* %#x is the long date representation, appropriate to the current locale */
if(!strftime((char *)locstr, 100, "%#x", (const struct tm *)testime))
printf("strftime failed!\n");
else
printf("In Italian locale, strftime returns \"%s\"\n", locstr);
/* Set the locale to French */
setlocale(LC_ALL, "french");
time(<ime);
testime = gmtime(<ime);
/* %#x is the long date representation, appropriate to the current locale */
if(!strftime((char *)locstr, 100, "%#x", (const struct tm *)testime))
printf("strftime failed!\n");
else
printf("In French locale, strftime returns \"%s\"\n", locstr);
/* Set the locale back to the default environment */
setlocale(LC_ALL, "C");
time(<ime);
testime = gmtime(<ime);
printf("Back to default...\n");
if(!strftime((char *)locstr, 100, "%#x", (const struct tm *)testime))
printf("strftime failed!\n");
else
printf("In 'C' locale, strftime returns \"%s\"\n", locstr);
return 0;
}
Output example:
In Italian locale, strftime returns "domenica 17 dicembre 2006"
In French locale, strftime returns "dimanche 17 décembre 2006"
Back to default...
In 'C' locale, strftime returns "Sunday, December 17, 2006"
Press any key to continue . . .