Back to index

glibc  2.9
tst-ftime_l.c
Go to the documentation of this file.
00001 #include <locale.h>
00002 #include <stdio.h>
00003 #include <stdlib.h>
00004 #include <string.h>
00005 #include <time.h>
00006 #include <wchar.h>
00007 
00008 
00009 int
00010 main (void)
00011 {
00012   locale_t l;
00013   locale_t old;
00014   struct tm tm;
00015   char buf[1000];
00016   wchar_t wbuf[1000];
00017   int result = 0;
00018   size_t n;
00019 
00020   l = newlocale (LC_ALL_MASK, "de_DE.ISO-8859-1", NULL);
00021   if (l == NULL)
00022     {
00023       puts ("newlocale failed");
00024       exit (1);
00025     }
00026 
00027   memset (&tm, '\0', sizeof (tm));
00028 
00029   tm.tm_year = 102;
00030   tm.tm_mon = 2;
00031   tm.tm_mday = 1;
00032 
00033   if (strftime (buf, sizeof (buf), "%e %^B %Y", &tm) == 0)
00034     {
00035       puts ("initial strftime failed");
00036       exit (1);
00037     }
00038   if (strcmp (buf, " 1 MARCH 2002") != 0)
00039     {
00040       printf ("initial strftime: expected \"%s\", got \"%s\"\n",
00041              " 1 MARCH 2002", buf);
00042       result = 1;
00043     }
00044   else
00045     printf ("got \"%s\"\n", buf);
00046 
00047   /* Now using the extended locale model.  */
00048   if (strftime_l (buf, sizeof (buf), "%e %^B %Y", &tm, l) == 0)
00049     {
00050       puts ("strftime_l failed");
00051       result = 1;
00052     }
00053   else if (strcmp (buf, " 1 M\xc4RZ 2002") != 0)
00054     {
00055       printf ("strftime_l: expected \"%s\", got \"%s\"\n",
00056              " 1 M\xc4RZ 2002", buf);
00057       result = 1;
00058     }
00059   else
00060     {
00061       setlocale (LC_ALL, "de_DE.ISO-8859-1");
00062       printf ("got \"%s\"\n", buf);
00063       setlocale (LC_ALL, "C");
00064     }
00065 
00066   /* And the wide character version.  */
00067   if (wcsftime_l (wbuf, sizeof (wbuf) / sizeof (wbuf[0]), L"%e %^B %Y", &tm, l)
00068       == 0)
00069     {
00070       puts ("wcsftime_l failed");
00071       result = 1;
00072     }
00073   else if (wcscmp (wbuf, L" 1 M\x00c4RZ 2002") != 0)
00074     {
00075       printf ("wcsftime_l: expected \"%ls\", got \"%ls\"\n",
00076              L" 1 M\x00c4RZ 2002", wbuf);
00077       result = 1;
00078     }
00079   else
00080     {
00081       setlocale (LC_ALL, "de_DE.ISO-8859-1");
00082       printf ("got \"%ls\"\n", wbuf);
00083       setlocale (LC_ALL, "C");
00084     }
00085 
00086   old = uselocale (l);
00087 
00088   n = strftime (buf, sizeof (buf), "%e %^B %Y", &tm);
00089 
00090   /* Switch back.  */
00091   (void) uselocale (old);
00092 
00093   if (n == 0)
00094     {
00095       puts ("strftime after first uselocale failed");
00096       result = 1;
00097     }
00098   else if (strcmp (buf, " 1 M\xc4RZ 2002") != 0)
00099     {
00100       printf ("strftime in non-C locale: expected \"%s\", got \"%s\"\n",
00101              " 1 M\xc4RZ 2002", buf);
00102       result = 1;
00103     }
00104   else
00105     {
00106       setlocale (LC_ALL, "de_DE.ISO-8859-1");
00107       printf ("got \"%s\"\n", buf);
00108       setlocale (LC_ALL, "C");
00109     }
00110 
00111   if (strftime (buf, sizeof (buf), "%e %^B %Y", &tm) == 0)
00112     {
00113       puts ("strftime after second uselocale failed");
00114       result = 1;
00115     }
00116   else if (strcmp (buf, " 1 MARCH 2002") != 0)
00117     {
00118       printf ("initial strftime: expected \"%s\", got \"%s\"\n",
00119              " 1 MARCH 2002", buf);
00120       result = 1;
00121     }
00122   else
00123     printf ("got \"%s\"\n", buf);
00124 
00125   return result;
00126 }